首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 甲级 1021 Deepest Root (并查集,树的遍历)

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

作者头像
ShenduCC
发布2018-04-27 10:34:04
1K0
发布2018-04-27 10:34:04
举报
文章被收录于专栏:算法修养算法修养

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1021. Deepest Root (25)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档