1131: [POI2008]Sta
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1629 Solved: 611
[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
1 4
5 6
4 5
6 7
6 8
2 4
3 4
Sample Output
7
HINT
Source
/**************************************************************
Problem: 1131
User:
Language: C++
Result: Accepted
Time:6760 ms
Memory:44264 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 137;
inline void read( int &res )
{
char ch = getchar(); res = 0;
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9') res = res * 10 + ch - 48, ch = getchar();
}
int n, head[N], top;
struct Node
{
int y, nxt;
Node() { }
Node( int y, int nxt ) : y(y), nxt(nxt) { }
} e[N << 1];
inline void Adde( int x, int y )
{
e[++top] = Node(y, head[x]), head[x] = top;
e[++top] = Node(x, head[y]), head[y] = top;
}
int que[N], h, t, fa[N], siz[N], dep[N];
void Bfs()
{
que[++t] = 1;
fa[1] = 0;
dep[1] = 0;
while(h < t)
{
int u = que[++h];
siz[u] = 1;
for(int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].y;
if(v == fa[u]) continue;
fa[v] = u;
dep[v] = dep[u] + 1;
que[++t] = v;
}
}
for(int i = t; i >= 1; --i)
for(int j = head[que[i]]; j; j = e[j].nxt)
if(e[j].y != fa[que[i]]) siz[que[i]] += siz[e[j].y];
}
long long f[N], ans = 0, pos;
void Bfs1()
{
h = t = 0;
que[++t] = 1;
for(int i = 1; i <= n; ++i) f[1] += dep[i];
ans = f[1];
pos = 1;
while(h < t)
{
int u = que[++h];
for(int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].y;
if(v == fa[u]) continue;
f[v] = f[u] + (n - siz[v]) - siz[v];
if(f[v] > ans || (f[v] == ans && pos > v))
ans = f[v], pos = v;
que[++t] = v;
}
}
}
int main()
{
read(n);
for(int i = 1, u, v; i < n; ++i)
read(u), read(v), Adde(u, v);
Bfs();
Bfs1();
cout << pos << endl;
return 0;
}
/*
8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
*/