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 ...
随机推荐
- Visual Assist X 破解步骤
1. 下载VA安装包,并点击exe文件安装(附下载地址:http://down.51cto.com/data/766817) 2. 将Visual Assist X Patch文件复制到C:\User ...
- Spring 中context.start作用
我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...
- 1.1……什么是3G
移动通信技术的发展 第一代移动通信技术(1st - Generation),只能进行语音通话. 第二代移动通信技术(2nd - Generation),可以收发短信.可以上网,但速度只有几十Kbps, ...
- codeforce 702D Road to Post Office 物理计算路程题
http://codeforces.com/contest/702 题意:人到邮局去,距离d,汽车在出故障前能跑k,汽车1公里耗时a,人每公里耗时b,修理汽车时间t,问到达终点最短时间 思路:计算车和 ...
- [转]sublime 使用技巧总结
原文链接:http://www.cnblogs.com/yingzi/archive/2012/04/24/2469056.html 对于用惯了editplus的人来说,突然接触到sublime有点无 ...
- public, protected, private, internal, protected internal简析
public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...
- CodeForces 705B Spider Man (水题)
题意:给定 n 个数,表示不同的环,然后把环拆成全是1,每次只能拆成两个,问你有多少次. 析:也不难,反正都要变成1,所以把所有的数都减1,再求和即可. 代码如下: #pragma comment(l ...
- MFC中常用的内容
在程序中更改静态文本内容. GetDlgItem(IDC_STATIC)->SetWindowText("欢迎"); 不用UpdateData(false); 如果提示con ...
- NOSQL之旅---HBase
最近因为项目原因,研究了Cassandra,Hbase等几个NoSQL数据库,最终决定采用HBase.在这里,我就向大家分享一下自己对HBase的理解. 在说HBase之前,我想再唠叨几句.做互联网应 ...
- JQuery要点(一)
简介: jQuery Mobile 是用于创建移动 Web 应用的前端开发框架. jQuery Mobile 可以应用于智能手机与平板电脑. jQuery Mobile 使用 HTML5 & ...