PTA-1021—— Deepest Root(最后两组数据错误)

Posted orangecyh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA-1021—— Deepest Root(最后两组数据错误)相关的知识,希望对你有一定的参考价值。

题目:

A graph which is connected and acyclic can be considered a tree. The hight 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 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 componentswhere 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

分析:

DFS。参考博客:点击前往。但依然有两组数据出错。

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #include<algorithm>
 5 using namespace std;
 6 struct Node{
 7     vector<int> points;
 8 }nodes[10001];
 9 
10 bool visited[10001];
11 int n;
12 int height=0,components=0;
13 vector<int> farthestNode;
14 
15 void dfs(int root,int level){
16     visited[root]=true;
17     if(level>height){        //若高度更高,则原本的集合清空,将最高的更新 
18         height=level;
19         farthestNode.clear();
20         farthestNode.push_back(root);
21     }else if(level==height){    //同等高度,将该结点添加进去 
22         farthestNode.push_back(root);
23     }
24     for(int i=0;i<nodes[root].points.size();i++){
25         if(visited[nodes[root].points[i]]==false){    //对子结点进行深搜 
26             dfs(nodes[root].points[i],level+1);
27         }
28     }
29 }
30 
31 int main(){
32     cin>>n;
33     if(n==1){
34         cout<<"1";
35         return 0;
36     }
37     for(int i=1;i<n;i++){
38         int a,b;
39         cin>>a>>b;
40         nodes[a].points.push_back(b);
41         nodes[b].points.push_back(a);
42     }
43     memset(visited,0,sizeof(visited));
44     for(int i=1;i<=n;i++){
45         if(visited[i]==false){
46             visited[i]=true;
47             dfs(i,0);
48             components++;
49         }
50     }
51     if(components>1){
52         cout<<"Error: "<<components<<" components";
53     }else{
54         int k=farthestNode[0];    //距离最远的点
55         farthestNode.clear();
56         height=0;
57         memset(visited,0,sizeof(visited));
58         dfs(k,0);        //从最远点再次进行深搜,得到的点全为deepest root
59         farthestNode.push_back(k);    //k也为deepest root
60         sort(farthestNode.begin(),farthestNode.end());
61         for(int i=0;i<farthestNode.size();i++){
62             cout<<farthestNode[i]<<endl;
63         } 
64     }
65     return 0;
66 }

 

 

以上是关于PTA-1021—— Deepest Root(最后两组数据错误)的主要内容,如果未能解决你的问题,请参考以下文章

1021. Deepest Root (25)

PAT 1021 Deepest Root

1021 Deepest Root (25 分)

1021 Deepest Root (25 分)(经典搜索)

PAT 甲级 1021 Deepest Root

PAT (Advanced Level) 1021. Deepest Root (25)