博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1021 Deepest Root (25 分)
阅读量:5094 次
发布时间:2019-06-13

本文共 3143 字,大约阅读时间需要 10 分钟。

1021 Deepest Root (25 分)
 

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 (≤) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N1 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:

51 21 31 42 5

Sample Output 1:

345

Sample Input 2:

51 31 42 53 4

Sample Output 2:

Error: 2 components 终于不是模拟题了,舒服。。。 大概题意就是----给你一组数据,要么是树,要么就是非连通图, 如果是树,你就把最远距离的端点输出来,如果不是树,就输出它分成了几个部分。 题目挺好的。 题解如下:26ms
1 #include 
2 #define N 10050 3 using namespace std; 4 int n, pos = 0; 5 vector
v[N]; 6 int vis[N], val[N]; 7 int max_len = 0, id = 0; 8 set
s; 9 set
::iterator it;10 bool flag = false;11 void dfs(int x){12 vis[x] = 1;13 for(int i = 0 ; i < v[x].size(); i++){14 int k = v[x][i];15 if(vis[k] == 0)16 dfs(k);17 }18 }19 void dfs1(int x,int len){20 vis[x] = 1;21 if(len > max_len){22 max_len = len;23 id = x;24 }25 for(int i = 0; i < v[x].size(); i++){26 int k = v[x][i];27 if(vis[k] == 0){28 dfs1(k, len+1);29 }30 }31 }32 33 void dfs2(int x,int len){34 vis[x] = 1;35 if(len == max_len){36 s.insert(x);37 val[x] = 1;38 flag = true;39 }40 for(int i = 0; i < v[x].size(); i++){41 int k = v[x][i];42 if(vis[k] == 0){43 dfs2(k, len+1);44 }45 }46 }47 48 int main(){49 cin >> n;50 if(n == 1){51 cout << "1" << endl;52 return 0;53 }54 int x, y;55 for(int i = 1; i < n; i++){56 cin >> x >> y;57 v[x].push_back(y);58 v[y].push_back(x);59 }60 memset(vis,0,sizeof(vis));61 for(int i = 1; i <= n; i++){62 if(vis[i] == 0){63 dfs(i);64 pos++;65 }66 }67 if(pos > 1){68 printf("Error: %d components\n", pos);69 }else{70 memset(vis,0,sizeof(vis));71 dfs1(1,0);72 memset(vis,0,sizeof(vis));73 dfs1(id,0);74 for(int i = 1; i <= n; i++){75 if(v[i].size() == 1 && val[i] == 0){76 memset(vis,0,sizeof(vis));77 dfs2(i,0);78 if(flag){79 flag = false;80 s.insert(i);81 val[i] = 1;82 }83 }84 }85 // cout << "****" <

 

 

转载于:https://www.cnblogs.com/zllwxm123/p/11048701.html

你可能感兴趣的文章
C#集合之不变的集合
查看>>
前端本质
查看>>
第一章 android系统移植与驱动开发概述
查看>>
php使用curl下载指定大小的文件
查看>>
Task 6.2冲刺会议九 /2015-5-22
查看>>
eclipse实现JavaWeb应用增量打包
查看>>
Java Web之Servlet入门篇(二)
查看>>
Master选举原理
查看>>
AStyle代码格式工具在source insight中的使用
查看>>
Objective-C在ARC下结合GCD的单例模式和宏模版
查看>>
zuul入门(5)zuul 处理异常
查看>>
SpringBoot+Angular2 开发环境搭建
查看>>
net_py_add_conv5_conv6
查看>>
修改idea的运行内存
查看>>
C++编程--类与继承(3)
查看>>
团队展示与选题
查看>>
Adobe Premiere Pro导入插件开发遇到的一个问题
查看>>
C语言学习笔记--#和##操作符
查看>>
如何将C#安装路径写入注册表
查看>>
.net 验证码
查看>>