C - Roads in the North DFS+树的直径
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
题目大意:就是输入点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+树的直径的更多相关文章
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- 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 ...
- 转 蓝桥杯 历届试题 大臣的旅费 [ dfs 树的直径 ]
题解: 求树的直径. 转一篇博客:http://www.cnblogs.com/hanyulcf/archive/2010/10/23/tree_radius.html 树的直径是指树的最长简单路.求 ...
- codeforces 734E(DFS,树的直径(最长路))
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...
- P2610 【[ZJOI2012]旅游】(dfs+树的直径)
楼下那篇题解说实话就是什么都没说,所以我再发一篇正常一点的. 楼下思路大体是正确的,但是之所以是说什么都没说,是因为他有两个比较致命的遗漏.首先是点,这里的点不是平时我们认为的点,如果多少接触过对偶图 ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
随机推荐
- 使用 Vue.js 改写 React 的官方教程井字棋
React 的官方教程井字棋很好的引导初学者一步步走进 React 的世界,我想类似的教程对 Vue.js 的初学者应该也会有启发,于是使用 Vue.js 进行了改写 可以先查看最终的结果,尝试点击体 ...
- Contest 156
2019-09-29 14:56:09 总体感受:30min解决了前三题,最后一题其实也很简单,但是题目没有看清.赛后5min解决了.总体来说,本次周赛是比较简单的一次. 注意点:首先是hard问题的 ...
- 报错:Error instantiating class com.liwen.mybatis.bean.Employee with invalid types () or values ().
实体类默认构造方法是无参构造方法,一旦重写构造方法,默认方法就会变成重写之后的构造方法,所以该错误报的错就是实体类缺少无参构造方法
- 【笔记3-31】Python语言基础-序列sequence
序列sequence 可变序列 列表 list 字典 不可变序列 字符串 str 元祖 tuple 通过索引修改列表 del 删除元素 del my_list[2] 切片赋值只能是序列 .insert ...
- Xmind8 Pro破解版
思维导图又叫心智导图,是表达发散性思维的有效图形思维工具 ,它简单却又很有效,是一种实用性的思维工具.思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出来,把主题关键词与图像. ...
- 为何Keras中的CNN是有问题的,如何修复它们?
在训练了 50 个 epoch 之后,本文作者惊讶地发现模型什么都没学到,于是开始深挖背后的问题,并最终从恺明大神论文中得到的知识解决了问题. 上个星期我做了一些实验,用了在 CIFAR10 数据集上 ...
- CSS3动画的使用以及优化
CSS3 动画 目录 1. 定义动画2. animation 属性3. animation 属性的兼容4. animation与transition 属性的取别5. animate.css 动画库6. ...
- Java并发编程锁系列之ReentrantLock对象总结
Java并发编程锁系列之ReentrantLock对象总结 在Java并发编程中,根据不同维度来区分锁的话,锁可以分为十五种.ReentranckLock就是其中的多个分类. 本文主要内容:重入锁理解 ...
- __str_方法和__repr__的区别
__str__方法和__repr__方法: 官方文档解释: Object.__repr__(self): 由 repr() 内置函数调用以输出一个对象的“官方”字符串表示.如果可能,这应类似一个有效的 ...
- [HDU1029]Ignatius and the Princess IV<桶 水题>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...