【模板】樹的重心(POJ1665)

 題意:找重心和最大子樹的節點數。

#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int N; // 1<= N <= 20000
const int maxn = 20000;
vector<int> tree[maxn + 5]; // tree[i]表示節點i的相鄰節點
int d[maxn + 5]; // d[i]表示以i爲根的子樹的節點個數

#define INF 10000000

int minNode;
int minBalance;

void dfs(int node, int parent) // node and its parent
{
    d[node] = 1; // d數組記錄的是它所有的子節點數目
    int maxSubTree = 0; // subtree that has the most number of nodes
    for (int i = 0; i < tree[node].size(); i++)
    {
        int son = tree[node][i];
        if (son != parent)
        {
            dfs(son, node);
            d[node] += d[son];
            maxSubTree = max(maxSubTree, d[son]);//這裏就比較神奇了,這一步和循環外的處理可以直接把記錄分支中最大的子節點數的數組省略了
        }
    }
    maxSubTree = max(maxSubTree, N - d[node]); // "upside substree with (N - d[node]) nodes"

    if (maxSubTree < minBalance)
    {
        minBalance = maxSubTree;
        minNode = node;
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &N);
        for (int i = 1; i <= N - 1; i++)
        {
            tree[i].clear();
        }
        for (int i = 1; i <= N-1; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            tree[u].push_back(v);
            tree[v].push_back(u);
        }
        minNode = 0;
        minBalance = INF;
        dfs(1, 0); // fist node as root
        printf("%d %d\n", minNode, minBalance);
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章