LightOJ1094 - Farthest Nodes in a Tree(树的直径)
2016-03-15 13:28
417 查看
http://lightoj.com/volume_showproblem.php?problem=1094
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.
树的直径典型题目
树的直径是让求给你一棵树 所有节点和他们的距离然后求这棵树上距离最远的两个点之间的距离
分析:
先是任意找一个点,然后bfs找到离他最远距离的点,把这个点标记起来
然后用这个点再bfs一次找到直径
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 |
树的直径是让求给你一棵树 所有节点和他们的距离然后求这棵树上距离最远的两个点之间的距离
分析:
先是任意找一个点,然后bfs找到离他最远距离的点,把这个点标记起来
然后用这个点再bfs一次找到直径
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stdlib.h> #include <vector> #include <queue> using namespace std; #define N 30005 int ans,Max,Index; int head ,vis ,dis ; struct node { int v,f,next; }e[N*4]; void Inn() { memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); ans=Index=0; } void Add(int u,int v,int f) { e[ans].v=v; e[ans].f=f; e[ans].next=head[u]; head[u]=ans++; } void bfs(int u) { Max=0; memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); queue<int>Q; Q.push(u); vis[u]=1; while(!Q.empty()) { int p,q; p=Q.front(); Q.pop(); for(int i=head[p];i!=-1;i=e[i].next) { q=e[i].v; if(!vis[q]) { vis[q]=1; Q.push(q); dis[q]=dis[p]+e[i].f; if(Max<dis[q]) { Max=dis[q]; Index=q; } } } } } int main() { int T,n,U,V,W,t=1; scanf("%d",&T); while(T--) { Inn(); scanf("%d",&n); for(int i=1;i<n;i++) { scanf("%d %d %d",&U,&V,&W); Add(U,V,W); Add(V,U,W); } bfs(0); bfs(Index); printf("Case %d: %d\n",t++,Max); } return 0; }
相关文章推荐
- GDataXMLNode应用
- nodejs中npm工具自身升级
- Nodejs v4.4.0API文档学习(2)Assert断言测试模块
- 使用rosserial把Arduino作为ROS一个Node遇到的一个问题
- LeetCode Populating Next Right Pointers in Each Node
- javascript创建node节点
- [LeetCode]117. Populating Next Right Pointers in Each Node II
- node.js入门(二) 模块 事件驱动
- node.js入门(一)
- [Node.js] Use "prestart" in scripts
- [Node.js] Using ES6 and beyond with Node.js
- node.js编写服务器
- LeetCode 25. Reverse Nodes in k-Group
- windows下安装nodejs简要步骤
- nodeJs--模块module.exports与实例化方法
- hadoop namenode进程参数调整
- nodejs安装提示express不是内部命令
- nodejs入门——搭建一个聊天室应用
- Nodejs v4.4.0API文档学习(1)简介
- 《超实用的Node.js代码段》连载一:获取Buffer对象字节长度