HDU 4863 Centroid of a Tree (树形DP)

HDU 4863

Centroid of a Tree

Centroid of a Tree

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 49 Accepted Submission(s): 15

Problem Description

Given a tree T with N vertices, your task is calculating the number of the connected sub-tree of T, which has the same centroid as T. In order to define the centroid, some integer value will be associated 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. A graph can have an arbitrary number of centroids. However,it can be proved that for trees, there are only two possibilities: 1. The tree has precisely one centroid. 2. The tree has precisely two centroids. In this case, the two centroids are adjacent. Note that in case 2, we just consider that T has two centroids, you should only count the sub-tree which has the two same centroids as T.

Input

The first line of the date is an integer T, which is the number of the text cases. Then T cases follow each case starts of a number N descript above. Then N-1 lines follow, each line contains two integers x and y, which means that there is a edge between x and y in tree T, you can assume that the index of T is from 1 to N. 1 <= T <= 50, 1 <= N <= 200,

Output

For each case, output the case number first, then output the number of the connected sub-tree which has the same centroid as T. Give your answer modulo 10007.

Sample Input

5 1 2 1 2 3 1 2 1 3 4 1 2 1 3 1 4 5 1 2 1 3 1 4 4 5

Sample Output

Case 1: 1 Case 2: 1 Case 3: 2 Case 4: 5 Case 5: 6

Author

FZU

Source

2014 Multi-University Training Contest 1

本题需要找出和原来的树有相同重心的子树。   首先树形DP来求出重心。   然后如果重心有一个。 以重心为根,树形DP一下,得出dp\[i\]\[j\]表示i的子树保留j个结点的种数。 然后可以先求反面。 就是根的一颗子树大小大于剩下的和。 暴力枚举最大子树,DP出其余子树取i个的方法数f\[i\]. 然后枚举最大子树取几个。 使用总数减掉这个就是答案。 如果两个重心,可以分成两颗树,这两颗树大小要一样。   好题啊。。。  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* ***************

Author :kuangbin

Created Time :2014/7/23 12:13:36

File Name :E:\2014ACM\比赛\2014多校训练\2014多校1\HDU4863.cpp

************************************************ */

#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <string>

#include <math.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

const int MAXN = 220;

struct Edge

{

int to,next;

}edge[MAXN*2];

int head[MAXN],tot;

void init()

{

tot = 0;

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

}

void addedge(int u,int v)

{

edge[tot].to = v;

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

head[u] = tot++;

}

int size[MAXN];

int mx[MAXN];

void dfs1(int u,int pre,int n)

{

size[u] = 1;

mx[u] = 0;

for(int i = head[u];i != -1;i = edge[i].next)

{

int v = edge[i].to;

if(v == pre)continue;

dfs1(v,u,n);

size[u] += size[v];

mx[u] = max(mx[u],size[v]);

}

mx[u] = max(mx[u],n-size[u]);

}

const int MOD = 10007;

int dp[MAXN][MAXN];//dp[i][j]是i子树取j个结点的方法数

void dfs_1(int u,int pre)

{

size[u] = 1;

dp[u][1] = 1;

for(int i = head[u];i != -1;i = edge[i].next)

{

int v = edge[i].to;

if(v == pre)continue;

dfs_1(v,u);

size[u] += size[v];

for(int j = size[u];j > 0;j--)

for(int k = 1;k < j && k <= size[v];k++)

{

dp[u][j] += dp[u][j-k]*dp[v][k]%MOD;

if(dp[u][j] >= MOD)dp[u][j] -= MOD;

}

}

}

int f[MAXN];

int solve1(int cen,int n)

{

memset(dp,0,sizeof(dp));

for(int i = 1;i <= n;i++)dp[i][0] = 1;

dfs_1(cen,cen);

int ans = 0;

for(int i = head[cen];i != -1;i = edge[i].next)

{

int u = edge[i].to;

memset(f,0,sizeof(f));

f[0] = 1;

for(int j = head[cen];j != -1;j = edge[j].next)

if(j != i)

{

int v = edge[j].to;

for(int y = n;y > 0;y--)

for(int x = 1;x <= size[v] && x <= y;x++)

{

f[y] += f[y-x]*dp[v][x]%MOD;

if(f[y] >= MOD)f[y] -= MOD;

}

}

int tmp = f[0]%MOD;

for(int j = 1;j <= size[u];j++)

{

ans += tmp*dp[u][j]%MOD;

ans %= MOD;

tmp += f[j];

tmp %= MOD;

}

}

int tot = 1;

for(int i = head[cen];i != -1;i = edge[i].next)

{

int v = edge[i].to;

int tmp = 0;

for(int j = 0;j <= size[v];j++)

{

tmp = (tmp+dp[v][j])%MOD;

}

tot = tot*tmp%MOD;

}

ans = tot-ans;

ans = (ans%MOD+MOD)%MOD;

return ans;

}

void dfs_2(int u,int pre,int no)

{

size[u] = 1;

dp[u][1] = 1;

for(int i = head[u];i != -1;i = edge[i].next)

{

int v = edge[i].to;

if(v == pre || v == no)continue;

dfs_2(v,u,no);

size[u] += size[v];

for(int j = size[u];j > 0;j--)

for(int k = 1;k < j && k <= size[v];k++)

{

dp[u][j] += dp[u][j-k]*dp[v][k]%MOD;

if(dp[u][j] >= MOD)dp[u][j] -= MOD;

}

}

}

int solve2(int cen1,int cen2,int n)

{

memset(dp,0,sizeof(dp));

for(int i = 1;i <= n;i++)dp[i][0] = 1;

dfs_2(cen1,cen1,cen2);

dfs_2(cen2,cen2,cen1);

int ans = 0;

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

{

ans += dp[cen1][i]*dp[cen2][i]%MOD;

ans %= MOD;

}

return ans;

}

int main()

{

//freopen("1003.in","r",stdin);

//freopen("out.txt","w",stdout);

int T;

int iCase = 0;

int n;

scanf("%d",&T);

while(T--)

{

iCase++;

scanf("%d",&n);

init();

int u,v;

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

{

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

addedge(u,v);

addedge(v,u);

}

dfs1(1,1,n);

int cen1,cen2;

int cnt = 0;

int MM = n;

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

{

if(mx[i] < MM)

{

MM = mx[i];

cen1 = i;

cnt = 1;

}

else if(mx[i] == MM)

{

cen2 = i;

cnt = 2;

}

}

if(cnt == 1)printf("Case %d: %d\n",iCase,solve1(cen1,n));

else printf("Case %d: %d\n",iCase,solve2(cen1,cen2,n));

}

return 0;

}
------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 288.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%