HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的...
三遍BFS求树的直径并预处理距离.......
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3522 Accepted Submission(s): 1784
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.
5
1 1
2 1
3 1
1 1
3
2
3
4
4
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue> using namespace std; const int maxn=20010; struct Edge
{
int to,next,w;
}edge[maxn*2]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v,int w)
{
edge[Size].to=v;
edge[Size].w=w;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} int dist_s[maxn],dist_t[maxn],dist[maxn]; int n; bool vis[maxn]; int bfs1()
{
int ret=1;
queue<int> q;
memset(vis,false,sizeof(vis));
q.push(1);
dist[1]=0;
vis[1]=true;
while(!q.empty())
{
int u=q.front(); q.pop(); for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int c=edge[i].w;
if(vis[v]) continue;
dist[v]=dist[u]+c;
vis[v]=true; q.push(v);
if(dist[v]>dist[ret])
ret=v;
}
}
return ret;
} int bfs2(int x)
{
int ret=x;
queue<int> q;
memset(vis,false,sizeof(vis));
q.push(x); vis[x]=true;
dist_s[x]=0;
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int c=edge[i].w;
if(vis[v]==true) continue;
vis[v]=true;
dist_s[v]=dist_s[u]+c;
q.push(v);
if(dist_s[v]>dist_s[ret])
ret=v;
}
}
return ret;
} int bfs3(int x)
{
int ret=x;
queue<int> q;
memset(vis,false,sizeof(vis));
q.push(x); vis[x]=true;
dist_t[x]=0;
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int c=edge[i].w;
if(vis[v]==true) continue;
vis[v]=true;
dist_t[v]=dist_t[u]+c;
q.push(v);
if(dist_t[v]>dist_t[ret])
ret=v;
}
}
return ret;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
memset(dist_s,0,sizeof(dist_s));
memset(dist_t,0,sizeof(dist_t));
memset(dist,0,sizeof(dist)); for(int i=2;i<=n;i++)
{
int x,w;
scanf("%d%d",&x,&w);
add_edge(x,i,w);
add_edge(i,x,w);
} int s=bfs1();
int t=bfs2(s);
bfs3(t);
//cout<<"s: "<<s<<" t: "<<t<<endl;
for(int i=1;i<=n;i++)
{
printf("%d\n",max(dist_s[i],dist_t[i]));
}
}
return 0;
}
HDOJ 2196 Computer 树的直径的更多相关文章
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
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条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- HDOJ --- 2196 Computer
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- [hdu2196]Computer树的直径
题意:求树中距离每个节点的最大距离. 解题关键:两次dfs,第一次从下向上dp求出每个节点子树中距离其的最大距离和不在经过最大距离上的子节点上的次大距离(后序遍历),第二次从上而下dp求出其从父节点过 ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
随机推荐
- 349 Intersection of Two Arrays 两个数组的交集
给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示: 每个在结果中的元素必定是唯一的. 我们 ...
- IOS开发之Swift学习笔记
1.因为存储属性要求初始化,我们可以使用lazy修饰符来延迟初始化.
- Html常用标签及全称
<!-- 块标签 divsion --><div></div> <!--br 换行 break--> <br /> <!--分割 ...
- python gdal 修改shp文件的属性值
driver = ogr.GetDriverByName('ESRI Shapefile')datasource = driver.Open(shpFileName, 1)layer = dataso ...
- 联想 K10(K10e70) 免解锁BL 免rec Magisk Xposed 救砖 ROOT 版本号 S206
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- html5——多媒体(二)
基本方法 load() //重新加载视频 play() //播放 pause() //暂停 基本属性 currentTime //视频播放的当前进度. duration //视频的总时间 paused ...
- jsp 文件下载
有的时候一个模板的下载,这种简单的下载服务端已存在文件功能,就可以方便的通过jsp文件下载的方式来轻松实现. //jsp 页面 js /** * 导出角色 */ function exportRole ...
- WCF开发的流程-服务端和客户端之间的通讯(内含demo讲解)
讲解技术之前,恳请博友让我说几句废话.今天是我第一在博客园发布属于自己原创的博文(如有雷同,那是绝对不可能的事,嘿嘿).之前一直是拜读各位博友的大作,受益匪浅的我在这对博友们说声谢谢,谢谢你们的共享! ...
- python os os.path模块学习笔记
#!/usr/bin/env python #coding=utf-8 import os #创建目录 os.mkdir(r'C:\Users\Silence\Desktop\python') #删除 ...
- 本地读取服务器Xml文件及本地读本地的xml
updateUrl="ServerUrl"(服务器路径) WebClient wc = new WebClient(); Stream stream = wc.OpenRead(u ...