[原创]华中农业大学第五届程序设计大赛 C Friends [树形dp]【动态规划】

2017-05-26 21:17:40 Tabris_ 阅读数:413


博客爬取于2020-06-14 22:40:36
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/72773662


题目链接:http://acm.hzau.edu.cn/problem.php?id=1201
————————————————————————————————————————

1201: Friends
Time Limit: 1 Sec Memory Limit: 1280 MB
Submit: 151 Solved: 40
[Submit][Status][Web Board]
Description
In a country, the relationship between people can be indicated by a tree. If two people are acquainted with each other, there will be an edge between them. If a person can get in touch with another through no more than five people, we should consider these two people can become friends. Now, give you a tree of N people’s relationship. ( 1 <= N <= 1e5), you should compute the number of who can become friends of each people?

Input
In the first line, there is an integer T, indicates the number of the cases.
For each case, there is an integer N indicates the number of people in the first line.

In the next N-1 lines, there are two integers u and v, indicate the people u and the people

v are acquainted with each other directly. People labels from 1.

Output
For each case, the first line is “Case #k :”, k indicates the case number.
In the next N lines, there is an integer in each line, indicates the number of people who can become the ith person’s friends. i is from 1 to n.

Sample Input
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8

Sample Output
Case #1:
6
7
7
7
7
7
7
6

——————————————————————————————————————————

题目大意:
就是有n个人,他们的朋友关系是一个树,现在通过至多5个人能联系到的也会成为朋友。问你现在的情况下,每个人会有多少个朋友。

解题思路:

很明显的树形dp

设dp[i][j] 为第i个节点的子树中距离i为j的节点个数.
设ans[i][j] 为整棵树中距离i为j的节点个数.

dp可以通过一次dfs计算出来,

ans稍麻烦些,

对于一个节点,

ans[][i]=dp[][i]+ans[][i1]dp[][i2];ans[][i] = dp[][i] + ans[][i-1] - dp[][i-2];

转移很好理解,画棵树理解下就好了.
注意的是根节点要特殊处理一下

附本题代码
———————————————————————————————————————————

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
# include <bits/stdc++.h>
typedef long long int LL;
using namespace std;

const int N = 2e6+7;
//const int INF = (~(1<<31));

int read(){
int x=0,f=1;char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch = getchar();}
return x;
}
/*******************************************/

vector<int >G[N];

int dp[N][10]; //dp[i][j] 第i个节点 距离为j的点数
int ans[N][10];
int n;

void dfs(int u,int f){
for(int i=0;i<=6;i++)dp[u][i]=(0==i);
int gz = G[u].size();
for(int i=0,to;i<gz;i++){
to = G[u][i];
if(to == f) continue;
dfs(to,u);
for(int j=1;j<=6;j++)
dp[u][j]+=dp[to][j-1];
}
}

void dfs2(int u,int f){
ans[u][0]=1;
ans[u][1]=dp[u][1]+(u!=1);
for(int j=2;j<=6;j++)
ans[u][j]=dp[u][j]+(u!=1)*(ans[f][j-1]-dp[u][j-2]);

int gz = G[u].size();
for(int i=0,to;i<gz;i++){
to = G[u][i];
if(to == f) continue;
dfs2(to,u);
}
}

int main(){
int _,kcase = 0;
scanf("%d",&_);
while(_--){
scanf("%d",&n);
for(int i=1,u,v;i<n;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}

dfs(1,0);
dfs2(1,0);

printf("Case #%d:\n",++kcase);
int res = 0;
for(int i=1;i<=n;i++){
res = 0;
for(int j=6;j;j--) res+=ans[i][j];
printf("%d\n",res);
}

for(int i=1;i<=n;i++) G[i].clear();
}
return 0;
}