(树的直径)LightOJ -- 1094
链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/C
求树的直径,这里只不过给每条边增加一个边长属性,变成了求树上两点距离最远为多少
树的直径,在今天比赛前我是没有接触过的, 队友说在学长的博客上看是用了两个bfs就可以了,可我看的题解是用了两个dfs
应该意思是一样的吧!!! 都学习一下
题解的代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std; #define N 30005 struct node
{
int v, next, w;
}edge[N<<]; int Head[N], dis[N];
int cnt, Max, Index; void Add(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = Head[u];
Head[u] = cnt++;
} void dfs(int u, int w)
{
dis[u] = w; if(dis[u] > Max)
{
Max = dis[u];
Index = u;
} for(int i=Head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].v; if(dis[v] == -)
dfs(v, dis[u]+edge[i].w);
}
} int main()
{
int t, n, iCase=; scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(Head, -, sizeof(Head));
cnt = Max = ; for(int i=; i<n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Add(u, v, w);
Add(v, u, w);
} memset(dis, -, sizeof(dis));
dfs(, ); memset(dis, -, sizeof(dis));
dfs(Index, );
printf("Case %d: %d\n", iCase++, Max);
}
return ;
}
(树的直径)LightOJ -- 1094的更多相关文章
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- LightOJ 1094 - Farthest Nodes in a Tree
http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 【lightoj-1094】树的直径(DFS)
链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w, ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- VIJOS1476旅游规划[树形DP 树的直径]
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...
随机推荐
- 将Delphi的对象方法设为回调函数
心血来潮,为了实现更好的通用性和封装性,需要把类方法作为回调函数,搜得一篇好文,节选转发.命名似乎应该是MethodToCallback才合适,可惜调试时总是报错,debugging. 原文地址:ht ...
- R画散点图、线型图、箱型图、直方图基本知识
1.导入数据 2.散点图 plot(iris[,1]~iris[,4],xlab='Length',ylab='Width',col='red',main='Length VS Width')
- Analyzing Microarray Data with R
1) 熟悉CEL file 从 NCBI GEO (http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE24460)下载GSE24460. 将得到 ...
- edgeR
1)简介 edgeR作用对象是count文件,rows 代表基因,行代表文库,count代表的是比对到每个基因的reads数目.它主要关注的是差异表达分析,而不是定量基因表达水平. edgeR wor ...
- spring boot 配置 freemarker
1.springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 简单比较下两者不同, 1.1freemaker 优点 freemarker 不足:thym ...
- 第五章 二叉树(c)二叉树
- ios - 设置一个VC的navigationController的显示图案或文字,其他navigationController依旧不变
1. override func viewDidLoad() { super.viewDidLoad() self.navigationController?.delegate = self } 2. ...
- Mac 终端使用 - 加密 1. MD5 2.Base64
1.MD5 打开终端 输入 :echo -n "加密内容" |md5 md5加密网站https://md5jiami.51240.com/ md5解密网站http://www.cm ...
- 解决安装Apache中出现checking for APR... no configure: error: APR not found. Please read the documentation的问题
Linux中安装Apache 编译出现问题: 解决办法: 1.下载所需要的软件包 wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz wg ...
- 在windows系统下安装oracle 11g
oracle 11g 安装在windows server 2012 系统下. 最近,需要配置数据库,要求在windows操作系统下,安装oracle 11g 数据库,因为以前没有安装过,所以成功后, ...