E - Farthest Nodes in a Tree
Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000)denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.
Output
For each case, print the case number and the maximum distance.
Sample Input
2
4
0 1 20
1 2 30
2 3 50
5
0 2 20
2 1 10
0 3 29
0 4 50
Sample Output
Case 1: 100
Case 2: 80
题目大意:输入A,B,点以及a点b点之间的距离输出,,两点最远的距离
AC代码:
#include<cstdio>
#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
struct stu{
int y,s;
};
vector<stu>ve[];
int ans=;
int xx;
int mark[];
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].y]==){
mark[ve[x][i].y]=;
dfs(ve[x][i].y,step+ve[x][i].s);
mark[ve[x][i].y]=;
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int j=;j<=t;j++){
int a,b,c,n;
ans=;
scanf("%d",&n); for(int i=;i<n-;i++){
scanf("%d %d %d",&a,&b,&c);
ve[a].push_back({b,c});
ve[b].push_back({a,c});
} memset(mark,,sizeof(mark));
mark[]=;
dfs(,);
memset(mark,,sizeof(mark));
mark[xx]=;
dfs(xx,);
printf("Case %d: %d\n",j,ans);
for(int i=;i<n;i++){
ve[i].clear();
}
}
return ;
}
E - Farthest Nodes in a Tree的更多相关文章
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)
题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
随机推荐
- 【面试QA】Attention
目录 Attention机制的原理 Attention机制的类别 双向注意力 Self-Attention 与 Soft-Attention 的区别 Transformer Multi-Head At ...
- Qt 的信号与槽(纯干货)
接触Qt断断续续有些时间了,总看了一堆的文章说信号槽的概念,心里就想骂人,做为一个初学者,最重要的就是怎么写代码,写代码写多了,再去看理论,有时水到渠成的就明白那些理论了.但所有讲信号槽的都把一堆信号 ...
- 力软敏捷框架7.0.6 葡萄城报表升级到ar14版本
忙了两天终于搞定升级到ar14版本,坑无数,终于算全部解决,在这里做一个小结. 1.第一步去掉框架中原本集成的ar13部分(吐槽一下应该是对12的集成). 首先去掉licenses.licx文件. 然 ...
- 汇编学习二-VB(常见函数分析)
VB代码如下所示 push ebp 00401FF1 . 8BEC mov ebp,esp 00401FF3 . 83EC 0C sub esp,0xC push <jmp.&MSVBV ...
- 2019级第一次月赛暨ACM工作室第一次招新赛、补题赛
A:最简单签到,没有之一 Description 此题简单如题意,就是求最大值 Input 多组输入 每组输入输入一串字符串(包括字母和数字),长度小于500 Output 每行输出字符ASCII值与 ...
- 10年阿里自动化测试架构师帮您收集的:git常用命令大全以及git原理图【泣血推荐,建议收藏】
一.Git分布式版本控制简介 Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势.本来想着只把最有用.最常用的 Git 命令记下来, ...
- python函数的传参模式
python里的变量更像是一个名字.标签.而Python中一切又皆为对象. 当函数传参时,函数参数作为一个标签,指向某个对象,因此更贴切的说是"call by object". 但 ...
- 编译安装inotify-tools和监控inotifywait事件
编译安装inotify-tools软件包 1)解包inotify-tools-3.13.tar.gz文件 [root@svr7~]#ls inotify-tools- ...
- Centos下载新版内核
下载新版内核的安装文 ...
- usdt钱包对接,usdt 对接交易平台,usdtapi,以太坊对接,以太坊代币对接
usdt钱包对接,usdt 对接交易平台,usdtapi,以太坊对接,以太坊代币对接 自动充提币接口开发. 可对接:商城系统,游戏APP,交易平台,网站,各类APP -实现自动充提,查询,上链等功能接 ...