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:

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

首先说说算法的思路:先用并查集判断树是否是联通;然后任选一个结点做bfs,则得到最低层的叶结点一定是Deepest Root,接下去在从已选出的Deepest Root中任选一点,在做一次dfs,然后在
将最底层的叶结点加入Deepest Root。此题在时间和空间上都有限制,如果暴力解决的话会超时,如果用邻接矩阵存树的话会超内存,所以采用链表存树。
代码
 #include <stdio.h>
#include <string.h>
#include <stdlib.h> #define NUM 10001
typedef struct linkNode{
int a;
linkNode *next;
}linkNode; linkNode *map[NUM];
int unionSet[NUM];
int level[NUM];
int queue[NUM];
int deepestRoot[NUM]; int findRoot(int);
int bfs(int);
void destroyMap(int); int main()
{
int N,i;
int s,e;
int k;
linkNode *p;
while(scanf("%d",&N) != EOF){
memset(unionSet,,sizeof(unionSet));
memset(map,,sizeof(map));
k = ;
for(i=;i<N;++i){
scanf("%d%d",&s,&e);
int sR = findRoot(s);
int eR = findRoot(e);
if(sR != eR){
unionSet[sR] = eR;
++k;
p = (linkNode*) malloc(sizeof(linkNode));
p->a = e;
p->next = NULL;
if(map[s]){
p->next = map[s];
map[s] = p;
}
else
map[s] = p;
p = (linkNode*) malloc(sizeof(linkNode));
p->a = s;
p->next = NULL;
if(map[e]){
p->next = map[e];
map[e] = p;
}
else
map[e] = p;
}
}
if(k < N - ){
printf("Error: %d components\n",N-k);
destroyMap(N);
continue;
}
memset(deepestRoot,,sizeof(deepestRoot));
int maxLevel = bfs();
int maxLevel_i;
for(i=;i<=N;++i){
if(level[i] == maxLevel){
deepestRoot[i] = ;
maxLevel_i = i;
}
}
maxLevel = bfs(maxLevel_i);
for(i=;i<=N;++i){
if(level[i] == maxLevel)
deepestRoot[i] = ;
}
for(i=;i<=N;++i){
if(deepestRoot[i])
printf("%d\n",i);
}
destroyMap(N);
}
return ;
} int findRoot(int s)
{
while(unionSet[s])
s = unionSet[s];
return s;
} int bfs(int s)
{
memset(level,-,sizeof(level));
int base = ,top = ;
int levelNum = ,endLevel = ;
int t,i;
linkNode *p;
queue[top++] = s;
while(top > base){
t = queue[base++];
level[t] = levelNum;
p = map[t];
while(p){
if(level[p->a] == -){
queue[top++] = p->a;
}
p = p->next;
}
if(endLevel == base){
endLevel = top;
++levelNum;
}
}
return levelNum - ;
} void destroyMap(int n)
{
linkNode *p,*q;
int i;
for(i=;i<=n;++i){
p = map[i];
while(p){
q = p->next;
free(p);
p = q;
}
}
}

PAT 1021的更多相关文章

  1. PAT 1021 个位数统计 (15)(C++&Java&Python)

    1021 个位数统计 (15)(15 分) 给定一个k位整数N = d~k-1~*10^k-1^ + ... + d~1~*10^1^ + d~0~ (0<=d~i~<=9, i=0,.. ...

  2. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  3. PAT 1021 个位数统计 C语言

    1021. 个位数统计 (15) 给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0) ...

  4. PAT——1021. 个位数统计

    给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...

  5. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  6. PAT 1021. 个位数统计 (15)

    给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...

  7. PAT 1021 个位数统计

    https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296 给定一个k位整数N = d~k-1~*10^ ...

  8. PAT 1021 Deepest Root

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...

  9. C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - P ...

随机推荐

  1. 《Python 学习手册4th》 第十五章 文档

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  2. MATLAB / Simulink on BeagleBone Black

    转自:beagleboard@googlegroups.com邮件组 作者:kevind I have MATLAB / Simulink working with BeagleBone Black. ...

  3. elasticsearch配置文件解析

    配置es的集群名称 : cluster.name:  fcz_es

  4. public, protected, private, internal, protected internal简析

    public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...

  5. 主席树模板(poj2104)

    主席树是可持久化线段树,可以记录线段树的历史版本. 代码中和线段树不同的是,l,r记录的是左右子树编号,因为普通的线段树版本中,左右子树自然就是o<<1和o<<1|1,但是主席 ...

  6. WebService学习之三:spring+cxf整合

    步骤一:spring项目(java web项目)引入CXF jar包 步骤二:创建webservice服务器 1)创建一个服务接口 package com.buss.app.login; import ...

  7. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. HTML5新增的CSS类API

  9. c语言 while (~scanf("%d%d",&n,&m)) 在这里这个符号“~”是什么意思

    按位取反,简单地说就是二进制1变0,0变1 由于scanf是有返回值的,且返回值为int型 特别的此处用法导致只有scanf返回-1,循环才会结束,也就是要返回EOF while (~scanf(&q ...

  10. swfupload用法总结

    <script src="${base}/thirdparty/swfupload/swfupload.js" type="text/javascript" ...