首页 > 代码库 > poj3107 Godfather 求树的重心

poj3107 Godfather 求树的重心

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 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 aibi 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

Source

Northeastern Europe 2005, Northern Subregion
 
 
题目大意:给定一颗树,求树的所有重心,按顺序输出。
这是模板题,是点分治的基础。求出所有的siz(随便dfs/bfs),就能求出每个点去掉后的剩下的最大块的大小。
 1 program rrr(input,output);
 2 type
 3   etype=record
 4      t,next:longint;
 5   end;
 6 var
 7   e:array[0..100010]of etype;
 8   a,q,father,siz,f,ans:array[0..50050]of longint;
 9   v:array[0..50050]of boolean;
10   n,i,j,x,y,h,t,cnt,min:longint;
11 function max(a,b:longint):longint;
12 begin
13    if a>b then exit(a) else exit(b);
14 end;
15 procedure add(x,y:longint);
16 begin
17    inc(cnt);e[cnt].t:=y;e[cnt].next:=a[x];a[x]:=cnt;
18 end;
19 begin
20    assign(input,r.in);assign(output,r.out);reset(input);rewrite(output);
21    readln(n);
22    cnt:=0;
23    for i:=1 to n-1 do begin readln(x,y);add(x,y);add(y,x); end;
24    fillchar(v,sizeof(v),false);
25    h:=0;t:=1;q[1]:=1;v[1]:=true;
26    while h<t do
27       begin
28          inc(h);
29          i:=a[q[h]];
30          while i<>0 do
31             begin
32                if not v[e[i].t] then
33                   begin
34                      v[e[i].t]:=true;father[e[i].t]:=q[h];
35                      inc(t);q[t]:=e[i].t;
36                   end;
37                i:=e[i].next;
38             end;
39       end;
40    fillchar(f,sizeof(f),0);
41    for i:=1 to n do siz[i]:=1;
42    min:=n;
43    for i:=n downto 2 do
44       begin
45          t:=max(f[q[i]],n-siz[q[i]]);
46          if t=min then begin inc(j);ans[j]:=q[i]; end
47          else if t<min then begin j:=1;ans[1]:=q[i];min:=t; end;
48          inc(siz[father[q[i]]],siz[q[i]]);
49          if siz[q[i]]>f[father[q[i]]] then f[father[q[i]]]:=siz[q[i]];
50       end;
51    if f[1]<min then begin j:=1;ans[1]:=1; end
52    else if f[1]=min then begin inc(j);ans[j]:=1; end;
53    fillchar(v,sizeof(v),false);
54    for i:=1 to j do v[ans[i]]:=true;
55    for i:=1 to n do if v[i] then write(i, );
56    close(input);close(output);
57 end.

 

poj3107 Godfather 求树的重心