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基础教程(第二版)》学习笔记 -> 第七章 更加抽象

    对象的魔力 多态:意味着可以对不同类的对象使用同样的操作: 封装:对外部世界隐藏对象的工作细节: 继承:以普通的类为基础建立专门的类对象 多态① 多态和方法绑定到对象特性上面的函数称为方法(metho ...

  2. subclipse svn 在64位win7下报Failed to load JavaHL Library

  3. linux挂载问题解决

    1. 挂载光盘 </pre></p><p><pre name="code" class="plain">[roo ...

  4. MFC DLL 资源模块句柄切换[转]

    以前写MFC的DLL的时候,总会在自动生成的代码框架里看到提示,需要在每一个输出的函数开始添加上 AFX_MANAGE_STATE(AfxGetStaticModuleState()).一直不明白这样 ...

  5. JAVA中的数据结构——集合类(序):枚举器、拷贝、集合类的排序

    枚举器与数据操作 1)枚举器为我们提供了访问集合的方法,而且解决了访问对象的“数据类型不确定”的难题.这是面向对象“多态”思想的应用.其实是通过抽象不同集合对象的共同代码,将相同的功能代码封装到了枚举 ...

  6. [Hive - LanguageManual ] ]SQL Standard Based Hive Authorization

    Status of Hive Authorization before Hive 0.13 SQL Standards Based Hive Authorization (New in Hive 0. ...

  7. IO-同步,异步,阻塞,非阻塞,阅读摘要

    http://www.cnblogs.com/Fly-Wind/p/io.html http://blog.csdn.net/historyasamirror/article/details/5778 ...

  8. 自己实现Single LinkedList

    My_Single_LinkedList 分4个部分实现(CRUD - 增删改查). 首先要有一个Node(节点类) class Node { public int val; public Node ...

  9. TJOI2013 DAY2

    第一题:明显先处理出最终序列,然后用线段树求解.处理最终序列可以用二分加树状数组(时间复杂度log2n, 用平衡树也可以搞...). /* * Problem: TJOI2013-day2-Seque ...

  10. HDU-3864 D_num Miller_Rabin和Pollard_rho

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...