首页 技术 正文
技术 2022年11月15日
0 收藏 529 点赞 4,183 浏览 2037 个字

1021. Deepest Root (25)

时间限制1500 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue

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

先求连通块,通过并查集,

然后枚举每一个点dfs,

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>using namespace std;
const int maxn=1e4;
int n;
struct Node
{
int value;
int next;
}edge[maxn*2+5];
int father[maxn+5];
int head[maxn+5];
int vis[maxn+5];
int num[maxn+5];
int tag[maxn+5];
int tot,cnt;
void add(int x,int y)
{
edge[tot].value=y;
edge[tot].next=head[x];
head[x]=tot++;
}
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void dfs(int root,int deep)
{
vis[root]=1;
int tag=0;
for(int i=head[root];i!=-1;i=edge[i].next)
{
int y=edge[i].value;
if(!vis[y])
{
tag=1;
dfs(y,deep+1);
}
}
if(!tag)
num[cnt]=max(num[cnt],deep);
}
int main()
{
scanf("%d",&n);
int x,y;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
father[i]=i;
tot=0;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
int fx=find(x);
int fy=find(y);
if(fx!=fy)
father[fx]=fy;
add(x,y);
add(y,x);
}
memset(tag,0,sizeof(tag));
int res=0;
for(int i=1;i<=n;i++)
{
find(i);
tag[father[i]]=1;
}
for(int i=1;i<=n;i++)
if(tag[i])
res++;
if(res>1)
printf("Error: %d components\n",res);
else
{
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
cnt=i;
dfs(i,0);
}
int ans=0;
for(int i=1;i<=cnt;i++)
ans=max(ans,num[i]);
for(int i=1;i<=cnt;i++)
if(num[i]==ans)
printf("%d\n",i);
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291