【poj Roads in the North】 题解
题目链接:http://poj.org/problem?id=2631
求树的直径模板。
定理:
树上任意一个点的在树上的最长路一定以树的直径的两端点其中一点结束。
做法:
两边bfs,第一次先找到node(树的直径的两端点其中一个),再一次求node的最长路所结束的点t
node—>t就是树的直径
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int n, dis[maxn], ans, node;
bool vis[maxn];
struct edge{
int from, to, next, len;
}e[maxn<<2];
int cnt, head[maxn];
void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].len = w;
e[cnt].to = v;
head[u] = cnt;
}
queue<int> q;
void bfs(int s)
{
vis[s] = 1;
q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(vis[e[i].to] == 0)
{
dis[e[i].to] = dis[now] + e[i].len;
vis[e[i].to] = 1;
q.push(e[i].to);
if(dis[e[i].to] > ans)
{
ans = dis[e[i].to];
node = e[i].to;
}
}
}
}
}
int main()
{
memset(head, -1, sizeof(head));
int u, v, w;
while(scanf("%d%d%d",&u, &v, &w) == 3)
{
add(u, v, w);
add(v, u, w);
}
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
ans = 0;
bfs(1);
memset(vis, 0, sizeof(vis));
dis[node] = 0;
ans = 0;
bfs(node);
printf("%d\n", ans);
return 0;
}
//Misaka_Azusa
//dsbdsb
【poj Roads in the North】 题解的更多相关文章
- [Poj] Roads in the North
http://poj.org/problem?id=2631 树的直径裸题 dfs/bfs均可 /* dfs */ #include <iostream> #include <cst ...
- Roads in the North POJ - 2631
Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
随机推荐
- java——程序的导出与导入
导出: 选择项目,右击选择 最下面的properties——Resource——Location,就是你的项目所在地, 找到文件所在,拷贝到你的U盘中(或者直接点击项目直接拖到桌面)完成复制 导入: ...
- AndroidVideoCache 框架源码分析
1.简析: 在客户端播放视频的使用,容易出现这样的一个问题.在网络状况不好的情况下,视频流很容易卡顿或者中断,即使播放软件本身有一点的缓存能力,但是这个往往不够,造成播放失败,卡顿. AndroidV ...
- MobileWeb 适配总结
开门见山,本篇将总结一下 MobileWeb 的适配方法,即我们常说的H5页面.手机页面.WAP页.webview页面等等. 本篇讨论的页面指专门针对手机设备设计的页面,并非兼容全设备的响应式布局. ...
- js Array数组对象常见方法总结
Array对象一般用来存储数据. 其常用的方法包括: 1.concat()方法 concat() 方法用于合并两个或多个数组.它不会更改现有数组,而是返回一个新数组. 例如: var arr1=[1, ...
- jQuery阻止向上冒泡事件
//阻止起泡取消下面的注释 e.stopPropagation(); //或者使用这种方式 //return false; }); $('.three').click(function(e){ ale ...
- 安卓app应用开发资料
android 配置文件画图 http://blog.csdn.net/loongggdroid/article/details/46687589 android下拉刷新控件 https://gith ...
- 《Visual C++ 2010入门教程》系列五:合理组织项目、使用外部工具让工作更有效
原文:http://www.cnblogs.com/Mrt-02/archive/2011/07/24/2115631.html 这一章跟大家分享一些与c++项目管理.VAX.SVN.VS快捷键等方面 ...
- CentOS 附加软件包
本人初学 CentOS,安装软件与 windows 下区别很大,大部分得通过 yum install xxx .这有个问题,一方面 yum 资源有限,另一方面 yum 默认装的版本较低.比如 Cent ...
- git rebase --onto详解
https://blog.pivotal.io/labs/labs/git-rebase-onto http://www.cnblogs.com/rickyk/p/3848768.html
- 用CIFilter生成QRCode二维码图片
用CIFilter生成QRCode二维码图片 CIFilter不仅仅可以用来做滤镜,它还可以用来生成二维码. CIFilterEffect.h + CIFilterEffect.m // // CIF ...