Description

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.

The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.

Input

Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.

Output

You are to output a single integer: the road distance between the two most remote villages in the area.

Sample Input

5 1 6
1 4 5
6 3 9
2 6 8
6 1 7

Sample Output

22
给定一颗棵树,求最长路径
思路:随机找一个节点u,DFS求出u的最远点m,然后再DFS求出m的最远点n,之后m-n就是最长路径 2.还可以用DP写,d(i)表示以i为根节点的最大路径值,=max(d(j)+1),j为i的子节点,取出最大和次大的d(j) +2;
 #include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#define Max 10005
using namespace std;
vector <int> tree[Max],len[Max];
int d[Max],vis[Max];
bool flag=;
int an;
int dfs(int node,int &ans)
{
int i,j;
int maxn=;
int t,index;
vis[node]=;
for(i=;i<tree[node].size();i++)
{
if(vis[tree[node][i]])
continue;
dfs(tree[node][i],t);
if((t+len[node][i])>maxn)
{
// cout<<t<<endl;
maxn=t+len[node][i];
index=i;
}
}
ans=maxn;
return i;
}
void dfs1(int node,int sum)
{
int i;
vis[node]=;
if(flag)
return;
if(sum==)
{
flag=;
an=node;
return;
}
for(i=;i<tree[node].size();i++)
{
if(sum>=len[node][i]&&vis[tree[node][i]]==)
dfs1(tree[node][i],sum-len[node][i]);
}
}
int main()
{
int i,j;
int a,b,val,p=;
freopen("in.txt","r",stdin);
bool flag=;
for(i=;i<Max;i++)
tree[i].clear(),len[i].clear();
while(scanf("%d%d%d",&a,&b,&val)!=EOF)
{
tree[a].push_back(b);
tree[b].push_back(a);
len[a].push_back(val);
len[b].push_back(val);
}
memset(vis,,sizeof(vis));
dfs(,p);
memset(vis,,sizeof(vis));
dfs1(,p);
memset(vis,,sizeof(vis));
dfs(an,p);
cout<<p<<endl;
}
												

Roads in the North(POJ 2631 DFS)的更多相关文章

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

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

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

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

  3. Roads in the North (树的直径)

    Building and maintaining roads among communities in the far North is an expensive business. With thi ...

  4. Hopscotch(POJ 3050 DFS)

    Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2845   Accepted: 1995 Descrip ...

  5. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  6. 深度搜索DFS-Lake Counting(POJ NO.2386)

    题目链接POJ NO.2386 解题思路: 这个也是一个dfs 的应用,在书上的例子,因为书上的代码并不全,基本都是函数分块来写,通过这个题目也规范了代码,以后能用函数的就都用函数来实现吧.采用深度优 ...

  7. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. [wikioi2144]砝码称重2(另类的dfs)

    题目描述 Description 有n个砝码,现在要称一个质量为m的物体,请问最少需要挑出几个砝码来称? 注意一个砝码最多只能挑一次 输入描述 Input Description 第一行两个整数n和m ...

  9. 【BZOJ】1603: [Usaco2008 Oct]打谷机(水题+dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1603 这种水题... dfs没话说.. #include <cstdio> #inclu ...

随机推荐

  1. java基础总结——数组

    数组需要掌握的: 1.数组的定义 2.数组的内存分配及特点 3.数组操作常见问题 4.数组常见操作 5.数组中的数组(理解) 数组唯一属性:length,即数组的长度. 1.数组定义 格式一: 元素类 ...

  2. CoreData (四)备

    监听NSFetchedResultsController 之前说过, NSFetchedResultsController是有两个重要的功能. 第一:NSFetchedResultsControlle ...

  3. awk详解

    一.简介 强大的文本分析工具,基于指定规则浏览和抽取信息.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理.awk有3个不同版本: awk.nawk和ga ...

  4. HashMap非线程安全分析

    通过各方资料了解,HashMap不是线程安全的,但是为什么不是线程安全的,在什么情况下会出现问题呢? 1. 下面对HashMap做一个实验,两个线程,并发写入不同的值,key和value相同,最后再看 ...

  5. Populating Next Right Pointers in Each Node II 解答

    Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...

  6. Increasing/ Decreasing Stack

    对于此类问题: 对于元素nums[i],找出往左/右走第一个比它小/大的数字 我们常常用递增栈/递减栈实现. 递增栈实现第一个比它小 递减栈实现第一个比它大 Example: 2 1 5 6 2 3 ...

  7. C#中,表达式的计算遵循一个规律:从左到右依次计算。

    int i = 0; int j = (i++)+(i++)=(i++)+i=i+++i=i+++i++=1;

  8. TCP和UDP的区别(转)

    TCP协议与UDP协议的区别    首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UDP协议的区别,我觉得这是没有从本质上弄清楚网络通信! ...

  9. 表单验证插件jquery.validate的使用方法演示

    jQueryValidate表单验证效果 jquery.validate验证错误信息的样式控制 <!--validate验证插件的基础样式--> input.error{border: 1 ...

  10. Top 15 Tools To Make Animated GIFs From Images & Video

    Creating an animated GIF picture from photos or video with Adobe Photoshop is easy, but not everyone ...