PAT 甲级 1021 Deepest Root (并查集,树的遍历)
来源:互联网 发布:如何用微博做淘宝客 编辑:程序博客网 时间:2024/06/05 19: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 (<=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:51 21 31 42 5Sample Output 1:
345Sample Input 2:
51 31 42 53 4Sample Output 2:
Error: 2 components
先求连通块,通过并查集,
然后枚举每一个点dfs,
#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <math.h>#include <algorithm>#include <vector>using namespace std;const int maxn=1e4;int n;struct Node{ int value; int next;}edge[maxn*2+5];int father[maxn+5];int head[maxn+5];int vis[maxn+5];int num[maxn+5];int tag[maxn+5];int tot,cnt;void add(int x,int y){ edge[tot].value=y; edge[tot].next=head[x]; head[x]=tot++;}int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x];}void dfs(int root,int deep){ vis[root]=1; int tag=0; for(int i=head[root];i!=-1;i=edge[i].next) { int y=edge[i].value; if(!vis[y]) { tag=1; dfs(y,deep+1); } } if(!tag) num[cnt]=max(num[cnt],deep);}int main(){ scanf("%d",&n); int x,y; memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) father[i]=i; tot=0; for(int i=1;i<n;i++) { scanf("%d%d",&x,&y); int fx=find(x); int fy=find(y); if(fx!=fy) father[fx]=fy; add(x,y); add(y,x); } memset(tag,0,sizeof(tag)); int res=0; for(int i=1;i<=n;i++) { find(i); tag[father[i]]=1; } for(int i=1;i<=n;i++) if(tag[i]) res++; if(res>1) printf("Error: %d components\n",res); else { for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); cnt=i; dfs(i,0); } int ans=0; for(int i=1;i<=cnt;i++) ans=max(ans,num[i]); for(int i=1;i<=cnt;i++) if(num[i]==ans) printf("%d\n",i); } return 0;}
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
- PAT 1021 Deepest Root(树的直径+BFS+并查集)
- PAT 1021 Deepest Root (25) (并查集+DFS)
- pat 1021. Deepest Root (并查集,搜索)
- PAT--1021. Deepest Root(DFS+并查集)
- PAT 甲级 Deepest Root (25)
- [PAT-甲级]1021.Deepest Root
- [并查集&&BFS]PAT1021 Deepest Root
- 1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)
- PAT甲级练习题A1021. Deepest Root (25)
- 【PAT甲级】1021. Deepest Root (25)
- PAT甲级练习1021. Deepest Root (25)
- 1021. Deepest Root (25) PAT 甲级
- PAT甲级1021. Deepest Root (25)
- PAT 甲级 1021. Deepest Root (25)
- PAT 甲级1021. Deepest Root (25)
- PAT (Advanced Level) 1021. Deepest Root (25) 并查集+DFS
- 1021. Deepest Root (25)【并查集+深搜】——PAT (Advanced Level) Practise
- 关于push进入下一页,底部tabbar隐藏的小问题
- android 混合开发框架phonegap+Mac
- 数学软件 Scilab 5.5.2
- Python入门:字符串处理
- React Native--学习资料入口
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
- android AES加密解密(最新版)
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- hadoop2.x使用put命令报错的问题
- Windows 下编译 owncloud
- JS去除字符串左右两端的空格
- Android studio 多渠道打包(超简洁版)
- JAVA内部类 (成员内部类、局部内部类、匿名内部类、静态内部类、内部类的继承))
- 在ubuntu下安装mongodb