树的直径 poj 2631
树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <deque>
#include <vector>
using namespace std;
const int maxn = 10008;
const int inf = 0x3ffffff;
struct Node {
int w, next,to;
Node (int x = 0, int y = 0,int u = 0 ){
to = x;w = y;next = u;
}
}e[maxn*4];
int d[maxn],head[maxn],tot;
void add(int u,int v,int w){
e[tot] = Node(v,w,head[u]);
head[u] = tot++;
e[tot] = Node(u,w,head[v]);
head[v] = tot++;
}
int BFS(int u) {
memset(d,-1,sizeof(d));
d[u] = 0;
int max_int = 0,id = u;
queue <int> q;
q.push(u);
while(!q.empty()){
u = q.front();q.pop();
if(d[u] > max_int) {
max_int = d[u];
id = u;
} for(int i=head[u];i!=-1;i=e[i].next){
if(d[e[i].to] == -1){
d[e[i].to] = d[u] + e[i].w;
q.push(e[i].to);
}
}
}
// cout << id <<endl;
return id;
}
int main(){
int u,v,w; //freopen("input.txt","r",stdin);
memset(head,-1,sizeof(head));
tot = 0;
while(scanf("%d%d%d",&u,&v,&w) == 3)add(u,v,w);
printf("%d\n",d[BFS(BFS(u))]);
return 0;
}
树的直径 poj 2631的更多相关文章
- I - 树的直径 POJ - 1383
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- 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 (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
随机推荐
- 人物角色群体攻击判定(三)Physics.OverlapSphere(群体攻击)
使用Physics.OverlapSphere来检测不方便调试, 其他都可以. 核心代码: //检测敌人 public void CheckEnemy() { Collider[] cols = ...
- POJ 1300 欧拉通路&欧拉回路
系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...
- CRC 模式及实现
CRC : Cyclic redundancy Check 循环冗余校验 概述参见wiki百科:http://en.wikipedia.org/wiki/Cyclic_redundancy_check ...
- vs2013中国集
在TOOLS的菜单条下的最后一项.进去后在输入框输入Language.按Enter.选择语言,然后确定就可以. 如图 假设没有点击下拉框底下的链接 就会调挑转到语言包下载界面 下载须要的语言就可以, ...
- Codeforces 474C Captain Marmot 给定4个点和各自旋转中心 问旋转成正方形的次数
题目链接:点击打开链接 题意: 给定T表示case数 以下4行是一个case 每行2个点,u v 每次u能够绕着v逆时针转90° 问最少操作多少次使得4个u构成一个正方形. 思路: 枚举判可行 #in ...
- Android应用切换皮肤功能实现(二)
原文地址http://www.apkbus.com/forum.php?mod=viewthread&tid=149034&highlight=%E7%9A%AE%E8%82%A4 上 ...
- ora-24247:网络访问被访问控制列表(ACL)拒绝
用dba账户使用下面脚本授予报错账户访问外部网络服务的权限,以SCOTT为例: BEGIN -- Only uncomment the following line if ACL "netw ...
- css3实现手机菜单展开收起动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- jsp页面获取服务器时间
Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MON ...
- 使用xml及java代码混合的方式来设置图形界面
参考<疯狂android讲义>第2版2.1节 设置android的图形界面有三种方法: 1.使用纯xml文件 2.使用纯java,代码臃肿复杂,不建议使用 3.使用xml与java混合,前 ...