# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1270622 | khoianh | Network (BOI15_net) | C++20 | 194 ms | 58952 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mn = 5e5 + 5;
ll n, a[mn], root;
vector<ll> v[mn], u;
void dfs(ll i, ll parent){
if(parent && (int)v[i].size() == 1){
u.push_back(i);
return;
}
for(auto j : v[i]){
if(j == parent) continue;
dfs(j, i);
}
return;
}
void solve(){
cin >> n;
for(int i = 1; i < n; ++i){
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i = 1; i <= n; ++i){
if(v[i].size() != 1) root = i;
}
dfs(root, 0);
cout << ((int)u.size() + 1) / 2 << "\n";
ll t = ((int)u.size() + 1) / 2;
for(int i = 0; i < t; ++i){
// if(i == 0) cout << u[i] << " " << root << "\n";
// else
cout << u[i] << " " << u[(i + t) % (int)u.size()] << "\n";
}
// if(t * 2 != (int)u.size()) cout << u.back() << " " << root << "\n";
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if(fopen(".INP", "r")) {
freopen(".INP", "r", stdin);
freopen(".OUT", "w", stdout);
}
int testCase = 1;
//cin >> testCase;
while(testCase--) solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |