Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2077 Accepted Submission(s): 1050
Problem Description
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.
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
Author
scnu
Recommend
lcy
题意: 给你一棵树, 问任意节点的最长路径。
思路: 树形DP
dp[i][j] 表示的第i个节点,第j条路径的最长路径
先搜出往儿子路径的各长度。
然后在树形DP
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
//树形DP(以节点1为根节点)
const int V = 10000 + 10;
const int inf = 0x7fffffff;
int n;
vector< pair<int, int> > vec[V]; //建图
vector<int> dp[V]; //各路经长度 0为父亲路径
int son_Max[V][2]; //儿子最大路径
int ans[V]; //结果
bool vis[V]; //标记搜索
int dfs(int index, int fa) {
int i, j, k, len = 0;
vis[index] = true;
for(i = 0; i < vec[index].size(); ++i) {
if(!vis[vec[index][i].first]) //如果不是父亲路径
dp[index].push_back(dfs(vec[index][i].first, index) + vec[index][i].second);
else
dp[index].push_back(0);
if(len < dp[index][dp[index].size() - 1]) {
len = dp[index][dp[index].size() - 1];
son_Max[index][0] = len;
son_Max[index][1] = dp[index].size() - 1;
}
}
return len;
}
void Tree_DP(int index, int len) {
int i, j, k;
dp[index][0] = len;
ans[index] = max(len, son_Max[index][0]);
vis[index] = true;
for(i = 0; i < vec[index].size(); ++i) {
if(vis[vec[index][i].first])
continue;
int Path = vec[index][i].second;
if(dp[index][0] >= son_Max[index][0]) //如果父亲路径更好
Path += dp[index][0];
else if(i != son_Max[index][1]) //如果不与该儿子路径相同
Path += son_Max[index][0];
else { //寻找第二长路径
int temp = 0;
for(j = 0; j < dp[index].size(); ++j)
if(j != i)
temp = max(temp, dp[index][j]);
Path += temp;
}
Tree_DP(vec[index][i].first, Path);
}
}
int main() {
int i, j, k;
while(~scanf("%d", &n)) {
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
memset(son_Max, 0, sizeof(son_Max));
for(i = 1; i <= n; ++i) {
vec[i].clear();
dp[i].clear();
}
vec[1].push_back(make_pair(0, 0));
vis[0] = true;
for(i = 2; i <= n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
vec[i].push_back(make_pair(a, b));
vec[a].push_back(make_pair(i, b));
}
ans[1] = dfs(1, 0);
memset(vis, false, sizeof(vis));
vis[0] = true;
Tree_DP(1, 0);
for(i = 1; i <= n; ++i)
printf("%d\n", ans[i]);
}
}