| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 788670 | lovrot | 컴퓨터 네트워크 (BOI14_network) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
