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:

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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
const int maxn=100010;
vector<int> G[maxn];
int father[maxn];
//并查集初始化
void init()
{
     for(int i=1;i<maxn;i++)
     {
         father[i]=i;
     }
}
int findFather(int x)
{
    int a=x;
    while(x!=father[x])
    {
        x=father[x];
    }
    //路径压缩
    while(a!=father[a])
    {
        int tmp=a;
        a=father[a];
        father[tmp]=x;
    }
    return x;
 }
void Union(int a,int b)//合并
{
    int faA=findFather(a);
    int faB=findFather(b);
    if(faA!=faB)
    {
        father[faA]=faB;
    }
}
//计算集合数目
int isRoot[maxn];
int cal(int n)
{
    int block=0;
    for(int i=1;i<=n;i++)
    {
        isRoot[findFather(i)]=true;
    }
    for(int i=1;i<=n;i++)
    {
        block+=isRoot[i];
    }
    return block;
}
//以某节点为根,遍历
int maxH=0;
vector<int> tmp,Ans;
void DFS(int u,int height,int pre)
{
    if(height>maxH)
    {
    tmp.clear();
    tmp.push_back(u);
    maxH=height;
    } else if(height==maxH)
    {
        tmp.push_back(u);
    }
    for(int i=0;i<G[u].size();i++)
    {
        if(G[u][i]==pre)continue;
        DFS(G[u][i],height+1,u);
    }
}
int main(){
   int a,b,n;
   scanf("%d",&n);
   init();
   for(int i=1;i<n;i++)//从一开始
   {
       scanf("%d%d",&a,&b);//无向图,节点用vector存储
    G[a].push_back(b);
    G[b].push_back(a);
    Union(a,b);
   }
   int block=cal(n);
   if(block!=1)
   {
       printf("Error: %d components",block);
   }else
   {
       DFS(1,1,-1);
       Ans=tmp;
       DFS(Ans[0],1,-1);
       for(int i=0;i<tmp.size();i++)
       {
          Ans.push_back(tmp[i]);
    }
    sort(Ans.begin(),Ans.end());
    printf("%d\n",Ans[0]);
    for(int i=1;i<Ans.size();i++)
    {
        if(Ans[i]!=Ans[i-1])
        {
            printf("%d\n",Ans[i]);
        }
    }
   }

    return 0;
}
时间: 2024-08-28 05:13:10

A1021. Deepest Root (25)的相关文章

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

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 roo

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 roo

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 分) 并查集判断成环和联通+求树的深度

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] A1021 Deepest Root

[题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边数为n-1的图一定是一棵树.因此先判断连通图个数(用DFS遍历图,从而计算连通图个数),等于1则能形成一棵树. 2 遍历每一个节点,假设它为根节点构造一棵树. 方法二: 1 由于连通.边数为n-1的图一定是一棵树,因此需要判断给定数据是否能使图连通.2 确定图连通后,则确定了树,选择合适根结点使树高

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

先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; struct Edge { int a,b; }e[20000]; int n,sz

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

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