lightoj1094 - Farthest Nodes in a Tree
2015-08-18 16:05
561 查看
1094 - 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.
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.
PROBLEM SETTER: JANE ALAM JAN
题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点index,然后找离index最远的点就是最大距离?
假定0为根节点找离0最远的点就是,最深的点index,然后找离index最远的点就是树上最远的距离
bfs
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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.PROBLEM SETTER: JANE ALAM JAN
题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点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*4]; int n, cnt, maxx; int Index; // 写成index过不了lightoj=·=|| int head , dist ; 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 != -1; i = e[i].next) { if(dist[e[i].v] == -1) { dfs(e[i].v, dist[u]+e[i].w); } } } int main() { int t, u, v, w, k = 1; scanf("%d", &t); while(t--) { cnt = maxx = 0; memset(head, -1, 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, -1, sizeof(dist)); dfs(0, 0); memset(dist, -1, sizeof(dist)); dfs(Index, 0); printf("Case %d: %d\n", k++, maxx); } return 0; }
bfs
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; #define N 30008 struct node { int v, w, next; } e[N*2]; int n, cnt, maxx; int Index; int head , dist , vis ; 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, 0, sizeof(vis)); queue<int> Q; Q.push(u); vis[u] = 1; dist[u] = 0; while(Q.size()) { u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].v; if(!vis[v]) { vis[v] = 1; 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 = 1; scanf("%d", &t); while(t--) { maxx = cnt = 0; memset(head, -1,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, 0, sizeof(dist)); bfs(0); bfs(Index); printf("Case %d: %d\n", k++, maxx); } return 0; }
相关文章推荐
- HTML DOM nodeType 属性
- [LeetCode 237] Delete Node in a linked list
- nodejs 获取文件的编码方式
- 《Node.js开发指南》知识整理
- NodeJS简介
- Node.js的优缺点
- windows系统 Node.js(安装-启动-测试)
- 认识node.js:express(一)
- MonoDevelop中文乱码
- 在max下完全卸载nodejs
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
- hadoop2.0的datanode数据存储文件夹策略的多个副本
- Lightoj 1094 - Farthest Nodes in a Tree 【树的直径裸题】
- 玩转Node.js - 05. LS 过滤器
- PhoneGap安装 环境搭建及配置
- zTree在Angular中的使用
- LeetCode 25 Reverse Nodes in k-Group
- Node.js静态文件服务器实战(接触nodejs开发中比较实用的部分技巧,获益匪浅)
- 【LeetCode】237. Delete Node in a Linked List
- node.js中模块报错【window is not defined】的解决方法