Poj 3107 Godfather (tree dp)
Godfather
Time Limit:2000 MS |
|
Memory Limit:65536 K |
Total Submissions:5064 |
|
Accepted:1769 |
Description
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 areNPersons 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 containsN-The number of persons suspected to belong to mafia (2 ≤N≤ 50 000). Let them be numbered from 1N.
The followingN? 1 lines contain two integer numbers each. The pairAi,BiMeans that the gangsterAiHas communicated with the gangsterBi. 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
61 22 32 53 43 6
Sample Output
2 3
Perform a deep search to find the number of nodes where each node's sub-nodes constitute a sub-tree. Then, determine whether to update the minimum sub-tree or enter a new mafia leaders.
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define ll long # define N 51000 # define mem (a, t) memset (a, t, sizeof (a) vector
G [N]; int ans [N]; struct ege {int v, next;} e [N * 2]; int head [N]; int cnt, n, n1, mmin; int num [N]; // records the number of nodes in the I subtree void add (int u, int v) {e [cnt]. v = v; e [cnt]. next = head [u]; head [u] = cnt ++;} int dfs (int u, int fa) {int I, v, tmp = 0, t1; num [u] = 1; // calculate the number of subnodes of the current branch U and their own number and for (I = head [u]; I! =-1; I = e [I]. next) {v = e [I]. v; if (v! = Fa) {num [u] + = dfs (v, u); tmp = max (tmp, num [v]) ;}} t1 = max (tmp, n-num [u]); // after removing vertex u, the number of nodes in the tree if (t1