[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 ...
随机推荐
- 阿里云kubernetes集群被xmrig挖矿程序入侵
原因是由于Kubernetes Apiserver不安全配置所致,Apiserver提供了资源操作的唯一入口,并提供认证.授权.访问控制.API注册和发现等机制,所以apiserver的安全至关重要. ...
- 怎样绑定this
有三种方法: 1. Function.prototype.call(); 2. Function.prototype.apply(); 3. Function.prototype.bind(); ...
- (java实现)双向循环链表
什么是双向循环链表 在了解双向循环链表之前,如果对链表还没有一个清晰的概念,建议你看看单链表和单向循环链表,这有利于你更好的理解下面的内容.(废话有点多[逃] 相比单链表,双向循环链表是一个更加复杂的 ...
- mybaits实现oracle批量新增数据,回填主键
项目有需求,百度了很久,反正他们说的方法,我都没成功,我也不知道是不是我写代码的姿势不正确,没办法只能自己想法子了 我们这个项目用到了通过Mapper,通用Mapper里通过OracleProvide ...
- 开发环境,不用每次都ant自动编译
公司所用ant技术,每次改个java文件,配置文件都需要重新编译一次发布 在实际搭环境的过程发现,ant就是把项目目录下的文件编译成功后的搬移到到 ,Tomcat 运行环境配置的目录下,凡是修改的文件 ...
- c#基础知识梳理(五)
上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10831189.html 一.运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户 ...
- Django迁移
Django迁移 学习目标: 如何在不编写任何SQL语句的情况下创建数据库表 更改model如何自动修改数据库 如何还原对数据库所做的更改 迁移能解决的问题 如果你不了解Django或者WEB开发,肯 ...
- Redis 测试 数据类型
- 改写Unity DropDown 支持多次点击同一选项均回调
[很久前的一个Note,不知道现在的Unity Dropdown是否已经支持该特性] Unity UGUI是开源的: https://bitbucket.org/Unity-Technologies/ ...
- JS实现数组去重(重复元素保留一个)
1.遍历数组法 它是最简单的数组去重方法(indexOf方法) 实现思路:新建一个数组,遍历去要重的数组,当值不在新数组的时候(indexOf为-1)就加入该新数组中: var arr=[2,8,5, ...