ACdream 1015 A - Double Kings

A - Double Kings

Time Limit:  2000/1000MS (Java/Others)     Memory Limit:  128000/64000KB (Java/Others)
Problem Description
Our country is tree-like structure, that is to say that N cities is connected by exactly N - 1 roads.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king. 
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
Input
There are multiple test cases.
The first line contains an integer  N (1 ≤ N ≤ 50000).
The next  N - 1 lines each line contains two integers  a and b indicating there is a road between city  a and city  b.  (1 ≤ a, b ≤ N)
Output
For each test case, output an integer indicating the number of cities who will choose elder son as their king.
Sample Input
4 
1 2
2 3
3 4

4
1 2
1 3
1 4
Sample Output
2
3
题意: 给你n个节点的树,表示n个城市 有n-1 条边 ,国王有2个儿子 大儿子先选择城市作为首都 , 二儿子后选择首都。
对于每一个城市 如果离大儿子首都近 就属于大儿子 如果离二儿子首都近 ,就属于二儿子,如果一样近 ,就属于大儿子。
问你 最后属于大儿子的 城市有几个。(两个儿子都尽量使得属于自己的城市尽量的多。)
 
  
思路:  其实就是求这个数的 重心。重心的定义: 的重心也叫的质心。找到一个点,其所有的子树中最大的子树节点数最少
,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡。   
对于二儿子 因为大儿子足够聪明  ,所以二儿子的首都 一定是属于大儿子的子树 ,并且大儿子为了使得二儿子的城市尽量的少,
那么他会选择一个节点 这个节点的所有子树的最大的子树节点数最小  。 所以这个节点就是树的重心。。

代码: 

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<algorithm>
#define N 50005
#define inf 0x3f3f3f3f

using namespace std;

vector< int >edge[N];
int n,root,vis[N],er[N],dian[N];

void dfs(int u)
{
	er[u]=1;
	vis[u]=1;
	dian[u]=0;
	
	for(int i=0;i<edge[u].size();i++)
	{
		int v=edge[u][i];
		if(vis[v]) continue;
		dfs(v);
		er[u]+=er[v];
		
		dian[u]=max(dian[u],er[v]);
		
	}
	dian[u]=max(dian[u],n-er[u]);
	
	if(dian[u]<dian[root]) root=u;
	else if(dian[u]==dian[root]) root= min(u,root);
	
	return ;
}

int main()
{
	int u,v;
	
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++) edge[i].clear();
		memset(vis,0,sizeof(vis));
		memset(dian,0,sizeof(dian));
		memset(er,0,sizeof(er));
	for(int i=1;i<n;i++)
	{
		scanf("%d %d",&u,&v);
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	
	root=0;
	dian[root]=inf;
	
	dfs(1);
	
	/*
	for(int i=1;i<=n;i++) printf("%d ",er[i]);
	printf("\n");
	for(int i=1;i<=n;i++) printf("%d ",dian[i]);
	printf("\n");
	
	printf("%d\n",root);*/
	
	printf("%d\n",n-dian[root]);
	
	}
	
	
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值