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的更多相关文章

  1. 【poj Roads in the North】 题解

    题目链接:http://poj.org/problem?id=2631 求树的直径模板. 定理: 树上任意一个点的在树上的最长路一定以树的直径的两端点其中一点结束. 做法: 两边bfs,第一次先找到n ...

  2. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  3. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  4. Roads in the North POJ - 2631

    Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...

  5. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  6. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  7. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  8. Roads in the North(POJ 2631 DFS)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  9. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

随机推荐

  1. go 构造切片slice

    定义切片 make([]int, 5)  长度和容量均为5 make([]int, 0, 5) 长度为0 容量为0 切片 slice2[3:5] 对slice2进行切片返回 第3 4 两个元素 不包含 ...

  2. shell习题第26题:监控mysql服务

    [题目要求] 假设mysql密码是123456. 写脚本监控mysql服务是否正常,比如是否可以执行show processlist,并检测一下当前的mysql服务是主还是从.如果是从,请判断他的主从 ...

  3. POSIX异步I/O接口使用

    POSIX1003.1b 实时扩展协议规定的标准异步 I/O 接口,即 aio_read 函数. aio_write 函数.aio_fsync 函数.aio_cancel 函数.aio_error 函 ...

  4. (六)lucene之其他查询方式(组合查询,制定数字范围、指定字符串开头)

    本章使用的是lucene5.3.0 指定数字范围查询 package com.shyroke.test; import java.io.IOException; import java.nio.fil ...

  5. .NET Core 常用第三方包

    .NET Core 常用第三方包 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10845370.html 写在前面 最近在学习.NET Core 中经常用 ...

  6. WIN10升级失败,故障代码 0X8007042B-0X4000D 解决

    个人博客链接:WIN10升级失败,故障代码 0X8007042B-0X4000D 解决

  7. 整屏纵向切换超出整屏内容纵向滚动 解决办法-fullpage

    这个问题我也是研究了好久,百度了很多办法,swiper我始终没有找到合适的解决办法,所以我放弃了swiper,改用fullpage. fullpage里面有个scrollOverflow的属性: 并且 ...

  8. 苹果发布app,上传ipa,不显示问题

    用Xcode或者leader上传ipa,提示是上传成功,但是在网页上不显示构建版本.如下图: 那么,你先点击“活动”,进去后,如果显示你的app正在审核,那么表示上传成功,等待:如果“活动”中不显示你 ...

  9. MUI顶部导航布局

    一.头部 核心css mui-bar mui-bar-nav <header class="mui-bar mui-bar-nav"> <a class=&quo ...

  10. 如何使用Visual Studio Code调试PHP CLI应用和Web应用

    在按照Jerry的公众号文章 什么?在SAP中国研究院里还需要会PHP开发? 进行XDebug在本地的配置之后,如果想使用Visual Studio Code而不是Eclipse来调试PHP应用,步骤 ...