bzoj1131 Sta
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
1 4
5 6
4 5
6 7
6 8
2 4
3 4
Sample Output
7
简单的树形dp
现在智障错误越来越多,忘开long long气死人
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e6+10;
int n;
int aa;char cc;
int read() {
aa=0;cc=getchar();
while(cc<'0'||cc>'9') cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
return aa;
}
int fir[maxn],nxt[2*maxn],to[2*maxn],e=0;
void add(int x,int y) {
to[++e]=y;nxt[e]=fir[x];fir[x]=e;
to[++e]=x;nxt[e]=fir[y];fir[y]=e;
}
long long fa[maxn],size[maxn],ans[maxn];//
void dfs1(int pos,long long d) {
int z;size[pos]=1;ans[1]+=d;
for(int y=fir[pos];y;y=nxt[y]) {
z=to[y];
if(z==fa[pos]) continue;
fa[z]=pos; dfs1(z,d+1);
size[pos]+=size[z];
}
}
void dfs2(int pos) {
if(pos!=1) ans[pos]=ans[fa[pos]]+n-2*size[pos];
int z;
for(int y=fir[pos];y;y=nxt[y]) {
z=to[y];
if(z==fa[pos]) continue;
dfs2(z);
}
}
int main() {
n=read();int x,y;
for(int i=1;i<n;++i) {
x=read();y=read();
add(x,y);
}
dfs1(1,1);
dfs2(1); int pos=1;
for(int i=2;i<=n;++i) if(ans[i]>ans[pos]) pos=i;
printf("%d",pos);
return 0;
}
弱者就是会被欺负呀

浙公网安备 33010602011771号