[bzoj1131][POI2008]Sta

13 篇文章 0 订阅

1131: [POI2008]Sta

Time Limit: 10 Sec   Memory Limit: 162 MB
[ Submit][ Status][ Discuss]

Description

给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大

Input

给出一个数字N,代表有N个点.N<=1000000 下面N-1条边.

Output

输出你所找到的点,如果具有多个解,请输出编号最小的那个.

Sample Input

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

Sample Output

7

HINT

sum表示子树,anti表示父亲作为子树的值
#include<bits/stdc++.h>
using namespace std;
const int N = 1000000 + 5;
long long siz[N],anti[N],sum[N],ans; int ans2,last[N*2],cnt,n;
struct Edge{ int to,next; }e[N*2];
void insert( int u, int v ){
	e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;
	e[++cnt].to = u; e[cnt].next = last[v]; last[v] = cnt;
}
void dfs( int x, int f ){
	siz[x] = 1;
	for( int i = last[x]; i; i = e[i].next )
		if( e[i].to ^ f ){
			dfs( e[i].to, x );
			siz[x] += siz[e[i].to];
			sum[x] += (sum[e[i].to]+siz[e[i].to]);
		}
}
void dfs2( int x, int f ){
	for( int i = last[x]; i; i = e[i].next )
		if( e[i].to ^ f ){
			anti[e[i].to] = anti[x] + sum[x] - sum[e[i].to] - siz[e[i].to] + n - siz[e[i].to];
			dfs2( e[i].to, x );
		}
	if( anti[x] + sum[x] >= ans ){
		if( anti[x] + sum[x] > ans ) ans = anti[x] + sum[x], ans2 = x;
		else ans2 = min( ans2, x );
	}
}
int main(){
	scanf("%d", &n);
	for( int i = 1,u,v,w; i < n; i++ ){
		scanf("%d%d", &u, &v);
		insert( u, v );
	}
	dfs( 1, 0 ); dfs2( 1, 0 );
	printf("%d\n", ans2);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值