【C++】1021. Deepest Root (25)
2014-03-23 17:56
274 查看
1021. Deepest Root (25)
时间限制1500 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 calledthe 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
1.这道题比较适合用bfs,而不是dfs
好吧,20分,有五分没有拿
#include <iostream> #include <string> #include<vector> #include<queue> #include<string.h> #include<algorithm> using namespace std; //bool mark[10001]; vector<vector <int>> a; int level[10001]={0}; queue <int >q; int bfs(int tt){ level[tt]=1; q.push(tt); while(q.size()!=0){ int tt=q.front(); for(int i=0;i<a[tt].size();i++){ if(level[a[tt][i]]==0){ level[a[tt][i]]=level[tt]+1; q.push(a[tt][i]); } } q.pop(); } return 0; } int maxl(int n){ int max=-1; for(int i=0;i<n;i++){ if(level[i]>max){ max=level[i]; } } return max; } int cmp(int a,int b){ return a<b; } int main(){ int N,i,j; freopen("in.txt","r",stdin); scanf("%d",&N); a.resize(N); for(i=0;i<N-1;i++){ int a1,a2; scanf("%d%d",&a1,&a2); a1--; a2--; a[a1].push_back(a2); a[a2].push_back(a1); } int tt=0; bfs(tt); vector <int> maxit; int suo=maxl(N); int cc=0; for( i=0;i<N;i++){ if(level[i]>0){ cc++; } if(level[i]==suo){ maxit.push_back(i); } } if(cc==N){ memset(level,0,sizeof(level)); tt=bfs(maxit[0]); suo=maxl(N); for( i=0;i<N;i++){ if(level[i]==suo){ maxit.push_back(i); } } sort(maxit.begin(),maxit.end(),cmp); for(i=0;i<maxit.size();i++){ cout<<maxit[i]+1<<endl; } }else{ int count=1; for(i=0;i<N;i++){ if(level[i]==0){ bfs(i); count++; } } printf("Error: %d components",count); } return 0; }
相关文章推荐
- 1021. Deepest Root (25)
- PAT(甲级)1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25) (查询最小路径,查询有多少部分圈,学习iterator)
- 1021. Deepest Root (25)
- PAT-1021 Deepest Root (25)
- PAT_A 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- PAT 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- PAT 1021. Deepest Root (25)
- PAT-1021. Deepest Root (25)-DFS求树的最大深度
- PAT 1021. Deepest Root (25)
- 1021. Deepest Root (25)(dfs+并查集)
- 1021. Deepest Root (25)
- PAT 1021 Deepest Root (25) (并查集+DFS)
- 1021. Deepest Root (25) -并查集判树 -BFS求深度