PAT 1021
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的更多相关文章
- 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,.. ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PAT 1021 个位数统计 C语言
1021. 个位数统计 (15) 给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0) ...
- PAT——1021. 个位数统计
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...
- [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 ...
- PAT 1021. 个位数统计 (15)
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...
- PAT 1021 个位数统计
https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296 给定一个k位整数N = d~k-1~*10^ ...
- PAT 1021 Deepest Root
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...
- C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - P ...
随机推荐
- java Ant 的使用
Apache Ant 1.7.0 is the best available version的下载地址:http://ant.apache.org/bindownload.cgi 部署: 参考JAVA ...
- <转>配置DNS辅助服务器:DNS系列之四
配置DNS辅助服务器 在前面的博文中,我们介绍了如何在DNS服务器中创建常用的DNS记录,本文中我们要为大家介绍如何配置DNS的辅助服务器,同时也要介绍一下和辅助区域类似的存根区域. DNS辅助服 ...
- 数据库(class0507)
局部变量_先声明再赋值 声明局部变量 DECLARE @变量名 数据类型 DECLARE @name varchar(20) DECLARE @id int 赋值 SET @变量名 =值 --set用 ...
- 在NodeJS中配置aws ec2
获取access key和secret access key 自己账户下有security credentials的选项 然后点击Acce ...
- 【VB技巧】VB静态调用与动态调用dll详解
本文“[VB技巧]VB静态调用与动态调用dll详解”,来自:Nuclear'Atk 网络安全研究中心,本文地址:http://lcx.cc/?i=489,转载请注明作者及出处! [[请注意]]:在以下 ...
- [VS2012]无法新建或者编译已有的项目
今天启动VS2012时,发现提示插件错误,然后打开以前的网站时,发现报错如下: ContractNameMicrosoft.VisualStudio.Utilities.IContentTypereg ...
- JAVA中“==”与equals()方法区别
equals 方法是 java.lang.Object 类的方法 有两种用法说明: ()对于字符串变量来说,使用"=="和"equals()"方法比较字符串时, ...
- ocp 1Z0-051 23-70题解析
23. Examine thestructure proposed for the TRANSACTIONS table: name Null Type TRANS_ID NOT NULLNUMBER ...
- codeforces 630F Selection of Personnel(组合数)
F. Selection of Personnel time limit per test 0.5 seconds memory limit per test 64 megabytes input s ...
- mongod的主要参数解释
mongod的主要参数有: