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

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

  5. codeforces 734E(DFS,树的直径(最长路))

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

  6. P2610 【[ZJOI2012]旅游】(dfs+树的直径)

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

  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(树的直径)

    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. python-神奇的下划线

    2019-12-16 22:45:29 python中下划线有各种各样的作用,本章就来分别介绍一下各种下划线的功能. 一.开头单下划线 _VAR 开头单下划线还是挺常用的,在类中表示为保护变量/保护函 ...

  2. selenium 使用教程详解-java版本

    第一章 Selenium 概述 1.1.Selenium 发展史 ​ Selenium是一系列基于Web的自动化工具,提供一套测试函数,用于支持Web自动化测试.函数非常灵活,能够完成界面元素定位.窗 ...

  3. Spring02——Spring 中 Bean 的生命周期及其作用域

    在前一篇文章中,我们已经介绍了 Spring IOC 的相关知识,今天将为个位介绍 Spring 中 Bean 的相关知识.关注我的公众号「Java面典」,每天 10:24 和你一起了解更多 Java ...

  4. 120prop-python3.7 读写.properties文件

    120prop-python3.7 读写.properties文件 转载 nature_ph 最后发布于2019-07-30 10:12:05 阅读数 229 收藏 发布于2019-07-30 10: ...

  5. 百道Python面试题实现,搞定Python编程就靠它

    对于一般的机器学习求职者而言,最基础的就是掌握 Python 编程技巧,随后才是相关算法或知识点的掌握.在这篇文章中,我们将介绍一个 Python 练习题项目,它从算法练习题到机试实战题提供了众多问题 ...

  6. ORA-01017的一种情况:sysdba可以登录,normal不可登录

        在arcCatalog中创建完sde数据库之后,用PLSQL登录提示只能用SYSDBA登录. 用户名:sde 密码:123456 数据库:ORCLZLL 连接为:Normal     点击登录 ...

  7. Google浏览器截取整个网页

    ~Ctrl+shift+i(开发者工具) ~Ctrl+shift+p ~输入full  

  8. Java系列之内部类

    今天温习一下 Java 中的内部类,内部类一般有如下几种:静态内部类.成员内部类.匿名内部类和方法内部类,下文中将主要介绍静态内部类和成员内部类,主要内容如下: 概述 静态内部类 成员内部类 匿名内部 ...

  9. (25+4/25+4)复健-KMP/EKMP/manache/Trie

    (29/29) 3.23已完成  1.KMP int Next[maxn]; void prekmp(char* x,int len){ ,suf=; Next[]=-; while(suf<l ...

  10. 痞子衡嵌入式:走进二维码(QR Code)的世界(1)- 引言

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是走进二维码(QR Code)的世界专题的引言. 如今二维码可以说是深入走进大家的生活了,推送名片.扫码支付都离不开它,大家几乎每天都会和 ...