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 ...
随机推荐
- free pascal
https://freepascal.org/ free pascal OPEN SOURCE COMPILER FOR PASCAL AND OBJECT PASCAL GENERAL HomeNe ...
- 抓取某高校附近共享单车位置,并使用web方式展示过去几天的位置变化
效果如图 使用了高德地图API:https://lbs.amap.com/api/javascript-api/example/marker/massmarks js代码如下: function Ma ...
- python笔记01(详情请看廖雪峰的官方网站)
python 在调用函数的时候, 如果传入的参数数量不对, 如果传入的参数类型不对 会报TypeError的错误,并且Python会明确提示参数错误原因. hex()内置函数会把一个整数转换成十六进制 ...
- HDFS网络拓扑概念及机架感知(副本节点选择)
网络拓扑概念 在本地网络中,两个节点被称为“彼此近邻”是什么意思?在海量数据处理中,其主要限制因素是节点之间数据的传输速率——带宽很稀缺.这里将两个节点间的带宽作为距离的衡量标准. 节点距离:两个节点 ...
- SUST_ACM_2019届暑期ACM集训热身赛题解
问题A:Hello SUST! 知识点:基本输入输出 C/C++: #include <stdio.h> int main() { int n; scanf("%d", ...
- Spring Boot & Restful API 构建实战!
作者:liuxiaopeng https://www.cnblogs.com/paddix/p/8215245.html 在现在的开发流程中,为了最大程度实现前后端的分离,通常后端接口只提供数据接口, ...
- 小白学Python(11)——pyecharts,绘制饼图 Pie
Pie-基本示例 from example.commons import Faker from pyecharts import options as opts from pyecharts.char ...
- 山区建小学(区间dp+前缀和+预处理)
[题目描述] 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中,0 < i ...
- 使用absolute实现的后台布局,包括小图标定位,菜单弹出等完整版
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#学习——控件
Windows应用程序控件的基类是位于System.Windows.Forms命名空间的Control类. Control类定义了控件类的共同属性.方法和事件,其他的控件类都直接或间接到派生自这个类. ...