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. VS2013+WDK8.1 驱动开发环境配置

    Windows Driver Kit 是一种完全集成的驱动程序开发工具包,它包含 WinDDK 用于测试 Windows 驱动器的可靠性和稳定性,本次实验使用的是 WDK8.1 驱动开发工具包,该工具 ...

  2. Nuget常用命令(转)

    转自:https://www.cnblogs.com/xcsn/p/6259853.html CMD将nuget升级到最新版本:nuget update -self 一.安装 1.安装指定版本类库in ...

  3. awr报告没有数据11.2.0.3

    有个朋友,反馈AWR没有数据: 咨询版本:oracle企业版本11.2.0.3 SQL> select * from v$version; BANNER -------------------- ...

  4. SpringCloud Hystrix 参数

    hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command PropertiesExecution ...

  5. ASP.NET Core 入门(3)(单元测试Xunit及Shouldly的使用)

    一.本篇简单介绍下在ASP.NET Core项目如何使用单元测试,例子是使用VS自带的Xunit来测试Web API接口,加上一款开源的断言工具Shouldly,方便写出更简洁.可读行更好的测试代码. ...

  6. 通过gpio控制一个进程开启或关闭

    目标: 板子上有个进程需要通过读取gpio的值, 当gpio值为1 时, 开启指定的进程,当gpio为0时, 杀掉这个指定的进程. #include <stdio.h> int main( ...

  7. Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager.

    react-native  0.60以上版本安装第三方库的时候会autolink  出现这个问题是 我安装 react-native-webview 之后运行 ios出现的,这是因为ios 没有自动安 ...

  8. S2-052

    前言 S2-052的RCE漏洞和以前的有些不同,不再是ognl表达式注入了,而是xml反序列化漏洞导致的RCE(另外还有S2-055漏洞是fastjson的反序列化漏洞).我复现的时候遇到一个坑,导致 ...

  9. 微服务、SOA、ESB比较

    很多时候会听到微服务.SOA.ESB之间有着联系也有着区别,有时候了解了一下,过段时间有混肴模糊了今天看了一篇文章写的很好,特地记录一下. 原文地址:https://mp.weixin.qq.com/ ...

  10. EF数据Linq方式查询

    using (var ctx = new NorthwindEntities()) { //单表查询SQL查询方式 //SELECT * FROM Customers AS c WHERE c.Cit ...