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 ...
随机推荐
- tomcat解决 java.lang.IllegalArgumentException: Request header is too large
tomcat运行项目时,有一个请求过去后,后台报这样的错java.lang.IllegalArgumentException: Request header is too large 原因:请求头超过 ...
- 大话设计模式--DI(依赖注入)
1.背景 想象一个场景:有个功能通过某个参数决定了路由到不同的方法上或者几个方法模块可以自由搭配,咋办?一般人会对每个方法写一个helper(比如SendMessageForEmail.SendMes ...
- [Codeforces]Codeforces Round #489 (Div. 2)
Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...
- css每次的初始化代码
;;} body{font-size:14px;} img{border:none;} li{list-style:none;} input,select,textarea{outline:none; ...
- TensorFlow-Gpu环境搭建——Win10+ Python+Anaconda+cuda
参考:http://blog.csdn.net/sb19931201/article/details/53648615 https://segmentfault.com/a/1190000009803 ...
- JS——鼠标跟随
注意事项: 1.pageX.pageY的兼容问题 2.使目标移动鼠标中间位置还必须减去盒子宽度的一半 <!DOCTYPE html> <html lang="en" ...
- Android中ViewPager动态创建的ImageView铺满屏幕
ImageView imageView=new ImageView(context); imageView.setScaleType(ScaleType.FIT_XY);//铺满屏幕
- jsp 文件下载
有的时候一个模板的下载,这种简单的下载服务端已存在文件功能,就可以方便的通过jsp文件下载的方式来轻松实现. //jsp 页面 js /** * 导出角色 */ function exportRole ...
- IIS添加映射配置
这种问题主要出现在使用应用程序级别的地址重写.如果你将一个动态的地址重写成虚拟的其它扩展名或者不带扩展名的地址,通常在IIS5.1和II6.0中,访问这样一个实际不存在的地址,首先会被Web服务器返回 ...
- POJ 3070 - 快速矩阵幂求斐波纳契数列
这题并不复杂. 设$A=\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}$ 由题中公式: $\begin{pmatrix}f(n+1) & ...