1021. Deepest Root (25)(图的直径)
来源:互联网 发布:上海雕塑培训班 知乎 编辑:程序博客网 时间:2024/05/21 15:50
- 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 (<=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<iostream>#include<vector>#include<set>#include<cstdio>#include<cstring>using namespace std;int father[10005];int vis[10005];vector <int > v[10005];set<int> se;int maxlev=-1;int findfa(int x){ if (x==father[x]) return x; else return father[x]=findfa(father[x]);}void dfs(int s,int leval){ if (leval>maxlev) { se.clear(); maxlev=leval; se.insert(s); } else if (leval==maxlev) { se.insert(s); } for (int i=0;i<v[s].size();i++) { if (vis[v[s][i]]==0) { vis[v[s][i]]=1; dfs(v[s][i],leval+1); vis[v[s][i]]=0; } }}int main(){ int n,a,b,sum=0; cin>>n; for (int i=0;i<=n;i++) father[i]=i; for (int i=1;i<n;i++) { cin>>a>>b; v[a].push_back(b); v[b].push_back(a); int fa=findfa(a); int fb=findfa(b); if(fa!=fb) father[fa]=fb; } for (int i=1;i<=n;i++) if (father[i]==i) sum++; if (sum>1) { printf("Error: %d components",sum); return 0; } vis[1]=1; dfs(1,1); vis[1]=0; set<int>ans; for (set<int>::iterator it=se.begin();it!=se.end();it++) ans.insert(*it); int num=*se.begin(); maxlev=-1; memset(vis,0,sizeof(vis)); se.clear(); dfs(num,1); for (set<int>::iterator it=se.begin();it!=se.end();it++) ans.insert(*it); for (set<int>::iterator it=ans.begin();it!=ans.end();it++) cout<<*it<<endl; 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)
- First集和Follow集
- 【水题】HDU 2021 发工资咯:)
- 疯狂的马 老是超时
- keepalive——http
- vue引入echart组件
- 1021. Deepest Root (25)(图的直径)
- 遗传算法的TSP例子
- 数据分析处理库Pandas-数据读取
- 基于SoC成像芯片MT9M111和CY7C68013的视频采集
- 《操作系统之哲学原理》笔记一
- springMVC入门程序helloworld
- 矩阵快速幂板子
- 欢迎使用CSDN-markdown编辑器
- Codeforces Round #444 (Div. 2) D. Ratings and Reality Shows 贪心