hdu2196 Computer (树形dp)
2017-02-14 16:34
405 查看
题目链接
A school bought the first computer some time ago(so this computer’s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.
Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4
这是一道经典的树形dp题目,先把其中一个点当做根节点,就可以把它看成一棵树,每个节点的最长路径有两种可能,一种是从自己的子节点方向过来的,另一种是从父节点过来。但是在从父节点过来的路径可能是经过它本身的,所以对于每个点,从子节点方向过来的要保留两个值,即最长和次长,可以存入dp[i][0],dp[i][1];
而更新从父节点过来的值时,比较cost+dp[son][0]==dp[father][0],如果相等,则说明是从经过它本身的,此时应选取次长节点,存入dp[son][2]中。
附上AC代码,参考了别人的博客写的。(刚开始看成了一组数据wrong了好多发……还有后来的蜜汁output limited exceed)
A school bought the first computer some time ago(so this computer’s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.
Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4
这是一道经典的树形dp题目,先把其中一个点当做根节点,就可以把它看成一棵树,每个节点的最长路径有两种可能,一种是从自己的子节点方向过来的,另一种是从父节点过来。但是在从父节点过来的路径可能是经过它本身的,所以对于每个点,从子节点方向过来的要保留两个值,即最长和次长,可以存入dp[i][0],dp[i][1];
而更新从父节点过来的值时,比较cost+dp[son][0]==dp[father][0],如果相等,则说明是从经过它本身的,此时应选取次长节点,存入dp[son][2]中。
附上AC代码,参考了别人的博客写的。(刚开始看成了一组数据wrong了好多发……还有后来的蜜汁output limited exceed)
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <vector> #define maxn 10010 using namespace std; int dp[maxn][3]; struct edge { int to,cost; }; vector<edge> g[maxn]; void dfs1(int x,int pre) { for(int i=0; i<g[x].size(); i++) { int v=g[x][i].to; int cost=g[x][i].cost; if(v==pre) continue; dfs1(v,x); //因为在更新从子树的长度而来的最长和次长的长度时,需要用到子树的值,而我们的dfs是从根节点开始遍历的,所以要先进行dfs再更新值 if(dp[v][0]+cost > dp[x][0]) { dp[x][1]=dp[x][0]; //更新从子树路径上次长长度 dp[x][0]=dp[v][0]+cost; //更新最长长度 } else if(dp[x][1]<dp[v][0]+cost) dp[x][1]=dp[v][0]+cost; //更新次长长度 } } void dfs2(int x,int pre) { for(int i=0; i<g[x].size(); i++) { int v=g[x][i].to; int cost=g[x][i].cost; if(v==pre) continue; dp[v][2]=max(dp[x][2],dp[x][0]==dp[v][0]+cost?dp[x][1]:dp[x][0])+cost; dfs2(v,x); //dfs2是更新从父节点途径过来的长度,所以要用到父节点中的值,所以先进行更新,再下一步dfs } } int main() { int n,p; while(~scanf("%d",&n)&&n) { for(int i=1; i<=n; i++) g[i].clear(); int a,b; for(int i=2; i<=n; i++) { cin>>a>>b; g[i].push_back((edge){a,b}); g[a].push_back((edge){i,b}); } memset(dp,0,sizeof(dp)); dfs1(1,-1); dfs2(1,-1); for(int i=1; i<=n; i++) cout<<max(dp[i][0],dp[i][2])<<endl; } return 0; }
相关文章推荐
- [HDU2196]Computer(树形dp)
- 树形DP---例题:hdu2196 Computer
- hdu2196 Computer[树形dp]
- HDU2196 - Computer(树形DP)
- hdu2196-树形dp|树的直径-Computer
- HDU2196 Computer(树形dp)
- hdu2196-Computer(树形dp)
- HDU2196 Computer(树形dp经典)
- hdu2196:Computer (树形dp)
- hdu2196--Computer(树形DP练习2)
- HDU2196-Computer(树形dp)
- hdu2196 Computer(树形dp)
- HDU2196-Computer(树形dp)
- [hdu2196]Computer(树形dp)
- 【HDU2196 Computer】经典树形dp
- hdu2196 Computer 【树形dp】
- 【树形DP】 hdu2196 Computer
- hdu2196 Computer 树形DP 树上点到其它点的最远距离
- HDU2196 Computer(树形DP)
- HDU2196 Computer 树形dp