题目链接:http://poj.org/problem?id=2631

求树的直径模板。

定理:

树上任意一个点的在树上的最长路一定以树的直径的两端点其中一点结束。

做法:

两边bfs,第一次先找到node(树的直径的两端点其中一个),再一次求node的最长路所结束的点t

node—>t就是树的直径

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int n, dis[maxn], ans, node;
bool vis[maxn];
struct edge{
int from, to, next, len;
}e[maxn<<2];
int cnt, head[maxn];
void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].len = w;
e[cnt].to = v;
head[u] = cnt;
}
queue<int> q;
void bfs(int s)
{
vis[s] = 1;
q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(vis[e[i].to] == 0)
{
dis[e[i].to] = dis[now] + e[i].len;
vis[e[i].to] = 1;
q.push(e[i].to);
if(dis[e[i].to] > ans)
{
ans = dis[e[i].to];
node = e[i].to;
}
}
}
}
}
int main()
{
memset(head, -1, sizeof(head));
int u, v, w;
while(scanf("%d%d%d",&u, &v, &w) == 3)
{
add(u, v, w);
add(v, u, w);
}
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
ans = 0;
bfs(1); memset(vis, 0, sizeof(vis));
dis[node] = 0;
ans = 0;
bfs(node); printf("%d\n", ans);
return 0;
}

//Misaka_Azusa

//dsbdsb

【poj Roads in the North】 题解的更多相关文章

  1. [Poj] Roads in the North

    http://poj.org/problem?id=2631 树的直径裸题 dfs/bfs均可 /* dfs */ #include <iostream> #include <cst ...

  2. Roads in the North POJ - 2631

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

  3. poj 2631 Roads in the North

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

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

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

  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 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  8. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

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

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

随机推荐

  1. mycat安装

    一.简介 在互联网数据大爆炸时代,对数据处理的要求越来越高,很明显单实例数据库已经不能满足需求了,这时候数据库的中间件便应运而生.中间件聚集多实例来提供更强的处理能力,并且隐藏了多实例产生的复杂性.m ...

  2. pyhton基础

    python是一种什么语言?python是一种动态解释性的强类型定义的语言(1)编程语言分类 编译型: 把源程序的每一条语句都编译成机器语言,并保存成二进制文件, 这样运行时计算机可以直接以机器语言来 ...

  3. css手风琴

    <style> .box{ width: 1000px; height: 450px; margin:0 auto; overflow: hidden;} .box div{ width: ...

  4. android query 模糊查询

    package com.example.utils; import java.util.ArrayList; import android.content.ContentValues; import ...

  5. 任务九:使用HTML/CSS实现一个复杂页面

    任务目的 通过实现一个较为复杂的页面,加深对于HTML,CSS的实战能力 实践代码的复用.优化 任务描述 通过HTML及CSS实现设计稿 设计稿PSD文件(点击下载),效果如 效果图(点击打开) 整个 ...

  6. c++ inline 的位置不当导致的 无法解析的外部符号

    这几天编写代码碰到 无法解析的外部符号 visual studio. 在类中 inline 修饰符应该放在类函数定义的时候而不是声明的地方 即 // test.h 头文件 class A { publ ...

  7. 利用NVM在系统中维护多个版本的nodejs

      0. 背景 开发时可能同时进行多个项目,而这些项目所依赖的node版本又不是一样的.比如我现在的angular项目采用的node是8.9.3版本,而vue项目的vue-cli则依赖更高.由于ang ...

  8. tomcat + jdk 快速部署

    环境: apache-tomcat-7.0.73 java version "1.8.0_112" 注释:创建普通用户,使用 sudu进行操作,本文略写. 1.JDK 1.jdk配 ...

  9. 【Leetcode】【Medium】Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  10. Docker Toolbox在window 10 home 下挂载宿主机目录到容器的正确操作

    由于我是window 10 home 而不是 pro版本,只能使用Docker Toolbox,其实docker for window对windows的支持更好,还有GUI,home版本安装Docke ...