hdu-2169 Computer(树形dp+树的直径)
题目链接:
Computer
Time Limit: 1000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
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.
/* 2196 15MS 1976K 2205 B G++ 2014300227*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,vis[N],head[N],cnt,a,b,dp[N],fp[N],mmax,ending;
queue<int>qu;
struct Edge
{
int to,next,val;
};
Edge edge[*N];
void add_edge(int s,int e,int va)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=va;
head[s]=cnt++;
}
void dfs(int x,int leng)
{
if(leng>mmax)
{
ending=x;
mmax=leng;
}
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
dfs(y,leng+edge[i].val);
}
}
}
void bfs1()
{
qu.push(ending);
dp[ending]=;
vis[ending]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
for(int i=head[fr];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(vis[y])
{
dp[y]=dp[fr]+edge[i].val;
qu.push(y);
vis[y]=;
}
}
}
}
void bfs2()
{
int start,mmx=;
for(int i=;i<=n;i++)
{
if(dp[i]>mmx)
{
start=i;
mmx=dp[i];
}
}
qu.push(start);
fp[start]=;
vis[start]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
for(int i=head[fr];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
fp[y]=fp[fr]+edge[i].val;
dp[y]=max(dp[y],fp[y]);
vis[y]=;
qu.push(y);
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
vis[i]=;
head[i]=-;
}
cnt=;
mmax=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
add_edge(i,a,b);
add_edge(a,i,b);
}
dfs(,);
bfs1();
bfs2();
for(int i=;i<=n;i++)
{
printf("%d\n",dp[i]);
}
}
return ;
}
hdu-2169 Computer(树形dp+树的直径)的更多相关文章
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Computer(HDU2196+树形dp+树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...
- hdu 4607 树形dp 树的直径
题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- VIJOS1476旅游规划[树形DP 树的直径]
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- poj3162 树形dp|树的直径 + 双单调队列|线段树,好题啊
题解链接:https://blog.csdn.net/shiqi_614/article/details/8105149 用树形dp是超时的,, /* 先求出每个点可以跑的最长距离dp[i][0|1] ...
随机推荐
- vs调试,监控变量
- 阿里云官方教程 Linux 系统挂载数据盘
适用系统:Linux(Redhat , CentOS,Debian,Ubuntu) * Linux的云服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作. 下面的操作将会把数据盘划 ...
- DisplayPort的时钟隐藏和时钟恢复
转:DisplayPort的时钟隐藏和时钟恢复 无时钟线的视频数据传输是DP协议的一大特点,将时钟信号隐藏在数据中是传输协议的设计趋势.时钟恢复技术也是DP芯片设计的关键技术.在这说一下在发送端时钟是 ...
- jquery文档处理如after错误
$('.foo').after('<li>0<li>')会添加两个元素 $('.foo').after('<li>o</li>')只会添加一个元素 所以 ...
- iBatis2 SqlMap中经常使用sql语句
本来我也不喜欢iBatis,那是由于我当时还不怎么会用它,如今我想说,iBatis是个好东西,不信你试试看.以下是我在项目实践中对iBatis的一个小总结.希望帮助众多在疲于iBatis编码而无暇思考 ...
- HTML5 2D平台游戏开发#4状态机
在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...
- Paxos算法学习
早在1990年,Leslie Lamport(即 LaTeX 中的"La",微软研究院科学家,获得2013年图灵奖)向ACM Transactions on Computer Sy ...
- php正则表达式匹配img中任意属性的方法
经常和图片打交道,不得不用到一些提取图片中scr.alt.title.等的属性,这里总结给大家一些常用的,感觉还不错,比较通用! PHP正则表达式匹配img中任意属性PHP 复制代码代码如下: < ...
- PHP面向对象——构造函数、析构函数
在PHP中构造函数和析构函数是固定的,如下: // 构造函数 function __construct([argument1,argument2,...,argumentN]){ /* Class i ...
- 【Python基础】之异常
一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...