hdu2196 树的直径 + bfs
- //Accepted 740 KB 15 ms
- //树的直径
- //距离一个顶点最远的点一定是树的直径的一个端点
- #include <cstdio>
- #include <cstring>
- #include <queue>
- #include <iostream>
- using namespace std;
- ;
- int dis[imax_n];
- int d1[imax_n];
- int d2[imax_n];
- int head[imax_n];
- *imax_n];
- int e;
- struct node
- {
- int u,v,c;
- node()
- {
- }
- node(int u,int v,int c)
- {
- this->u=u;
- this->v=v;
- this->c=c;
- }
- }p[*imax_n];
- int n;
- void addEdge(int u,int v,int c)
- {
- p[e]=node(u,v,c);
- next[e]=head[u];
- head[u]=e++;
- }
- void init()
- {
- memset(head,-,sizeof(head));
- memset(next,-,sizeof(next));
- e=;
- }
- void read()
- {
- init();
- int x,y;
- ;i<=n;i++)
- {
- scanf("%d%d",&x,&y);
- addEdge(i,x,y);
- addEdge(x,i,y);
- }
- }
- int max(int a,int b)
- {
- return a>b?a:b;
- }
- queue<int >q;
- bool vis[imax_n];
- int bfs(int s)
- {
- //printf("111111\n");
- memset(dis,,sizeof(dis));
- ;
- int temp_node;
- while (!q.empty()) q.pop();
- memset(vis,,])*(n+));
- q.push(s);
- vis[s]=;
- while (!q.empty())
- {
- int x=q.front();
- if (dis[x]>ans)
- {
- ans=dis[x];
- temp_node=x;
- }
- q.pop();
- ;i=next[i])
- {
- int y=p[i].v;
- if (!vis[y])
- {
- vis[y]=;
- q.push(y);
- if (dis[x]+p[i].c>dis[y])
- dis[y]=dis[x]+p[i].c;
- }
- }
- }
- return temp_node;
- }
- int main()
- {
- while (scanf("%d",&n)!=EOF)
- {
- read();
- );
- x=bfs(x);
- memcpy(d1,dis,sizeof(dis));
- bfs(x);
- memcpy(d2,dis,sizeof(dis));
- ;i<=n;i++)
- {
- printf("%d\n",max(d1[i],d2[i]));
- }
- }
- ;
- }
hdu2196 树的直径 + bfs的更多相关文章
- poj2631 树的直径 + bfs
//Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...
- 树上选两点(使最短)树的直径+bfs
题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...
- ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS
题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...
- luogu P3761 [TJOI2017]城市 树的直径 bfs
LINK:城市 谢邀,学弟说的一道毒瘤题. 没有真正的省选题目毒瘤 或者说 写O(n)的做法确实毒瘤. 这里给一个花20min就写完的非常好写的暴力. 容易想到枚举哪条边删掉 删掉之后考虑在哪两个点上 ...
- POJ1985 树的直径(BFS
Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants ...
- 树的直径(BFS)
][];];];];,,;vis[i]=; ; j <= n ; j++){ ){;//标记 res[j]=res[root]+; ; i <= n- ; i++){; data[b][a ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
- HDU4612+Tarjan缩点+BFS求树的直径
tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...
随机推荐
- Webdriver - Selenium Grid Configuration
Grid parameter: role = <hub|node> (default is no grid, just run an RC/webdriver server). When ...
- jmeter 建立一个网络服务的测试计划
如何创建一个 测试计划 测试一个网络服务. 你会 创建5个用户发送请求到一页. 同时,你会告诉用户运行测试两次. 的总数 请求用户请求(5)x(1)x(重复2次)= 10 HTTP请求. 来 建立测试 ...
- jmeter 构建一个Web测试计划
添加用户 第一步你想做的每一个JMeter测试计划是添加一个 线程组 元素. 线程组告诉 JMeter的用户数量你想模拟,用户应该发送的次数 请求,请求他们应该送多少. 继续添加ThreadGroup ...
- 加载图片、倒计时--Columbia项目总结
CSS3 居然会在隐藏显示的时候触发,真心太神奇了(在ff下是没有用的,在chrome下才阔以) 貌似我还是写了大循环,怎么样才能优化这个循环呢 加载图片:已经用到过好多次了↓ function lo ...
- Ubuntu下配置和编译cpp-ethereum客户端
Ethereum,中文翻译是“以太坊”,是一个公有区块链的开源项目.因为以太坊是基于P2P网络所以没有中心节点,所以用户仅安装Ethereum客户端即可连入Ethereum公共网络或者在自己的test ...
- BZOJ1737 [Usaco2005 jan]Naptime 午睡时间
断环然后裸DP就好了... $f[i][j][k]$表示1号时间段没有被算入答案,到了第$i$个时间段,一共选了$j$个时间段,$k = 0 /1$表示第i个时间段有没有被算进答案的最优值 $g[i] ...
- 源码编译Chrome
官网描述 http://www.chromium.org/developers/how-tos/build-instructions-windows 为啥还要写这篇博客 太久没在这里写博客 Chrom ...
- input 中的enabled与disabled属性
<style type="text/css"> *{ padding:; margin:; list-style-type: none; box-sizing:bord ...
- linux 查看僵尸进程
top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...
- PDF 补丁丁 0.4.1.688 测试版发布(请务必用其替换 682 测试版)
修复了测试版682 损坏书签.读取字符宽度表出错的问题.请下载了旧测试版的网友马上换用新的测试版.