hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4440 Accepted Submission(s):
2236
this computer's id is 1). During the recent years the school bought N-1 new
computers. Each new computer was connected to one of settled earlier. Managers
of school are anxious about slow functioning of the 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.
there is natural number N (N<=10000) in the first line, followed by (N-1)
lines with descriptions of computers. i-th line contains two natural numbers -
number of computer, to which i-th computer is connected 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.
number Si for i-th computer (1<=i<=N).
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 40100
#define maxn(x,y)(x>y?x:y)
using namespace std;
int head[MAX];
int vis[MAX],dis[MAX];
int n,m,ans,ant;
int sum,beg,en;
int a[MAX],b[MAX];
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int i,j,a,b;
ans=0;
memset(head,-1,sizeof(head));
for(i=2;i<=n;i++)
{
scanf("%d%d",&a,&b);
add(a,i,b);
add(i,a,b);
}
}
void bfs(int sx)
{
int i,j;
queue<int>q;
sum=0;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
vis[sx]=1;
beg=sx;
q.push(sx);
while(!q.empty())
{
int top=q.front();
q.pop();
for(i=head[top];i!=-1;i=edge[i].next)
{
int k=edge[i].v;
if(!vis[k])
{
vis[k]=1;
dis[k]=dis[top]+edge[i].w;
q.push(k);
}
if(sum<dis[k])
{
sum=dis[k];
beg=k;
}
}
}
}
void solve()
{
int i,j;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
bfs(1);
bfs(beg);
en=beg;//找到第一个端点
for(i=1;i<=n;i++)
a[i]=dis[i];//记录每个点到这个端点的距离
bfs(en);//找到另一个端点
for(i=1;i<=n;i++)
b[i]=dis[i];//记录每个点到这个端点的距离
for(i=1;i<=n;i++)
{
ant=0;
ant=maxn(a[i],b[i]);
printf("%d\n",ant);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
getmap();
solve();
}
return 0;
}
hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】的更多相关文章
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- 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 ...
- [hdu2196]Computer树的直径
题意:求树中距离每个节点的最大距离. 解题关键:两次dfs,第一次从下向上dp求出每个节点子树中距离其的最大距离和不在经过最大距离上的子节点上的次大距离(后序遍历),第二次从上而下dp求出其从父节点过 ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- POJ 1383 Labyrinth (树的直径求两点间最大距离)
Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...
- poj1985 / poj2631(树的直径)
poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...
- hdu4612 无向图中任意添加一条边后使桥的数量最少 / 无向图缩点+求树的直径
题意如上,含有重边(重边的话,俩个点就可以构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选任意起点U,进行bfs,到达最远的一个点v ...
随机推荐
- The version of CocoaPods used to generate the lockfile (*) is higher than the version of the current executable (*). Incompatibility issues may arise.
解决方法: sudo gem update cocoapod
- C++ 实现01背包动态规划
简述一下01背包: 背包容量大小固定,有一些物品,每个物品都有重量和价值两个属性,且物品唯一不重复(即同一物品只能放入一个),放入物品的总重量不能超过背包容量 ,求放入背包的物品的总价值最大化.0代表 ...
- WCF大文件传输服务
由于项目需要,自己写一个基于WCF的大文件传输服务雏形.觉得有一定的参考价值,因此放在网上分享. 目前版本为v1.1特点如下: 1.文件传输端口为18650 2.上传和下载文件 3.支持获取文件传输状 ...
- php中如何实现网上商城用户历史浏览记录的代码
/如是COOKIE 里面不为空,则往里面增加一个商品ID if (!empty($_COOKIE['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 ...
- 一个在线的C++帮助文档网站 转载
http://www.cplusplus.com/ //C++参考 http://www.cppreference.com/wiki/start //C++参考 http ...
- mysql 刘道成视频教程 第3课
第3课: 1.mysql中条件使用关键字where: 2.查 select name,content from msg ; select name,content 控制列 where id>2 ...
- USACO 2.2 Subset Sums 集合(subset)
Description 对于从1到N的连续整集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字和是相等的: {3} ...
- C#中Socket用法,多个聊天和单一聊天。
自己琢磨Socket刚刚几天,所以整理出来和大家共享一下.废话少说直接进入正题. 在C#中提供了两种网络服务,一种是Socket类,另一种是TcpListener(服务器),TcpClient(客户端 ...
- GridView获取单个单元格的值
0.GridView中的所有数据都存储在Rows集合中,可以通过Rows的Cell属性获取单个单元格的值:如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,从单元格检索控件:如果 ...
- 纯JS写出日历
封装代码: (function(cmf){ cmf.showDcalendar=function(){ var fnname=cmf.fn var id="cmfrili2" va ...