PAT1021. Deepest Root

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
思路:并查集加上DFS  此题需要注意的就是理论的证明。相当于在一棵树上寻找最长路径。

  1 #include <iostream>
  2 #include <vector>
  3 #include <cstdio>
  4 #include <algorithm>
  5 using namespace std;
  6 #define MAX 10010
  7 vector<int>G[MAX];
  8 int father[MAX];
  9 bool Isfather[MAX];
 10 vector<int>temp,ans;
 11
 12 void Init()
 13 {
 14     for(int i=0;i<MAX;i++)
 15     {
 16         father[i]=i;
 17     }
 18 }
 19 int FindFather(int x)
 20 {
 21     int z=x;
 22     while(x!=father[x])
 23     {
 24         x=father[x];
 25     }
 26     //进行压缩
 27     while(z!=father[z])
 28     {
 29        int tem=father[z];
 30        father[z]=x;
 31        z=tem;
 32     }
 33     return x;
 34 }
 35 void Union(int a,int b)
 36 {
 37     int fa=FindFather(a);
 38     int fb=FindFather(b);
 39     if(fa!=fb)
 40     {
 41         father[fa]=fb;
 42     }
 43 }
 44 int Cal(int n)
 45 {
 46     int count=0;
 47     for(int i=1;i<=n;i++)
 48     {
 49         if(father[i]==i)
 50           count++;
 51     }
 52     return count;
 53 }
 54 int heightmax=0;
 55 void DFS(int index,int height,int pre)//pre????
 56 {
 57     if(height>heightmax)
 58     {
 59         temp.clear();
 60         temp.push_back(index);
 61         heightmax=height;
 62     }else if(height==heightmax)
 63     {
 64         temp.push_back(index);
 65     }
 66     for(int i=0;i<G[index].size();i++)
 67     {
 68         if(G[index][i]==pre)
 69           continue;
 70         DFS(G[index][i],height+1,index);
 71     }
 72 }
 73
 74
 75 int main(int argc, char *argv[])
 76 {
 77     int N;
 78     Init();
 79     scanf("%d",&N);
 80     for(int i=0;i<N-1;i++)
 81     {
 82         int a,b;
 83         scanf("%d%d",&a,&b);
 84         G[a].push_back(b);
 85         G[b].push_back(a);
 86         Union(a,b);
 87     }
 88     int block=Cal(N);
 89     if(block!=1)
 90       printf("Error: %d components\n",block);
 91     else
 92     {
 93         DFS(1,1,-1);
 94         ans=temp;
 95         DFS(ans[0],1,-1);
 96         for(int i=0;i<temp.size();i++)
 97            ans.push_back(temp[i]);
 98          sort(ans.begin(),ans.end());
 99          printf("%d\n",ans[0]);
100          for(int i=1;i<ans.size();i++)
101          {
102              if(ans[i-1]!=ans[i])
103                printf("%d\n",ans[i]);
104            }
105     }
106     return 0;
107 }

时间: 2024-12-15 20:03:15

PAT1021. Deepest Root的相关文章

PAT-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: E

PAT-1021. Deepest Root (25)-DFS求树的最大深度

这道题目主要是给你一个图,那么计算从任何一点开始,以此为根节点,树的最大深度.不保证图的连通性. 通过率挺低的,应该是那个大数据的测试用例,内存超出的问题卡住了,最大数据是10^4,如果用邻接矩阵的形式保存图形,那么将是n*n的空间复杂度,就是10^8*4B个数据,为4*10^5KB内存,题目是3.2*10^5KB内存,所以内存超出.卡在内存上了.所以我们想到的是用邻接表来保存图,但是我们实现的时候不用链表,而是用向量数组的形式来保存图,这个方式极好,要常用. 思路就是用dfs来搜索每一个节点的

1021. Deepest Root (25) 并查集&amp;&amp;DFS

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 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 t

PAT 1021. Deepest Root (25)

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

A1021. 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: E

Deepest Root (并查集+DFS树的深度)

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: E

1021. Deepest Root

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: E

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: E

PAT 甲级 1021 Deepest Root

https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 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