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 ...
随机推荐
- ACM_梦中的函数
梦中的函数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 寒假那段时间,每天刷题的小G连做梦都是代码,于是有了这道题. 给定一个 ...
- Android 显示意图和隐式意图的区别
意图在android的应用开发中是很重要的,明白了意图的作用和使用后,对开发会有很大帮助.如果没有把意图搞懂,以后开发应用会感觉缺些什么. 意图的作用: 1.激活组件 ...
- windows server 2008 r2 安裝IE11
https://support.microsoft.com/en-us/help/2847882/prerequisite-updates-for-internet-explorer-11 https ...
- Criteria 查询
Criteria.Criterion接口和Expression类组成,他支持在运行时动态生成查询语句. Criteria查询是Hibernate提供的一种查询方式 Hibernate检索方式: PO ...
- 字符串String的理解
1.String是一个final的类型 即不可被继承修改,一经生成不可改变.所以在代码中使用String s = s1 + s2;的时候,执行完之后s所指向的是一个新生成的对象,这里有个地方值得注意 ...
- [ SCOI 2009 ] 最长距离
\(\\\) \(Description\) 一个\(N\times M\)的网格图中有一些坏点,图是四联通的. 你至多可以拿走\(K\)个坏点,求拿走后联通的点对中欧几里得距离最大是多少. \(N, ...
- 1B课程笔记分享_StudyJams_2017
课程1B 概述 课程1B主要讲解了Android UI的ViewGroups(视图组).LinearLayout(线性布局).RelativeLayout(相对布局),Portrait Mode(竖屏 ...
- 【MySQL】二进制分发安装
操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz ...
- 史上巨坑: vim的"set foldmethod=syntax"设置竟然是导致ctrl+p(ctrl+n)补全在文件稍大时光标位于中间位置补全效率变慢的元凶!
最近我的vim又让我闹心了. 问题出现在supertab的补全速度上, 有时候按下tab键半天才弹出补全列表, 即便是弹出了列表在列表上下移动也变得的相当缓慢, 这让我的很是蛋疼. 在完全无法接受这个 ...
- IOS7 APP 升级的10个TIP 建议
There is no way to preserve the iOS 6 style status bar layout. The status bar will always overlap yo ...