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

原创 2016年05月30日 17:55:28

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

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

【PAT甲级】1021. Deepest Root (25)

注:要考虑时间空间效率问题:使用邻接表,并减少使用dfs的次数(原来为n次dfs,后面改为2次)。#include #include #include #include using names...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

PAT 1021. Deepest Root

【题目链接】 题目大意:给定一个无向图,如果它没有环且是连通的则证明是树,那么就要找出一个结点集,使得集合中的任意一点作为树的根节点,都能使得树的深度达到最大。否则,如果该图不是树,则要求输出图的连...

PAT 1021. Deepest Root

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1021 题意:N个节点,给出N-1条边。要求判断是否为树,若不为树,输出有几个连通分量;若为...

pat 1021. Deepest Root

这题比较简单,我的思路是 1,从任意点开始一次bfs找到最远的jie

PAT 1021. Deepest Root (25)

最后一个case没有过,改#include #include #include #include #define MAXN 10008 using namespace std; int n,k...

【PAT】1021. Deepest Root (25)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t...

pat 1021. Deepest Root (25)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t...

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

题目https://www.patest.cn/contests/pat-a-practise/1021 题意:给出一张图,判断该图能否形成一棵树,若能则求出树的最长简单路(树的直径),否则求出连...

PAT 1021. Deepest Root (25)

//1021. Deepest Root (25) //第二个case是测试N =0 的情况 #include #include #include #include using namesp...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)