hdu 2196 Computer(树形DP经典)
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5925 Accepted Submission(s): 2979
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.
1 1
2 1
3 1
1 1
2
3
4
4
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#define N 10010
using namespace std;
struct node
{
int to,len;//下一个节点,长度
node (int x,int y)
{
to=x;
len=y;
}
};
int n;
/*
第一个dfs是先搜然后再转移状态的,用结点下方的数据更新
第二个dfs是先状态转移然后再搜的,这是从结点上方进行数据更新,刚好满足题意:结点左右两个“子树”
*/
vector<node> adj[N*];
int maxn[N];//第i个点的最大权值
int smaxn[N];//第i个点的第二大的权值
int maxi[N];//最大权值的点
int smaxi[N];//第二大权值的点
void dfs1(int u,int p)//p是u的父节点
{
maxn[u]=;
smaxn[u]=;
for(int i=;i<adj[u].size();i++)
{
int v=adj[u][i].to;
if(v==p) continue;//和父节点一样
dfs1(v,u);
if(maxn[v]+adj[u][i].len>smaxn[u])//先和第二长的距离比较,这样能记录下第二长的距离,这个数据能为下一个dfs提供判断
{
smaxn[u]=maxn[v]+adj[u][i].len;
smaxi[u]=v;
if(maxn[u]<smaxn[u])//更新之后第二长的路径,如果比原来最长的路径还长就替换,并且原来的最长的就变成第二长得了
{
swap(maxn[u],smaxn[u]);
swap(maxi[u],smaxi[u]);
}
}
}
} //从父节点更新过来的
void dfs2(int u,int p)
{
for(int i=;i<adj[u].size();i++)
{
int v=adj[u][i].to;
if(v==p) continue;//和父节点一样
if(v==maxi[u])
//这个地方如果下一个结点刚巧是就是dfs1中搜出来的最大值的节点的话
//那么这一条路就不能用最大值去算,因为这样就算上上一条路的了
{
if(adj[u][i].len+smaxn[u]>smaxn[v])
{
smaxn[v]=adj[u][i].len+smaxn[u];
smaxi[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxi[v],smaxi[v]);
}
}
}
else
{
if(adj[u][i].len+maxn[u]>smaxn[v])
{
smaxn[v]=adj[u][i].len+maxn[u];
smaxi[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxi[v],smaxi[v]);
}
}
}
dfs2(v,u);
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
adj[i].clear();
for(int i=;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
adj[i].push_back(node(a,b));
adj[a].push_back(node(i,b));
}
dfs1(,-);//从子节点更新
dfs2(,-);//从父节点更新
for(int i=;i<=n;i++)
printf("%d\n",maxn[i]);
}
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 S ...
- 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 ...
随机推荐
- 【转】NoClassDefFoundError和ClassNotFoundException
调试Hadoop源码时,一运行就报这个错误,后来发现是maven配置时,scope配置的问题, MAVEN Scope使用 相关链接:http://acooly.iteye.com/blog/178 ...
- 【转】常用Maven插件
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...
- Sets 比赛时想错方向了。。。。 (大数不能处理负数啊)
Sets Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...
- Training little cats poj3735
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9299 Accepted: 2 ...
- 天上掉Pizza
天上掉Pizza 时间限制: 3 Sec 内存限制: 128 MB提交: 73 解决: 48[提交][状态][讨论版] 题目描述 明明喜欢Pizza,但总是缺钱.有一天,他在报纸上阅读,他最喜爱的 ...
- Centos7 创建本地 docker 仓库极其遇到的问题
环境安装: VirtualBox 安装 Centos7 安装 docker 1. 配置私有仓库和客户端地址 私有仓库:192.168.1.104 客户端:192.168.1.103 通过 Centos ...
- Log4net日志使用教程-控制台、文本、数据库三种记录方式
一.log4net简介: 1. Log4net的优点: 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的 ...
- angualrJs清除定时器
angualrJs清除定时器爬坑之路: 今天发现一个奇怪问题,放在自定义指令里边的定时器竟然在页面跳转之后,在另一个页面这个循环定时器还在执行,这肯定是不行的,会影响系统的性能. 我在angular里 ...
- jquery定时刷新数据
$(function () { setInterval("startRequest()", 3000); }); function startRequest() { $(" ...
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...