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. nyoj-366-D的小L(求全排列)

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡 ...

  2. GestureDetector和SimpleOnGestureListener的使用教程

    1. 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vi ...

  3. 微信授权登陆接入第三方App(步骤总结)Android

    微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录

  4. 5.1.1 读取Redis 数据

    Redis 服务器是Logstash 推荐的Broker选择,Broker 角色就意味会同时存在输入和输出两个插件. 5.1.1 读取Redis 数据 LogStash::Input::Redis 支 ...

  5. hdu4536-XCOM Enemy Unknown(爆搜)

    XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...

  6. Android使用bindService启动服务

    1.Service package com.example.ebobo; import java.util.Timer; import java.util.TimerTask; import andr ...

  7. Unity 安卓下DLL热更新一(核心思想)

    大家都知道一谈起热更新的话首选是Ulua这个插件, 其实Unity可以使用dll热更新的,如果你实在不想用Lua来编写逻辑,0.0请下看Dll+AssetBundle如何实现热更新的.让你看完这个文章 ...

  8. 在OC和Swift中使用IBDesignable/IBInspectable

    iOS8新特性IBDesignable/IBInspectable,可以直接在XIB或者Storyboard中直接,设置UI类的属性.例 如:UIView.layer.borderWidth.bord ...

  9. Oracle中drop user和drop user cascade的区别

    drop user : 仅仅是删除用户,drop user username cascade :会删除此用户名下的所有表和视图. userSpecify the user to be dropped. ...

  10. C#中方法Show.和ShowDialog的使用区别

    show()是非模式窗体. showDialog()是模式窗体. 如果这个时候用Show的话,则会发生的事情是,打开子窗体的同时主窗体又显示出来,而使用ShowDialog()的时候主要当子窗体关闭的 ...