Pat(Advanced Level)Practice--1021(Deepest Root)
来源:互联网 发布:ubuntu samba 写权限 编辑:程序博客网 时间:2024/05/23 11:39
Pat1021代码
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
#include<cstdio>#include<vector>#include<queue>#include<map>#define MAX 10005using namespace std;int dis[MAX];vector<int> adj[MAX];int parent[MAX];int n;int Find(int x){int p=x;while(p!=parent[p])p=parent[p];return p;}void Union(int x,int y){int fx,fy;fx=Find(x);fy=Find(y);if(fx!=fy)parent[fx]=fy;}int BFS(int start){for(int i=1;i<=n;i++)dis[i]=-1;queue<int> q;q.push(start);dis[start]=0;int maxdis=0;while(!q.empty()){int cur=q.front();for(int i=0;i<adj[cur].size();i++){int temp=adj[cur][i];if(dis[temp]<0){q.push(temp);dis[temp]=dis[cur]+1;if(dis[temp]>maxdis)maxdis=dis[temp];}}q.pop();}return maxdis;}int main(int argc,char *argv[]){int i,j;scanf("%d",&n);for(i=1;i<=n;i++)parent[i]=i;for(i=1;i<n;i++){int x,y;scanf("%d%d",&x,&y);adj[x].push_back(y);adj[y].push_back(x);Union(x,y);}int count=0;for(i=1;i<=n;i++)if(parent[i]==i)count++;if(count>1)printf("Error: %d components\n",count);else{map<int,int> m;int max=BFS(1);int index,first=1;for(i=1;i<=n;i++){if(dis[i]==max&&first){index=i;m[i]++;first=0;}else if(dis[i]==max)m[i]++;}max=BFS(index);dis[i]=max;for(i=1;i<=n;i++)if(dis[i]==max)m[i]++;map<int,int>::iterator it;for(it=m.begin();it!=m.end();it++)printf("%d\n",it->first);}return 0;}
- Pat(Advanced Level)Practice--1021(Deepest Root)
- PAT (Advanced Level) Practise 1021 Deepest Root (25)
- PAT (Advanced Level) Practise 1021 Deepest Root (25)
- 【PAT Advanced Level】1021. Deepest Root (25)
- 【PAT】【Advanced Level】1021. Deepest Root (25)
- 1021. Deepest Root (25) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1021. Deepest Root (25)
- 浙大 PAT Advanced level 1021. Deepest Root (25)
- Pat(Advanced Level)Practice--1066(Root of AVL Tree)
- PAT (Advanced Level) Practice 1006
- pat 1021 Deepest Root
- PAT (Advanced Level) 1021. Deepest Root (25) 并查集+DFS
- 1021. Deepest Root (25)【并查集+深搜】——PAT (Advanced Level) Practise
- PAT (Advanced) 1021. Deepest Root (25)
- Pat(Advanced Level)Practice--1025(PAT Ranking)
- Pat(Advanced Level)Practice--1075(PAT Judge)
- PAT 1021 Deepest Root (25)
- Pat(Advanced Level)Practice--1003(Emergency)
- js Dom 基础知识
- 用C实现了简单的顺序表
- NGUI之scroll view制作,以及踩的坑总结
- Win7 在IIS中增加MIME类型文件
- Mac下安装cocos2d-x
- Pat(Advanced Level)Practice--1021(Deepest Root)
- 线性表之大整数求和
- [LeetCode]Two Sum
- H.264/AVC是什么?
- 继承中的构造函数调用顺序
- linux 启动oracle
- SAP财务清账原理与方式
- android textview改变部分文字的颜色和string.xml中文字的替换
- TWO SUM