【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 ...
随机推荐
- <<数学传奇>>概述
简介 在网易公开课上看完了蔡天新教授的<<数学传奇>>,课程共10集.分别讲述了古希腊的毕达哥拉斯.波斯(伊朗)的 花拉子密和欧玛尔.海亚姆.中国的秦九韶.英国的牛顿.法国的笛 ...
- OOP 第一章作业总结
程序设计结构分析 类图分析 第一次作业 由于第一次作业完成的功能比较简单,而且出于对面向对象设计理念不熟悉(其实现在也不是很熟悉,逃),整个程序设计的非常简单.通过类图(见下)可以看出,程序只有两个类 ...
- input的属性用法介绍
Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=text 输入类型是text,这是我们见的 ...
- 从ExtensionLoader理解Dubbo扩展机制
Dubbo的扩展机制是怎么实现的?最简单的回答就是@SPI. Dubbo的插件化思路来源于Java SPI. JAVA SPI 机制 SPI的全名为Service Provider Int ...
- Could not open php://output for writing.问题解决
这是一个自己在项目中遇到的问题. 由于一直以来,公司都是用的Linux服务器,这次为客户做项目,换成了winserver. 项目中有一项功能是Excel导出,采用PHPEXCEL类库实现.由于是个小项 ...
- PAT 1034. Head of a Gang[bug]
有一个两分的case出现段错误,真是没救了,估计是要写bfs的形式,可能栈溢出了 #include <cstdio> #include <cstdlib> #include & ...
- python 中str字符串 和list列表基本操作总结
s = '#afsfdf222#' print(s.capitalize()) # 首字母大写其他字母小写 print(s.center(20, '%')) # 居中并在空白处添加指定字符 print ...
- Apache Maven 3.5.0配置安装
1.maven 3.5 下载地址:http://maven.apache.org/download.cgi 2.下载了解压到 3.配置环境变量 4.测试看是否安装成功 5.maven配置(全局配置,用 ...
- .NET开源工作流RoadFlow-流程运行-管理员干预
在流程运行过程中管理员可以干预流程实例的走向,如管理加强制退回,指派和删除流程实例操作.在 流程管理-->实例管理 中查找到相应的流程实例,点击管理按钮即可管理该流程实例: 点击指派按钮,选择要 ...
- Java 之数组(4)
什么是数组: 问:编写代码保存 4 名学生的考试成绩. 答:简单啊,定义 4 个变量呗 问:那“计算全年级 400 名学生的考试成绩”,肿么办 答: ....... 数组,就可以帮助你妥妥的解决问题啦 ...