# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
788670 | lovrot | Computer Network (BOI14_network) | C++17 | 0 ms | 0 KiB |
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 <cstdio>
#include <vector>
#define EB emplace_back
using namespace std;
const int N = 5e5 + 10;
int n;
vector<int> G[N];
vector<int> F[N];
void dfs(int u, int p) {
if((int) G[u].size() == 1) {
F[u].EB(u);
return;
}
int xam = 0, w;
for(int v : G[u]) {
if(v == p) continue;
dfs(v, u);
if((int) F[v].size() > xam) {
xam = F[v].size();
w = v;
}
}
int lst = F[w].back();
F[w].pop_back();
for(int v : G[u]) {
if(v == p || v == w) continue;
for(int i = 0; i < (int) F[v].size() - 1; ++i) F[w].EB(F[v][i]);
}
F[w].EB(lst);
for(int v : G[u]) {
if(v == p || v == w) continue;
F[w].EB(F[v].back());
}
swap(F[u], F[w]);
/*printf("%d : [", u);
for(int v : F[u]) printf("%d, ", v);
printf("]\n");*/
}
int main() {
scanf("%d", &n);
int root;
for(int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u].EB(v);
G[v].EB(u);
if(G[u].size() > 1) root = u;
if(G[v].size() > 1) root = v;
}
//printf("root %d\n", root);
dfs(root, 0);
printf("%d\n", (int) (F[root].size() + 1) / 2);
for(int i = 0; i < F[root].size(); i += 2) {
if(i == F[root].size() - 1) --i;
printf("%d %d\n", F[root][i], F[root][i + 1]);
}
return 0;
}