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. jacascript 基础数据类型(一)

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 数据类型有 number.boolean.string.object.null.undefined; un ...

  2. maven项目打包和编译跳过单元测试和javadoc

    代码中可能由于单元测试.注释(方法中的参数)或者maven javadoc插件的问题导致无法打包,影响工作,为避免这两种情况可以在打包时输入命令: mvn clean install -Dmaven. ...

  3. Element-ui-Basic

    一.Layout 布局 1.基础布局 <el-row> <el-col :span="24"><div class="grid-conten ...

  4. 解决ios中input兼容性问题

    1.解决input输入框在iOS中有阴影问题 input{ -webkit-appearance: none; } 2.checkbox.raido在ios中阴影问题 单选:              ...

  5. eclipse调试之edit source lookup path解决方案

    转自:https://blog.csdn.net/zkn_CS_DN_2013/article/details/48731133

  6. Go map使用

    前言 map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值. 在一个map里所有的键都是唯一的,而且必须是支持==和!=操作符的类型,切片.函数以及包含切片的 ...

  7. ios编程时常见问题总结

    (1)在UIViewController里面使用了timer,会使得controller被retain,因此在viewdisapper时应将timer置为nil,否则controller的deallo ...

  8. centos安装netcat工具及测试

    netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...

  9. 【python】python 自动发邮件

    一.一般发邮件的方法 Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件. 注意到构造MIMETEXT对象时,第一个参数就是邮件正文,第 ...

  10. Sharing is only supported for boot loader classes because bootstrap classpath has been appended

    在idea里面运行项目,terminal里面报“Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boo ...