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. delegate 为什么用 weak属性

    weak指针主要用于“父-子”关系,父亲拥有一个儿子的strong指针,因此是儿子的所有者:但是为了阻止所有权回环,儿子需要使用weak指针指向父亲:你的viewcontroller通过strong指 ...

  2. 浏览器URL传参最大长度问题

    这几天为解决一个BUG头疼了一段时间,BUG现象如下: 一个选择人员的选择控件,当选择多个人时(50多个的时候),返回没有错误现象,而再一次打开的时候就报404错误.看到这个错误非常纳闷,无法下手,只 ...

  3. bzoj 2555 SubString(SAM+LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...

  4. 连分数(分数类模板) uva6875

    //连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...

  5. ping命令的几个简单使用

    发觉linux下的ping命令花样还挺多的,下面是几个例子 1.ping www.baidu.com,最粗糙的用法,此时主机将不停地向目的地址发送ICMP echo request数据包,直至你按下C ...

  6. Junit3.8

    使用Junit的最佳实践:新建一个名为test的source folder,用于存放测试类源代码目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面 测试 ...

  7. redis高级实用特性

    1. 安全性 2. 主从复制 3. 事务处理 4. 持久化机制 5. 发布订阅消息 : 可以做一个消息系统 6. 虚拟内存的使用 一 . 安全性 设置客户端连接后进行任何其他指定前需要使用的密码 . ...

  8. 窥探JVM内存分配和回收的过程

    一.环境 JDK 垃圾收集器 是否启用TLAB 通用JVM参数(堆内存分配见下图) 1.6.0_65 Serial + Serial Old 否 -Xms20m -Xmx20m -Xmn10m -XX ...

  9. Beginning OpenGL ES 2.0 with GLKit Part 1

    Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, ...

  10. ds18b20里的 温度值正负判断 为什么要判断大于6348 ,为什么取这个值?

    http://zhidao.baidu.com/question/576118682.html?quesup2&oldq=1