1021. Deepest Root (25)
来源:互联网 发布:中小学生交通事故数据 编辑:程序博客网 时间:2024/06/05 16:48
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes’ numbers.
Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print “Error: K components” where K is the number of connected components in the graph.
Sample Input 1:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components
算法分析:采用领接矩阵存储图,用两次深度优先搜索遍历图,第一次遍历是判断给定的图是否是连通图,如果不是则计算连通图的个数然后输出‘错误’,随便找出最深的顶点;第二次遍历是以第一次遍历中找到的顶点其中任意一个为出发点,再找出最深的顶点。结合前两次遍历中找到的最深的顶点,按从小到大输出。
#include <stdio.h>#include <stdlib.h>int g[10001][10001] = {{0}}; //图//depth表示每个顶点的深度,第一个顶点深度为1;result表示符合要求顶点的集合。int visited[10001] = {0}, depth[10001] = {0}, result[10001] = {0};int n;void dfs(int v, int level) //深度优先遍历,并更新每个顶点的深度{ int i; visited[v] = 1; depth[v] = level; for(i = 1; i <= n; i++) { if(!visited[i] && g[v][i] == 1) { dfs(i, level + 1); } }}void mark() //找出最深的顶点,并初始化{ int maxDepth = -1, i; for(i = 1; i <= n; i++) //找到最深顶点的深度 if(depth[i] > maxDepth) maxDepth = depth[i]; for(i = 1; i <= n; i++) //找到最深的顶点,并记录该顶点 { if(depth[i] == maxDepth) result[i] = 1; } for(i = 1; i <= n; i++) //初始化 { depth[i] = 0; visited[i] = 0; }}int main(){ int i, count = 0; scanf("%d", &n); for(i = 0; i < n - 1; i++) { int v, w; scanf("%d %d", &v, &w); g[v][w] = g[w][v] = 1; } for(i = 1; i <= n; i++) //第一次遍历 { if(!visited[i]) { dfs(i, 1); count++; } } if(count > 1) //不是连通图 printf("Error: %d components", count); else //是连通图 { mark(); for(i = 1; i <= n; i++) 第二次遍历 { if(result[i]) { dfs(i, 1); break; } } mark(); for(i = 1; i <= n; i++) if(result[i]) printf("%d\n", i); } return 0;}
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- iOS身份证号码识别
- 六、ServletContext和ServletConfig学习
- 用个小例子来介绍一下JDK8的CompletableFuture
- Ehcache是现在最流行的纯Java开源缓存框架
- 一维数组Reverse翻转与rotate旋转
- 1021. Deepest Root (25)
- elasticsearch-索引与分片实现原理
- Error:warning: Ignoring InnerClasses attribute for an anonymous inner class错误解决方法
- 字符串哈希函数
- easyui我用到的标签及属性
- 数字
- 高可用笔记(1) nginx
- xml对特殊字符的处理(字符转义)
- 设计模式:策略模式