SGU 134 Centroid 找树的重心 水题

134. Centroid

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

 

You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree. 
In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.

 

Input

The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning that there exists an edge between vertex a and vertex b.

 

Output

You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.

 

Sample Input

7

1 2

2 3

2 4

1 5

5 6

6 7

Sample Output

3 1

1


Author : Mugurel Ionut Andreica
Resource : SSU::Online Contester Fall Contest #2
Date : Fall 2002

 

 

 

 

题意:

给出一棵树,找出树的所有重心,并且按升序输出。

 

dfs一遍即可。

 

不过还是PE了一次,WA了一次。

PE:SGU是单样例输出,和CF一样。

WA:一个<被我打成了 <<

 

 

 

SGU 134 Centroid 找树的重心 水题
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 

 5 using namespace std;

 6 

 7 const int maxn=16000+5;

 8 const int inf=0x3f3f3f3f;

 9 

10 struct Edge

11 {

12     int to,next;

13 }edge[maxn<<1];

14 

15 int head[maxn];

16 int tot;

17 int son[maxn];

18 int siz[maxn];

19 int ans[maxn];

20 int sum[maxn];

21 

22 void init()

23 {

24     memset(head,-1,sizeof(head));

25     tot=1;

26     memset(son,-1,sizeof(son));

27 }

28 

29 void addedge(int u,int v)

30 {

31     edge[tot].to=v;

32     edge[tot].next=head[u];

33     head[u]=tot++;

34 }

35 

36 void dfs(int u,int fa)

37 {

38     siz[u]=1;

39     for(int i=head[u];~i;i=edge[i].next)

40     {

41         int v=edge[i].to;

42         if(v==fa)

43             continue;

44         dfs(v,u);

45         siz[u]+=siz[v];

46         if(son[u]==-1||siz[son[u]]<siz[v])

47             son[u]=v;

48     }

49 }

50 

51 int solve(int n)

52 {

53     dfs(1,-1);

54 

55     for(int i=1;i<=n;i++)

56     {

57         sum[i]=max(siz[son[i]],n-siz[i]);

58     }

59 

60     int minsum=inf;

61     for(int i=1;i<=n;i++)

62         if(sum[i]<minsum)

63             minsum=sum[i];

64     int ret=0;

65     for(int i=1;i<=n;i++)

66     {

67         if(sum[i]==minsum)

68             ans[ret++]=i;

69     }

70     return ret;

71 }

72 

73 int main()

74 {

75     int n;

76     //while(scanf("%d",&n)!=EOF)

77     //{

78         scanf("%d",&n);

79         init();

80         int u,v;

81         for(int i=1;i<n;i++)

82         {

83             scanf("%d%d",&u,&v);

84             addedge(u,v);

85             addedge(v,u);

86         }

87 

88         int ret=solve(n);

89 

90         printf("%d %d\n",sum[ans[0]],ret);

91         for(int i=0;i<ret-1;i++)

92             printf("%d ",ans[i]);

93         printf("%d\n",ans[ret-1]);

94     //}

95 

96     return 0;

97 }
View Code

 

你可能感兴趣的:(水题)