树的重心

2024-08-15

求解树的重心

#include <bits/stdc++.h>
#define io cin.tie(0), cout.tie(0), ios::sync_with_stdio(false)
#define LL long long
#define ULL unsigned long long
#define EPS 1e-8
#define INF 0x7fffffff
#define SUB -INF - 1
using namespace std;
const int N = 50005;
struct edge
{
    int to, next;
} edge[N << 1];
int head[N], cnt = 0;
void init()
{
    for (int i = 0; i < N; i++)
    {
        edge[i].next = -1;
        head[i] = -1;
    }
    cnt = 0;
}
void addedge(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
int n;
int d[N], ans[N], num = 0, maxnum = INF;
void dfs(int u, int fa)
{
    d[u] = 1;
    int tmp = 0;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa)
            continue;
        dfs(v, u);
        d[u] += d[v];
        tmp = max(tmp, d[v]);
    }
    tmp = max(tmp, n - d[u]);
    if (tmp < maxnum)
    {
        maxnum = tmp;
        num = 0;
        ans[++num] = u;
    }
    else if (tmp == maxnum)
        ans[++num] = u;
}
int main()
{
    scanf("%d", &n);
    init();
    for (int i = 1; i < n; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        addedge(u, v);
        addedge(v, u);
    }
    dfs(1, 0);
    sort(ans + 1, ans + 1 + num);
    for (int i = 1; i <= num; i++)
        printf("%d ", ans[i]);
    return 0;
}