POJ-1655_Balancing Act(树的重心)

→→This is the question←←                 [Submit]

Balancing Act

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18971 Accepted: 8016

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

 又是一次苦逼的测试,对于这次测试我只能说见识短浅就只能挨打啊,旁边的大佬噼里啪啦的就把这道题给AC了,而我却没看懂这是个什么玩意,终于测试完有人告诉我这是一个树的重心题,emm,树的重心是啥???我都没听说过,这可怎么求???

咱先来看一下树的重心是个啥子玩意  :

树的重心也叫树的质心。对于一棵树n个节点的无根树,找到一个点,使得把树变成以该点为根的有根树时,最大子树的结点数最小。换句话说,删除这个 [1]  点后最大连通块(一定是树)的结点数最小

其实说这道题裸那是一点 都不过分,题目就是让你去求重心嘛,输出的那个节点很明显就是树的重心啊,只不过是还需要输出一下这个重心去掉后的最大子树的最小节点数。。。有点绕人 哈哈哈

那么这道题应该怎么去求呢?

我们在这里开两个数组先,

dp[i]表示第i个节点往子节点方向  最大节点数的最大值;

num[i]表示第i个节点除父亲节点方向外,其他方向的  所有子节点数,要包括他自身

代码依旧是dfs,这里的状态转移方程就比较有意思了

dp[x]=max(dp[x],num[t]);//寻找删掉点后被分成的小树的最大节点数

num[x]+=num[t];//初始值为1,即他的所有子节点个数 

 

循环枚举之外还有一句就是 dp[x]=max(dp[x],n-num[x]);即求出这个点为根的子树以外和原dp比较 进一步刷新最大值

下面是AC代码,顺便送你一个小滑稽  嘻嘻嘻

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=2e4+5;

int n,t,cnt=0;
int dp[mm];//dp[i]表示i节点往子节点方向最大节点数的最大值 
int num[mm];//num[i]表示i除父节点方向外的所有子节点数 包括本身 
struct node{
	int to;
	int next;
}pp[mm<<1]; 
int head[mm];
void add(int x,int y){
	pp[cnt].to=y;
	pp[cnt].next=head[x];
	head[x]=cnt++;
}

void dfs(int x,int pre){
	dp[x]=0;
	num[x]=1;
	for(int i=head[x];~i;i=pp[i].next){
		int t=pp[i].to;
		if(t==pre)continue;
		dfs(t,x);
		dp[x]=max(dp[x],num[t]);//寻找删掉点后被分成的小树的最大节点数
		num[x]+=num[t];//初始值为1,即他的所有子节点个数 
	}
	dp[x]=max(dp[x],n-num[x]);//剩下的节点个数 
}

int main()
{
	scanf("%d",&t);
	while(t--){
		cnt=0;
		mem(head,-1); 
		scanf("%d",&n);
		int x,y;
		for(int i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			add(x,y);
			add(y,x);
		}
		dfs(1,-1);
		int res=1,ans=dp[1];
		for(int i=1;i<=n;i++){//寻找删点后连通分量最大点数的最小值 
			if(dp[i]<ans){
				res=i;
				ans=dp[i];
			}
		}
		printf("%d %d\n",res,ans);
	} 
	return 0;
}

/*                                                                  
 *            .,,       .,:;;iiiiiiiii;;:,,.     .,,                   
 *          rGB##HS,.;iirrrrriiiiiiiiiirrrrri;,s&##MAS,                
 *         r5s;:r3AH5iiiii;;;;;;;;;;;;;;;;iiirXHGSsiih1,               
 *            .;i;;s91;;;;;;::::::::::::;;;;iS5;;;ii:                  
 *          :rsriii;;r::::::::::::::::::::::;;,;;iiirsi,               
 *       .,iri;;::::;;;;;;::,,,,,,,,,,,,,..,,;;;;;;;;iiri,,.           
 *    ,9BM&,WA了活该       .,:;;:,,,,,,,,,,,hXA8:   T了天命..,,,.       
 *   ,;&@@#r:;;;;;::::,,.   ,r,,,,,,,,,,iA@@@s,,:::;;;::,,.   .;.      
 *    :ih1iii;;;;;::::;;;;;;;:,,,,,,,,,,;i55r;;;;;;;;;iiirrrr,..       
 *   .ir;;iiiiiiiiii;;;;::::::,,,,,,,:::::,,:;;;iiiiiiiiiiiiri         
 *   iriiiiiiiiiiiiiiii;;;::::::::::::::::;;;iiiiiiiiiiiiiiiir;        
 *  ,riii;;;;;;;;;;;;;:::::::::::::::::::::::;;;;;;;;;;;;;;iiir.       
 *  iri;;;::::,,,,,,,,,,:::::::::::::::::::::::::,::,,::::;;iir:       
 * .rii;;::::,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,::::;;iri       
 * ,rii;;;::,,,,,,,,,,,,,:::::::::::,:::::,,,,,,,,,,,,,:::;;;iir.      
 * ,rii;;i::,,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,,::i;;iir.      
 * ,rii;;r::,,,,,,,,,,,,,:,:::::,:,:::::::,,,,,,,,,,,,,::;r;;iir.      
 * .rii;;rr,:,,,,,,,,,,,,,,:::::::::::::::,,,,,,,,,,,,,:,si;;iri       
 *  ;rii;:1i,,,,,,,,,,,,,,,,,,:::::::::,,,,,,,,,,,,,,,:,ss:;iir:       
 *  .rii;;;5r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,sh:;;iri        
 *   ;rii;:;51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.:hh:;;iir,        
 *    irii;::hSr,.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,sSs:;;iir:         
 *     irii;;:iSSs:.,,,,,,,,,,,,,,,,,,,,,,,,,,,..:135;:;;iir:          
 *      ;rii;;:,r535r:...,,,,,,,,,,,,,,,,,,..,;sS35i,;;iirr:           
 *       :rrii;;:,;1S3Shs;:,............,:is533Ss:,;;;iiri,            
 *        .;rrii;;;:,;rhS393S55hh11hh5S3393Shr:,:;;;iirr:              
 *          .;rriii;;;::,:;is1h555555h1si;:,::;;;iirri:.               
 *            .:irrrii;;;;;:::,,,,,,,,:::;;;;iiirrr;,                  
 *               .:irrrriiiiii;;;;;;;;iiiiiirrrr;,.                    
 *                  .,:;iirrrrrrrrrrrrrrrrri;:.                        
 *                        ..,:::;;;;:::,,.                             
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值