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

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

https://blog.csdn.net/qq_33184171/article/details/72773662

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

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

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

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

ans稍麻烦些,

$ans[][i] = dp[][i] + ans[][i-1] - dp[][i-2];$

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