This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef pair<int, int> ii;
const int N = 5e5 + 5;
int n, child[N], nLeaf;
vector<int> gr[N], vec;
void prep (int u, int p) {
for (int v : gr[u]) if (v != p) {
prep(v, u);
child[u] += child[v];
}
child[u] += (gr[u].size() == 1);
}
int findCentroid (int u, int p) {
for (int v : gr[u]) if (v != p) {
if (child[v] >= (nLeaf + 1) / 2 ) return findCentroid(v, u);
}
return u;
}
void dfs (int u, int p) {
for (int v : gr[u]) if (v != p) dfs(v, u);
if (gr[u].size() == 1) vec.pb(u);
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int _ = 1; _ < n; ++_) {
int u, v; cin >> u >> v;
gr[u].pb(v); gr[v].pb(u);
}
for (int u = 1; u <= n; ++u) nLeaf += (gr[u].size() == 1);
cout << (nLeaf + 1) / 2 << '\n';
prep(1, 1);
int cen = findCentroid(1, 1);
for (int u : gr[cen]) dfs(u, cen);
for (int i = 0; i < vec.size() - (nLeaf + 1) / 2; ++i) {
cout << vec[i] << ' ' << vec[ i + (nLeaf + 1) / 2 ] << '\n';
}
if (nLeaf % 2) cout << vec[0] << ' ' << vec[nLeaf / 2];
return 0;
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:49:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size() - (nLeaf + 1) / 2; ++i) {
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |