lightoj1094 - Farthest Nodes in a Tree
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for 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 |
Case 1: 100 Case 2: 80 |
Notes
Dataset is huge, use faster i/o methods.
题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点index,然后找离index最远的点就是最大距离?
假定0为根节点找离0最远的点就是,最深的点index,然后找离index最远的点就是树上最远的距离
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define N 30008 struct node
{
int v, w, next;
}e[N*]; int n, cnt, maxx;
int Index; // 写成index过不了lightoj=·=||
int head[N], dist[N]; void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void dfs(int u, int w)
{
dist[u] = w;
if(w > maxx)
{
maxx = dist[u];
Index = u;
}
for(int i = head[u]; i != -; i = e[i].next)
{
if(dist[e[i].v] == -)
{
dfs(e[i].v, dist[u]+e[i].w);
}
}
}
int main()
{
int t, u, v, w, k = ; scanf("%d", &t); while(t--)
{
cnt = maxx = ;
memset(head, -, sizeof(head)); scanf("%d", &n);
n--;
while(n--)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
memset(dist, -, sizeof(dist));
dfs(, );
memset(dist, -, sizeof(dist));
dfs(Index, );
printf("Case %d: %d\n", k++, maxx);
}
return ;
}
bfs
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define N 30008 struct node
{
int v, w, next;
} e[N*]; int n, cnt, maxx;
int Index;
int head[N], dist[N], vis[N]; void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void bfs(int u)
{
memset(vis, , sizeof(vis));
queue<int> Q;
Q.push(u);
vis[u] = ;
dist[u] = ; while(Q.size())
{
u = Q.front();
Q.pop(); for(int i = head[u]; i != -; i = e[i].next)
{
int v = e[i].v;
if(!vis[v])
{
vis[v] = ;
dist[v] = dist[u]+e[i].w;
if(dist[v] > maxx)
{
maxx = dist[v];
Index = v; }
Q.push(v);
}
}
}
} int main()
{
int t, u, v, w, k = ;
scanf("%d", &t); while(t--)
{
maxx = cnt = ;
memset(head, -,sizeof(head)); scanf("%d", &n);
n--; while(n--)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
memset(dist, , sizeof(dist));
bfs();
bfs(Index);
printf("Case %d: %d\n", k++, maxx);
}
return ;
}
lightoj1094 - Farthest Nodes in a Tree的更多相关文章
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- 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 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- 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. Th ...
- 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 ...
随机推荐
- KETTLE——初见KETTLE
(PS:这是很早以前在CSDN上发过的,那个账号不想用了,所以搬过来) 就在前一段时间,因为公司需要突然被老大告知要用一个ETL工具,第一次知道这么个工具,完全不知道是做什么的.大概问了一下,说是一种 ...
- git.ZC_命令积累
1.删除文件 git rm 想要删除的文件的名字及其后缀 git commit -m "对本次提交的描述信息" git push 删除文件夹,执行命令: git rm 想要删除的文 ...
- Java多态性应用——多态数组、多态参数
多态数组: Person[] person = {new Person("张三", 32), new Student("李四", 21, 120, 90.0), ...
- [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)
[BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...
- 偏序问题及CDQ分治详解
CDQ用来解决分治时左半部分对右半部分造成影响的问题. CDQ分治的经典问题是三维偏序问题. 要想解决三维偏序问题,首先你要知道什么是偏序.(废话) 一维偏序: 给出直线上的n个点,问有多少对点满足x ...
- 2019 Multi-University Training Contest 2 - 1008 - Harmonious Army - 最大流
http://acm.hdu.edu.cn/showproblem.php?pid=6598 一开始就觉得是网络流,但是一直都不会怎么建图. 这里要考虑. 每一组边(u,v,a,b,c)建立如下的连接 ...
- ssh: Could not resolve hostname github.com: Name or service not known
问题描述 今天早上在自己的虚拟机上用git pull命令更新github上的版本库时提示下面的错误 [root@localhost ~] git clone git@github.com:sdscbr ...
- 用C#实现获取文件夹大小的源代码
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- nodejs http服务器简单搭建
var http = require('http') // 1. 创建 Server var server = http.createServer() // 2. 监听 request 请求事件,设置 ...
- webpack webpack.config.js配置
安装指定版本的webpack npm install webpack@3.6 -g 安装live-server 运行项目插件 输入live-server 运行后自动打开网页 npm ins ...