hdu 2196 Computer 树形dp模板题
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2850 Accepted Submission(s): 1450
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.
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.
1 1
2 1
3 1
1 1
2
3
4
4
代码:
/***
分析:以编号的i的节点为例(非根节点),最长的路径长度只有俩种可能,
1)子树中存在最长路径;
2)通过父节点的路径中存在最长路径
所以,只有分别求出每一节点对应的那俩种路径取大最大值即可,其中,根节点只存在第一种可能
***/
#include "stdio.h"
#include "string.h" #define N 10005 struct node{
int x,y;
int weight;
int next;
}edge[*N];
int idx,head[N]; void Init(){idx=; memset(head,-,sizeof(head));} void Add(int x,int y,int weight)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = weight;
edge[idx].next = head[x];
head[x] = idx++;
} struct point{
int id;
int value;
}dp1[N],dp2[N]; //dp1[i]记录点i的最远距离,dp2[i]记录点i的次远距离, void swap(point &a,point &b)
{
point c;
c = a;
a = b;
b = c;
} void DFS1(int x,int father)
{
int i,y;
dp1[x].value = dp2[x].value = ;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
DFS1(y,x);
if(dp1[y].value + edge[i].weight > dp2[x].value)
{
dp2[x].value = dp1[y].value + edge[i].weight;
dp2[x].id = y;
if(dp1[x].value < dp2[x].value) //dp1[i]记录点i的最远距离,dp2[i]记录点i的次远距离,
swap(dp1[x],dp2[x]);
}
}
} void DFS2(int x,int father)
{
int i,y;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
if(dp1[x].id == y) //点y是父亲x的最远距离的下一个节点
{
if(dp2[y].value < dp2[x].value+edge[i].weight) //,那么看点y的次元距离能否通过父亲x的其他节点更新
{
dp2[y].value = dp2[x].value + edge[i].weight;
dp2[y].id = x;
if(dp1[y].value < dp2[y].value)
swap(dp1[y],dp2[y]);
}
}
else
{
if(dp2[y].value < dp1[x].value+edge[i].weight)
{
dp2[y].value = dp1[x].value+edge[i].weight;
dp2[y].id = x;
if(dp1[y].value < dp2[y].value)
swap(dp1[y],dp2[y]);
}
}
DFS2(y,x);
}
} int main()
{
int i,n;
int x,y,k;
while(scanf("%d",&n)!=EOF)
{
Init();
for(y=; y<=n; ++y)
{
scanf("%d %d",&x,&k);
Add(x,y,k);
Add(y,x,k);
}
DFS1(,-);
DFS2(,-);
for(i=; i<=n; ++i)
printf("%d\n",dp1[i].value);
}
return ;
}
hdu 2196 Computer 树形dp模板题的更多相关文章
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer(树形DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer(树形DP经典)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- HDU - 2196(树形DP)
题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...
- hdu 2196【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...
- HDU 2196 Compute --树形dp
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- jQuery实现星星评分功能
一.这是我做的调查问卷中的一个功能.(第三方MVC框架) 二.功能说明:1.用户点击星星,将值放到隐藏域中.2.用户可以重新点击星星修改回答. 前台JS代码: <script type=&quo ...
- ASP.NET MVC的路由
好久没写博文了,感觉最近好像少了点动力.唉!这回就看看这个MVC的路由. 说这个路由机制其实不是MVC里面特有的,ASP.NET里面本身就有的,只不过在WebForm里面一般比较少用,而在MVC里就是 ...
- Winfrom中ListBox绑定List数据源更新问题
Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...
- 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据
[源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...
- js倒计时防页面刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- No.002:Add Two Numbers
问题: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- Java内存泄露的原因
Java内存泄露的原因 1.静态集合类像HashMap.Vector等的使用最容易出现内存泄露,这些静态变量的生命周期和应用程序一致,所有的对象Object也不能被释放,因为他们也将一直被Vector ...
- html5 大幅度地增加和改良input元素的种类
增加和改良input元素 url类型.email类型.date类型.time类型.datetime类型.datetime-local类型.month类型.week类型.number类型.range类型 ...
- img标签src不给路径就会出现边框————记一次二笔的编码经历
<img/>在src加载失败或没有给的,浏览器会自动给img加上边框. 如下图这样: 产品觉得影响美观,一定要pass掉. 原码是这样: .ctn{ position: relative; ...
- RHEL7虚拟机实验快照
配置虚拟机连接网络 首先确保NetworkManager服务正常运行 [root@administrator ~]# systemctl status NetworkManager ● Network ...