This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Possible Overdose for this coder
#include <bits/stdc++.h>
#define put(x) cerr << #x << ": " << x << '\n'
#define line() cerr << "**************\n"
//#define F first
//#define S second
//#define mul(x, y) (((x) * (y)) %mod)
//#define bit(i, j) (((i)>>(j)) &1)
//#define left(id) ((id<<1) + 1)
//#define right(id) ((id<<1) + 2)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
const int maxn = 5e5 + 10;
int n;
vector <int> G[maxn];
int l[maxn], num;
void dfs(int v, int par){
if(G[v].size() == 1)
l[v] = 1;
for(int u: G[v]){
if(u == par)
continue;
dfs(u, v);
l[v] += l[u];
}
}
int find(int v, int par){
for(int u: G[v]){
if(u == par)
continue;
if(l[u] > num / 2)
return find(u, v);
}
return v;
}
vector <int> s[maxn];
void make(int v, int par, int x){
if(G[v].size() == 1)
s[x].push_back(v);
for(int u: G[v]){
if(u == par)
continue;
make(u, v, x);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
int u, v;
for(int i=1; i<n; i++){
cin >> u >> v;
G[u].push_back(v), G[v].push_back(u);
}
dfs(1, 0);
num = l[1];
v = find(1, 0);
int cnt = 0;
for(int u: G[v])
make(u, v, cnt++);
int res = (num + 1) >> 1;
vector <int> ans(2 * res);
int pt = 0;
for(int i=0; i<cnt; i++)
for(int vi: s[i]){
ans[pt] = vi;
pt += 2;
if(pt == (2 * res))
pt = 1;
}
if(ans.size() && ans.back() == 0)
ans.back() = s[0][0], put(1);
cout << res << '\n';
for(int i=0; i<ans.size(); i+=2)
cout << ans[i] << ' ' << ans[i + 1] << '\n';
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:94:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i=0; i<ans.size(); i+=2)
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |