C - Roads in the North DFS+树的直径

时间:2023-02-19 09:56:46
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
题目大意:就是输入点a,b以及a,b之间的距离,输出两点之间最长的距离
AC代码
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,l;
struct stu{
int a,b;
};
int mark[];
vector<stu>ve[];
int ans=;
int xx; void dfs(int x,int step){
if(step>ans){
ans=step;
xx=x;
}
for(int i=;i<ve[x].size();i++){
if(mark[ve[x][i].a]==){
mark[ve[x][i].a]=;
dfs(ve[x][i].a,step+ve[x][i].b);
mark[ve[x][i].a]=;
}
}
} int main()
{
while(scanf("%d %d %d",&n,&m,&l)!=EOF){//输入到文件结束,, 数据输入完成后要按Ctrl+z才可以输出
ve[n].push_back({m,l});
ve[m].push_back({n,l});//存图
} memset(mark,,sizeof(mark));
mark[]=;
dfs(,);
memset(mark,,sizeof(mark));
mark[xx]=;
dfs(xx,);
printf("%d",ans);
return ;
}
														
		

C - Roads in the North DFS+树的直径的更多相关文章

  1. poj 2631 Roads in the North【树的直径裸题】

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

  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. 转 蓝桥杯 历届试题 大臣的旅费 &lbrack; dfs 树的直径 &rsqb;

    题解: 求树的直径. 转一篇博客:http://www.cnblogs.com/hanyulcf/archive/2010/10/23/tree_radius.html 树的直径是指树的最长简单路.求 ...

  5. codeforces 734E(DFS&comma;树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  6. P2610 【&lbrack;ZJOI2012&rsqb;旅游】(dfs&plus;树的直径)

    楼下那篇题解说实话就是什么都没说,所以我再发一篇正常一点的. 楼下思路大体是正确的,但是之所以是说什么都没说,是因为他有两个比较致命的遗漏.首先是点,这里的点不是平时我们认为的点,如果多少接触过对偶图 ...

  7. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  8. POJ 2631 Roads in the North&lpar;树的直径&rpar;

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

  9. poj 2631 Roads in the North (*树的直径)

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

随机推荐

  1. html学习第三天—— 第13&comma;14章

    颜色值缩写 关于颜色的css样式也是可以缩写的,当你设置的颜色是16进制的色彩值时,如果每两位的值相同,可以缩写一半. 例子1: p{color:#000000;} 可以缩写为: p{color: # ...

  2. JavaWeb学习总结(十一)--JDBC之批处理

    一.批处理的介绍 在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率.批处理只针对更新(增.删.改)语句,批 ...

  3. UVa 12325 Zombie&&num;39&semi;s Treasure Chest【暴力】

    题意:和上次的cf的ZeptoLab的C一样,是紫书的例题7-11 不过在uva上交的时候,用%I64d交的话是wa,直接cout就好了 #include<iostream> #inclu ...

  4. PHP7 网络编程(五)进程间通信【待】

    https://blog.csdn.net/godleading/article/details/78391159

  5. 《Python编程》课程报告 python技术在数据分析中的应用之网络爬虫

      摘要:... 2 1       引言 :... 2 1.1课题研究背景和研究现状... 2 1.1.1课题背景和目的... 3 1.1.2研究现状... 4 1.1.2.1语言... 4 1.1 ...

  6. &lbrack;agc011E&rsqb;Increasing Numbers-&lbrack;思考题&rsqb;

    Description 传送门 Solution 依题得所有不下降数(设为a)可以拆为若干个全1数的和(如:1558=1111+111+111+111+111+1+1+1) 并且任意a所能拆出的全一数 ...

  7. Linux进程间通信&colon;管道&comma;信号量&comma;消息队列&comma;信号&comma;共享内存&comma;套接字

    Linux下的进程通信手段基本上是从UNIX平台上的进程通信手段继承而来的.而对UNIX发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...

  8. Linux 监测磁盘常用的工具sar iostat vmstat

    Linux 检测内存常用的工具sar iostat vmstat #每秒刷新一次显示2次 sar -d 1 2 iostat -kx 1 2 vmstat -d 1 2 磁盘统计信息解释 tps 每秒 ...

  9. C&num;实现动态编译代码

    /*------------------------------------------------------------------------------ * Copyright (C) 201 ...

  10. 使用YCSB测试mongodb

    项目里面需要对mongodb的性能进行测试,看了下网上很多做法都是使用YCSB进行测试,因此开始学习使用YCSB. 参考资料: YCSB github地址:https://github.com/bri ...