Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.
Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.
Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.
Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.
Input
The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.
The following n − 1 lines contain two integer numbers each. The pair ai, bi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.
Output
Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.
Sample Input
6 1 2 2 3 2 5 3 4 3 6
Sample Output
2 3
思路:
求树的重心模板题。
六个人,组成一个有上下级的树形网络(上下级关系未知)。头目的位置是:如果删除头目,那么剩下的最大的子树要尽可能小。求这样的所有可能头目。即求树的重心。
对于样例的解释:
首先构造一个无根树,如图:
然后随便假定一个节点为根,这里假定节点1为根,然后无根树会变成有根树:
然后,我们找出每个节点的最大子树的节点数,(红色的是该节点最大子树的节点数)(向父亲方向的也算作考虑在内的一颗子树)
然后,我们找出这些红色数字中最小的那些对应节点,如图:
所以,2和3就是这棵树的重心。
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#define MAXN 50005
using namespace std;
int sz[MAXN], father[MAXN], n, MIN = 0x3f3f3f3f;
int to[MAXN << 1], Next[MAXN << 1], head[MAXN], tot;
vector<int> ans;
void add_edge(int u, int v) {
to[tot] = v;
Next[tot] = head[u];
head[u] = tot++;
}
void dfs(int node, int pre) {
sz[node] = 1;
int MAX = 0;
for (int i = head[node]; ~i; i = Next[i]) {
int v = to[i];
if (v != pre) {
dfs(v, node);
sz[node] += sz[v];
MAX = max(MAX, sz[v]);
}
}
MAX = max(MAX, n - sz[node]);
if (MAX < MIN) {
ans.clear();
MIN = MAX;
ans.push_back(node);
}
else if (MAX == MIN) {
ans.push_back(node);
}
}
int main()
{
tot = 0;
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i <= n-1; i++) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
dfs(1, 0);
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
printf("%d", ans[i]);
if(i != ans.size()){
printf(" ");
}
}
return 0;
}