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 ...
随机推荐
- 扯一扯基于4046系IC的锁相电路设计
4046系IC(下简称4046),包括最常见的CD4046(HEF4046),可以工作在更高频的74(V)HC4046,以及冷门而且巨难买到的74HC(T)7046和74HCT904 ...
- Contest 158
2019-10-14 15:30:38 总体感受:这次依然很快搞定了前三题,最后一题乍看之下还是比较简单的,但是出奇多的corner case让我非常苦恼,这也让我意识到要想真正征服最后一题,还有一个 ...
- eclipse-JEE配置Tomcat并发布第一个项目
一.配置过程 Window--preferences--Server--Runtime Environment, 然后点击add 我下载的是Tomcat7.0,选择你的版本就行了 选择Tomcat的安 ...
- 展示html/javascript/css------Live-Server
Live-server简介 这是一款带有热加载功能的小型开发服务器.用它来展示你的HTML / JavaScript / CSS,但不能用于部署最终的网站. 官网地址:https://www.npmj ...
- 关于swift使用CocoaPods倒入三方库的framework后父类倒入子类无法继承的问题
今天开发项目的时候遇到这么一个问题在使用cocoapods倒入了三方库后我在BaseController中倒入三方库,其余controller继承自basecontroller,然而在继承的子类中无法 ...
- Ruby学习计划-(0)前言
前言 接触过的编程语言现在来说有C.C++.Java.Object-C.Swift.这几门语言中最熟悉的就是oc了,毕竟靠他吃饭,对于其他几门语言都是初入门径而已,由于本身一直从事iPhone ...
- vscode 保存自动 格式化eslint 代码
在网上找了很多种方法,大多都没有成功 一下是一种成功的 配置方法: 1) First, you need to install the ESLint extension in the VS code ...
- 1012 The Best Rank (25 分)
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- Mysql索引、explain执行计划
1.索引的使用场景 哪些情况使用索引: 1.主键自动建立唯一索引 2.频繁作为查询条件的字段应该创建索引 where 3.多表关联查询中,关联字段应该创建索引on两边都要创建索引 select * f ...
- MongoDB查询mgov2的聚合方法
1.多条表数据累计相加. respCount := struct { Rebatescore int64 //变量命名必须要和查询的参数一样.}{} o := bson.M{"$match& ...