poj 1655 Balancing Act(树dp)

本文介绍了一种计算树中节点最小平衡数的方法。通过深度优先搜索算法,计算每个节点删除后形成森林中最大树的节点数,进而找出最小平衡数及对应的节点编号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Balancing Act
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7986 Accepted: 3290

Description

Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T.
For example, consider the tree:

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.

For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.

Input

The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.

Output

For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.

Sample Input

1
7
2 6
1 2
1 4
4 5
3 7
3 1

Sample Output

1 2


题意:给出一棵树,每个结点都有一个平衡值,一个结点的平衡值是去掉这个结点后形成的森林中最多结点的那棵树的结点数。现在要求出哪个结点的平衡值最小,最小的平衡值为多少。
思路:设以结点i为根的子树的结点数为cnt[i],进行dfs求出每个cnt[i]以及以i为根的树中各个子树的结点数(设最多的结点数为tmp),之后每个结点的平衡值就是max(tmp,n-cnt[i])

AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <map>
#include <algorithm>
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const int maxn = 20005;

vector<int>G[maxn];
int n, cnt[maxn], balance[maxn];
void dfs(int u, int pre){
    cnt[u] = 1;
    int tmp = 0;
    for(int i = 0; i < (int)G[u].size(); i++)
    {
        int v = G[u][i];
        if(v == pre) continue;
        dfs(v, u);
        cnt[u] += cnt[v];
        tmp = max(tmp, cnt[v]);
    }
    balance[u] = max(tmp, n - cnt[u]);
}
int main()
{
    int t, a, b;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i <= n; i++) G[i].clear();
        for(int i = 0; i < n - 1; i++)
        {
            scanf("%d%d", &a, &b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        memset(cnt, 0, sizeof(cnt));
        dfs(1, -1);
        int x = 1;
        for(int i = 1; i <= n; i++)
        if(balance[i] < balance[x]) x = i;
        printf("%d %d\n", x, balance[x]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值