1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <cstdio> #include <cstring> #include <algorithm> #include <functional> using namespace std; const int N = 50001, M = 2 * N; int n, u, v, h[N], e[M], ne[M], idx; int results[N], k, ans = N; void add(int u, int v) { e[idx] = v, ne[idx] = h[u], h[u] = idx++; } int dfs(int u, int f) { int res = 0, sum = 1; for (int i = h[u]; ~i; i = ne[i]) { int v = e[i]; if (v == f) continue; int s = dfs(v, u); res = max(res, s); sum += s; } res = max(res, n - sum); if (res < ans) { ans = res; k = 0; results[k++] = u; } else if (res == ans) { results[k++] = u; } return sum; } int main() { scanf("%d", &n); memset(h, -1, (n + 1) * 4); for (int i = 1; i < n; ++i) { scanf("%d%d", &u, &v); add(u, v), add(v, u); } dfs(1, -1); sort(results, results + k, greater<int>()); while (k--) printf("%d ", results[k]); printf("\n"); return 0; }
|