关闭

PAT 甲级 1021 Deepest Root (并查集,树的遍历)

标签: PAT1021Deepest Root
219人阅读 评论(0) 收藏 举报
分类:

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 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,

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;
const int maxn=1e4;
int n;
struct Node
{
  int value;
  int next;
}edge[maxn*2+5];
int father[maxn+5];
int head[maxn+5];
int vis[maxn+5];
int num[maxn+5];
int tag[maxn+5];
int tot,cnt;
void add(int x,int y)
{
  edge[tot].value=y;
  edge[tot].next=head[x];
  head[x]=tot++;
}
int find(int x)
{
  if(father[x]!=x)
    father[x]=find(father[x]);
  return father[x];
}
void dfs(int root,int deep)
{
  vis[root]=1;
  int tag=0;
  for(int i=head[root];i!=-1;i=edge[i].next)
  {
    int y=edge[i].value;
        if(!vis[y])
    {
      tag=1;
      dfs(y,deep+1);
    }
  }
  if(!tag)
    num[cnt]=max(num[cnt],deep);
}
int main()
{
  scanf("%d",&n);
  int x,y;
  memset(head,-1,sizeof(head));
  for(int i=1;i<=n;i++)
    father[i]=i;
  tot=0;
  for(int i=1;i<n;i++)
  {
        scanf("%d%d",&x,&y);
        int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
      father[fx]=fy;
    add(x,y);
    add(y,x);
  }
  memset(tag,0,sizeof(tag));
  int res=0;
  for(int i=1;i<=n;i++)
  {
    find(i);
    tag[father[i]]=1;
    }
  for(int i=1;i<=n;i++)
       if(tag[i])
       res++;
  if(res>1)
    printf("Error: %d components\n",res);
  else
  {
    for(int i=1;i<=n;i++)
    {
            memset(vis,0,sizeof(vis));
      cnt=i;
      dfs(i,0);
    }
    int ans=0;
        for(int i=1;i<=cnt;i++)
      ans=max(ans,num[i]);
    for(int i=1;i<=cnt;i++)
      if(num[i]==ans)
        printf("%d\n",i);
  }
  return 0;
}


0
0
查看评论

浙大PAT 1021. Deepest Root (25)

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
  • IAccepted
  • IAccepted
  • 2014-03-04 12:54
  • 3647

PAT 1021-1030 题解

早期部分代码用Java实现。由于PAT虽然支持各种语言,但只有C/C++标程来限定时间,许多题目用Java读入数据就已经超时,后来转投C/C++。浏览全部代码:请戳 本文谨代表个人思路,欢迎讨论;) 1021. Deepest Root (25) 题意 无环连通图也可以视...
  • BIAOBIAOqi
  • BIAOBIAOqi
  • 2013-08-03 03:02
  • 3670

PAT甲级1021

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
  • qq_22194315
  • qq_22194315
  • 2017-02-04 20:44
  • 463

pat1021Deepest Root (25)

题意分析: (1)这一题型比较巧妙,直接给出一个树的拓扑结构,求出这棵树最深的根,言外之意就是指:当以这个节点为根的时候,树具有最深的深度。若这个图具有多个连通分支,就输入分支数。 (2)首先给出算法,随后证明这个算法一定能找到它所有的最深根:第一步:从任意一个节点开始进行深度优先遍历,找到离他最远...
  • LostStephon
  • LostStephon
  • 2015-10-03 18:26
  • 458

pat 1021 Deepest Root

题目是要判断图是否都连接构成树,求使树高最大的所有的根,实际上求图上两点间最大距离。我的思路是依次取各点进行DFS,记下各点可达的最大深度,再在其中取最大值,若各点的最大深度等于该值,该点为所求。若最大深度不等于n-1,则不是一棵树,再用并查集判断有几部分。可能思路有问题,WA了N久。参考网上程序,...
  • jjike
  • jjike
  • 2013-02-28 22:59
  • 825

PAT 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...
  • qq_35031375
  • qq_35031375
  • 2018-01-04 10:03
  • 8

PAT 1021 Deepest Root (25) (并查集+DFS)

 题意:给出一个无环图,求某个节点使得以它为根节点组成一棵树使得这棵树的深度达到最大。 思路:先用并查集判断是不是一个联通快,然后两遍DFS,第一次求出最深根是多少,第二次,求深度与其相等深度搜索深度相等的。 #include #include #include #inc...
  • Grit_ICPC
  • Grit_ICPC
  • 2016-05-27 17:34
  • 170

PAT 1021 Deepest Root(树的直径+BFS+并查集)

题目https://www.patest.cn/contests/pat-a-practise/1021 题意:给出一张图,判断该图能否形成一棵树,若能则求出树的最长简单路(树的直径),否则求出连通分量的个数。 解题思路本题是树的直径+并查集的应用,似乎是某C9的机试题之一。解决这道题的整体思路...
  • lecholin
  • lecholin
  • 2017-06-16 14:29
  • 143

PAT-A 1021 Deepest Root (25)

Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can be conside...
  • a463176553
  • a463176553
  • 2015-05-23 02:09
  • 327

PAT 1021 Deepest Root (25)

先dfs一次找到最长的一端,作为根进行第二次dfs,找到所有最长的树。 要考虑只有1个点的情况 #include #include #include #include using namespace std; const int N=10010; int cnt=1,start[N],n,rt...
  • zhengweijian15
  • zhengweijian15
  • 2014-02-12 14:04
  • 539
    个人资料
    • 访问:132927次
    • 积分:4681
    • 等级:
    • 排名:第7434名
    • 原创:349篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    最新评论