Roads in the North(POJ 2631 DFS)
Description
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
Output
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)的更多相关文章
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- Roads in the North (树的直径)
Building and maintaining roads among communities in the far North is an expensive business. With thi ...
- Hopscotch(POJ 3050 DFS)
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2845 Accepted: 1995 Descrip ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- 深度搜索DFS-Lake Counting(POJ NO.2386)
题目链接POJ NO.2386 解题思路: 这个也是一个dfs 的应用,在书上的例子,因为书上的代码并不全,基本都是函数分块来写,通过这个题目也规范了代码,以后能用函数的就都用函数来实现吧.采用深度优 ...
- HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- [wikioi2144]砝码称重2(另类的dfs)
题目描述 Description 有n个砝码,现在要称一个质量为m的物体,请问最少需要挑出几个砝码来称? 注意一个砝码最多只能挑一次 输入描述 Input Description 第一行两个整数n和m ...
- 【BZOJ】1603: [Usaco2008 Oct]打谷机(水题+dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1603 这种水题... dfs没话说.. #include <cstdio> #inclu ...
随机推荐
- HttpAsyncClient 的简单使用
下载地址:http://hc.apache.org/downloads.cgi 在NetBeans中导入以下jar文件: 1:一次请求: public static void oneReuest(){ ...
- Ext4.0.7使用Ext.grid.ColumnModel报错:TypeError: Ext.grid.Model is not a constructor
代码如下: Ext.onReady(function(){ //定义列 var cm = new Ext.grid.ColumnModel([ {header: '编号', dataIndex: 'i ...
- wapPush
短信推送:wapPsuh简介
- statfs函数说明
函数: statfs 功能描述: 查询文件系统相关的信息. 用法: #include <sys/vfs.h> /* 或者 <sys/statfs.h> ...
- A站有一个页面需要PV统计 A站读写该数据 B站读该数据 需要数据同步
A站弄个缓存,并且开放出一个读取借口给B站 B站读取数据的时候,调用该接口和数据库内的数据累加,然后进行限时即可 ---------------------- 另外其他方法 session服务.mem ...
- Shortest Word Distance 解答
Question Given a list of words and two words word1 and word2, return the shortest distance between t ...
- Uva227.Puzzle
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- C 本地文件夸网文件Cp操作
1,linux平台C简单实现本地文件cp 码子及运行效果测试
- JUnit4中的测试套件
测试套件 JUnit3.8中,用测试套件同时运行多个测试类(http://www.cnblogs.com/mengdd/archive/2013/04/07/3006265.html). 在JUnit ...
- hdu 5063 Operation the Sequence(Bestcoder Round #13)
Operation the Sequence Time Limi ...