树形DP POJ1655 树的重心

7 篇文章 0 订阅
3 篇文章 0 订阅

经典树形DP模板题,找树的重心

Balancing Act

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16297 Accepted: 6890

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

 

参考网上他人整理,树的重心有以下知识点:

定义1:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心
定义2:以这个点为根,那么所有的子树(不算整个树自身)的大小都不超过整个树大小的一半。
性质1:树中所有点到某个点的距离和中,到重心的距离和是最小的;如果有两个重心,那么他们的距离和一样。
性质2:把两个树通过一条边相连得到一个新的树,那么新的树的重心在连接原来两个树的重心的路径上。
性质3:把一个树添加或删除一个叶子,那么它的重心最多只移动一条边的距离。

(题外话:发现加粗利于阅读!)

嗯这道题的题意就是找树的重心emmm

首先,这个题中的树肯定是要存成无向图(vector实现很方便)。随便从一个点开始,即随便把这点当作根,dfs深度优先搜索向下搜索,s[i]记录i节点为根节点的子树上的节点个数(包括自己),dp[i]记录i节点为根节点的节点数最多的子树的节点数(此时假想树的该节点的父亲也是它的一棵子树,它就为该树根节点),父亲节点的子树通过n-s[i]来得到。深搜结束再遍历一遍dp找到最小的那个(第一个遇到的haihiahia)就是重心了。

 

//其实就是找树的重心
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn=20000;
const int inf=0x3f3f3f3f;

int n;
int dp[maxn+10],s[maxn+10];
vector <int> v[maxn+10];

void dfs(int u,int pre)
{
	int k=v[u].size();

	s[u]=1;
	for(int i=0;i<k;++i){
		int j=v[u][i];
		if(j==pre)
			continue;
		dfs(j,u);
		dp[u]=max(dp[u],s[j]);
		s[u]+=s[j];
	}
	dp[u]=max(dp[u],n-s[u]);
}

int main()
{
	int t;
	int a,b;
	int ans,node;

	scanf("%d",&t);
	while(t--){
		node=0;ans=inf;
		memset(s,0,sizeof(s));
		memset(dp,0,sizeof(dp));
		for(int i=0;i<=maxn;++i)
			v[i].clear();
		scanf("%d",&n);
		for(int i=0;i<n-1;++i){
			scanf("%d%d",&a,&b);
			v[a].push_back(b);
			v[b].push_back(a);
		}
		dfs(1,0);
		for(int i=1;i<=n;++i){
			if(dp[i]<ans){
				ans=dp[i];
				node=i;
			}
		}
		printf("%d %d\n",node,ans);
	}

	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值