[Poj] Roads in the North
http://poj.org/problem?id=2631
树的直径裸题
dfs/bfs均可
/*
dfs
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std;
const int N = 1e5 + ; #define yxy getchar() int now = , point, Max_dis;
int dis[N], head[N];
bool vis[N];
struct Node {int v, w, nxt;} G[N << ]; inline void add(int u, int v, int w){
G[now].v = v; G[now].w = w; G[now].nxt = head[u]; head[u] = now ++;
} void dfs(int u, int dist){
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(!vis[v]){
dis[v] = dist + G[i].w;
vis[v] = ;
if(dis[v] > Max_dis){
Max_dis = dis[v];
point = v;
}
dfs(v, dis[v]);
}
}
} int main()
{
memset(head, -, sizeof(head));
int u_, v_, w_;
while(scanf("%d%d%d", &u_, &v_, &w_) == ){
add(u_, v_, w_); add(v_, u_, w_);
}
vis[] = ;
dfs(, );
Max_dis = ;
memset(vis, , sizeof(vis));
vis[point] = ;
dfs(point, );
cout << Max_dis;
return ;
}
/*
bfs
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue> using namespace std;
const int N = 1e5 + ; #define yxy getchar() int now = , point, Max_dis;
int dis[N], head[N];
bool vis[N];
struct Node {int v, w, nxt;} G[N << ];
queue <int> Q; inline void add(int u, int v, int w){
G[now].v = v; G[now].w = w; G[now].nxt = head[u]; head[u] = now ++;
} void bfs(int S){
Q.push(S);
vis[S] = ;
while(!Q.empty()){
int topp = Q.front();
Q.pop();
for(int i = head[topp]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(!vis[v]){
vis[v] = ;
dis[v] = dis[topp] + G[i].w;
Q.push(v);
if(dis[v] > Max_dis){
Max_dis = dis[v];
point = v;
}
}
}
}
} int main()
{
memset(head, -, sizeof(head));
int u_, v_, w_;
while(scanf("%d%d%d", &u_, &v_, &w_) == ){
add(u_, v_, w_); add(v_, u_, w_);
}
bfs();
Max_dis = ;
memset(vis, , sizeof(vis));
memset(dis, , sizeof(dis));
bfs(point);
cout << Max_dis;
return ;
}
[Poj] Roads in the North的更多相关文章
- 【poj Roads in the North】 题解
题目链接:http://poj.org/problem?id=2631 求树的直径模板. 定理: 树上任意一个点的在树上的最长路一定以树的直径的两端点其中一点结束. 做法: 两边bfs,第一次先找到n ...
- 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 ...
- 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【树的直径裸题】
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 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- Roads in the North(POJ 2631 DFS)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
随机推荐
- SSH框架结合案例构建配置
ssh框架概述 SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架.区别于 Secure Shell . 集成SSH框架的系统从职责上分 ...
- Hibernate一对多自关联、多对多关联
今天分享hibernate框架的两个关联关系 多对多关系注意事项 一定要定义一个主控方 多对多删除 主控方直接删除 被控方先通过主控方解除多对多关系,再删除被控方 禁用级联删除 关联关系编辑,不 ...
- MySQL的explain语句分析
+----+-------------+-------+------------+------+---------------+-----+---------+------+------+------ ...
- tiny-Spring【2】逐步step分析-新加入特性
tiny-Spring是黄亿华大佬自己写的一个集合IOC和AOP于一身的一种轻量级[教学用]Spring框架,它的github库地址为:https://github.com/code4craft/ti ...
- C语言并查集例子——图问题巧用parent[]数组
输入:测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M:随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城 ...
- Java Web 深入分析(4) Java IO 深入分析
I/O问题可以说是现在海量数据时代下 ,I/O大部分web系统的瓶颈.我们要了解的java I/O(后面简称为(IO)) IO类库的基本结构 磁盘IO的工作机制 网络IO的工作机制 NIO的工作方式 ...
- opencv-04--图像金字塔
图像金字塔被广泛应用于各种视觉应用中.图像金字塔是一个图像集合,集合中图像都源于同一个原始图像,而且是通过对原始图像连续降采样获得,直到达到某个中止条件才停止降采样.(当然,降为一个像素肯定是中止条件 ...
- centos系统基本操作命令
系统相关命令 查看系统版本: cat /etc/centos-release 系统更新: yum update 用户相关命令 增加用户: useradd [用户名] 设置密码:password ...
- UIApplicationDelegate里面最常用的几个函数执行顺序小结
(1)点击桌面图标正常启动App或者杀死进程后点击推送消息启动App 1.application:willFinishLaunchingWithOptions 2.application:applic ...
- fiddle--APP弱网测试
一.安装Fiddler 网上说要先安装.NET Framwork4,应该是由于本机已装,所以在安装Fiddler时并没有相关提示. Fiddler安装包:https://www.telerik.com ...