Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5194    Accepted Submission(s): 2620

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.

 
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
题意:求树上每个点到离它最远的点的距离.(方法一难懂,方法二我也不知道为什么是正确的,没办法证明)
方法一:
分析:要求树上每个点能够到达的最远距离,那么最远距离这个值有两个来源,分别是来自其子树或者来自其父亲,我们先用一次深搜将每棵树的子树遍历,求出子树的最大值和次
大值并且两个值所在的分支不同,为什么需要求出次大值呢?这里就是本题的精华了:在第二次DFS时,如果父亲在子树能够到达的最远距离和当前要求解的子结点在同一条分支上,那么我们就肯定不能选这条了,而应该选其"兄弟"结点中的最长路,也就是次短路.(这一点需要画图好好理解)
dp[i][0]代表以i为根子树中的最长路,dp[i][1]代表子树中的次长路,dp[i][2]代表父亲树中的最长路,设i的父亲为fi
dp[i][2] = max(dp[fi][2] , dp[i][0]+dis[i][fi]==dp[fi][0]?dp[fi][1]:dp[fi][0]) + dis[i][fi];
///题意:求树上每个点到离它最远的点的距离.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 10050
using namespace std; struct Edge{
int u,v,w,next;
}edge[*N];
int head[N];
int dp[N][]; ///dp[i][0]代表以i为根子树中的最长路,dp[i][1]代表子树中的次长路,dp[i][2]代表父亲树中的最长路 void addEdge(int u,int v,int w,int &k){
edge[k].u = u,edge[k].v = v,edge[k].w = w;
edge[k].next = head[u],head[u]=k++;
} void dfs(int u,int fa){ ///找子树中的最大值和次大值
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(v==fa) continue;
//printf("%d\n",v);
dfs(v,u);
if(dp[u][]<dp[v][]+edge[k].w){
dp[u][] = dp[v][] + edge[k].w;
if(dp[u][]>dp[u][]){
swap(dp[u][],dp[u][]);
}
}
}
}
void dfs1(int u,int fa){///找从父亲延伸过去的最大值
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w = edge[k].w;
if(v==fa) continue;
dp[v][] = max(dp[u][] , dp[v][]+edge[k].w==dp[u][]?dp[u][]:dp[u][]) + edge[k].w;
dfs1(v,u);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
int tot=;
for(int u=;u<=n;u++){
int v,w;
scanf("%d%d",&v,&w);
addEdge(u,v,w,tot);
addEdge(v,u,w,tot);
}
dfs(,-);
dfs1(,-);
for(int i=;i<=n;i++){
printf("%d\n",max(dp[i][],dp[i][]));
}
}
return ;
}

方法二:先对任意一个点进行搜索的到离它最远的端点,这个点必定是树的直径(树的直径指树中的最长路)的其中一个端点,然后以这个端点开始又进行搜索,得到一个离他最远

的店,这个点是直径的另外一个端点,我们在找的时候分别更新所有点到两个端点的距离,对于每个点我们区大值就是结果。

///题意:求树上每个点到离它最远的点的距离.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define N 10050
using namespace std; struct Edge{
int u,v,w,next;
}edge[*N];
int head[N]; void addEdge(int u,int v,int w,int &k){
edge[k].u = u,edge[k].v = v,edge[k].w = w;
edge[k].next = head[u],head[u]=k++;
}
int dis[N],dis1[N];
int vis[N];
void BFS(int x){
memset(vis,,sizeof(vis));
queue<int> q;
q.push(x);
vis[x]=;
while(!q.empty()){
int t = q.front();
q.pop();
for(int k=head[t];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(!vis[v]){
vis[v]=;
q.push(v);
dis[v] =max(dis[v],dis[t]+w);
}
}
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
memset(dis,,sizeof(dis));
memset(dis1,,sizeof(dis1));
int tot=;
for(int u=;u<=n;u++){
int v,w;
scanf("%d%d",&v,&w);
addEdge(u,v,w,tot);
addEdge(v,u,w,tot);
}
BFS();
int START=,END=;
int len = -;
for(int i=;i<=n;i++) {
if(dis[i]>len) {len = dis[i],START = i;}
}
memset(dis,,sizeof(dis));
BFS(START);
len = -;
for(int i=;i<=n;i++) {
dis1[i]=dis[i];
if(dis[i]>len) {len = dis[i],END = i;}
}
memset(dis,,sizeof(dis));
BFS(END);
for(int i=;i<=n;i++){
printf("%d\n",max(dis[i],dis1[i]));
} }
return ;
}

hdu 2196(方法1:经典树形DP+方法2:树的直径)的更多相关文章

  1. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  2. HDU 4616 Game(经典树形dp+最大权值和链)

    http://acm.hdu.edu.cn/showproblem.php?pid=4616 题意:给出一棵树,每个顶点有权值,还有存在陷阱,现在从任意一个顶点出发,并且每个顶点只能经过一次,如果经过 ...

  3. hdu 1054 Strategic Game 经典树形DP

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. hdoj3534(树形dp,求树的直径的条数)

    题目链接:https://vjudge.net/problem/HDU-3534 题意:给出一棵树,求树上最长距离(直径),以及这样的距离的条数. 思路:如果只求直径,用两次dfs即可.但是现在要求最 ...

  5. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  6. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  7. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  8. HDU 1561 The more, The Better 经典树形DP

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. HDU 3534 Tree (经典树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 ...

随机推荐

  1. Python调用MYSQL,将文件名和路径批量入库用法小结

    最近项目需要将大量的压缩文件导入到数据库中,所以开始总结用Python批量处理的办法,本次是首先将这些压缩文件的文件名提取出来,然后导入到数据库中. 由于涉及到路径的读取处理,所以方法有os模块和co ...

  2. beta版本冲刺三

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  3. sc"

    2.11 题目:二叉搜索树中的最近公共祖先 2.12 设计思路 if 树中不存在 u 或 v 错误 结束程序 定义 p 指向根节点 while true do: if p->key大于 u 和 ...

  4. lintcode-130-堆化

    130-堆化 给出一个整数数组,堆化操作就是把它变成一个最小堆数组. 对于堆数组A,A[0]是堆的根,并对于每个A[i],A [i * 2 + 1]是A[i]的左儿子并且A[i * 2 + 2]是A[ ...

  5. 【Linux】——搭建nexus

    1.安装 前提条件: JDK已经安装,运行java -version查看. 将本地下载好的nexus存放到linux上,存放路径为 /usr/local/software.可使用winscp直接拷贝. ...

  6. float与定位的区别

    float和绝对定位的区别 CSS中脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位.1 需要注意的是,使用float脱离文档流时,其他盒子 ...

  7. fzu 2246(ac 自动机)

    fzu 2246(ac 自动机) 题意: 某一天YellowStar学习了AC自动机,可以解决多模式匹配问题.YellowStart当然不会满足于此,它想进行更深入的研究. YellowStart有一 ...

  8. HDU1213:How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. c++(类)构造函数、复制构造函数

    复制构造函数是一种特殊的构造函数,它的作用是用一个已经存在的对象去初始化另一个对象.一般情况下不需要自行定义复制构造函数,系统默认提供一个逐个复制成员值的复制构造函数. 何时要使用呢? 1.将新对象初 ...

  10. 计算1-1/x+1/x*x

    // algo1-1.cpp 计算1-1/x+1/x*x. #include<stdio.h> #include<sys/timeb.h> void main() { time ...