POJ - 1655 Balancing Act(树的重心)

Time limit:1000 ms;Memory limit:65536 kB

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

题意:n个结点的一棵树,去掉某个结点x后,会变成一个森林。这个森林中的每棵树都有自己的结点数,其中最大结点数设为ans,问删除哪个结点x后,ans最小?并写出x,ans。

分析:其实就是求树的重心(定义:对于一棵n个结点的无根树,找到一个点,使得把树变成以该点为根的有根树时,最大子树的结点数最小。换句话说,删除这个点后最大连通块(一定是树)的结点数最小。)


解题步骤如下:

①题目给的是无向图,所以用邻接表储存时记得双向连边。

②用一次DFS找到重心,并求出其最大连通块的结点数。

注意事项:任取一点为根时,一定要标记走过的点(记忆化处理,不走回头路)


AC代码如下(157ms):

/*
 * dp[i]表示以i为根的子树的结点个数
 * dp[i] = Σdp[j] + 1; j是i的son
 * 结点i的最大连通块结点个数为max(dp[i],N-dp[i])
 */
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define fast                                ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll                                  long long
#define _for(i,a,b)                         for(int i = a;i < b;i++)
#define rep(i,a,b)                          for(int i = a;i <= b;i++)

const int maxn = 20010;
int kase, N, ans[maxn];
vector<int>G[maxn];//邻接表
int dp[maxn];
bool flag[maxn];//记忆化处理。

void dfs(int r)
{
	int sum, k;
	sum = k = 0;
	flag[r] = true;//标记经历过此点(此处一定要标记,否则死循环)
	int nson = G[r].size();//此点儿子的个数
	_for(i, 0, nson)
	{
		int son = G[r][i];
		if (!flag[son])
		{
			dfs(son);
			sum += dp[son];
			k = max(k, dp[son]);
		}
		else continue;
	}
	dp[r] = sum + 1;//以r为根的结点总个数
	ans[r] = max(k, N - dp[r]);//以r为根的最大连通块的结点个数
}

int main()
{
	scanf("%d", &kase);
	while (kase--)
	{
		scanf("%d", &N);
		rep(i, 1, N)G[i].clear();//清空邻接表
		memset(dp, 0, sizeof(dp));
		memset(flag, false, sizeof(flag));
		int x, y;
		_for(i, 0, N - 1)
		{
			scanf("%d%d", &x, &y);
			G[x].push_back(y);
			G[y].push_back(x);
		}
		int dot = 0;
		int cnt = maxn;
		dfs(1);
		rep(i, 1, N)
		{
			if (ans[i]<cnt)
				dot = i, cnt = ans[i];
		}

		printf("%d %d\n", dot, cnt);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值