首页 技术 正文
技术 2022年11月17日
0 收藏 752 点赞 3,302 浏览 2473 个字

Description

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.

There is exactly one node, called the root, to which no directed edges point.

Every node except the root has exactly one edge pointing to it.

There is a unique sequence of directed edges from the root to each node.

For example, consider the illustrations below, in which nodes are
represented by circles and edges are represented by lines with
arrowheads. The first two of these are trees, but the last is not.

Is It A Tree?(并查集)

In this problem you will be given several descriptions of
collections of nodes connected by directed edges. For each of these you
are to determine if the collection satisfies the definition of a tree or
not.

Input

The input will consist of a sequence of descriptions (test cases)
followed by a pair of negative integers. Each test case will consist of a
sequence of edge descriptions followed by a pair of zeroes Each edge
description will consist of a pair of integers; the first integer
identifies the node from which the edge begins, and the second integer
identifies the node to which the edge is directed. Node numbers will
always be greater than zero.

Output

For each test case display the line “Case k is a tree.” or the line
“Case k is not a tree.”, where k corresponds to the test case number
(they are sequentially numbered starting with 1).

Sample Input

6 8  5 3  5 2  6 4
5 6 0 08 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 03 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1

Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.题目意思:判断所给的数据能否构成一颗树。解题思路:题目中所给的是有向树,并给出了性质:
1.只有一个节点,称为根节点,没有定向边指向它。
2.除了根节点外,每个节点都只有有一条指向它的边。
3.从树根到任一结点有一条有向通路。
抽象过来就是三个条件:
1.只有一个入度为0的点,作为根节点。
2.除根节点外,其他点的入度只能为1。
3.所有点都能连通,也就是所有点需要在一个集合中,使用并查集来划分集合。注意!!!可能会出现空树,也就是0 0,空树也是树。
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=1e4+;
int pre[MAX];///并查集记录父亲节点
int in[MAX];///入度
int vis[MAX];///节点是否存在
void init()
{
int i;
for(i=; i<MAX; i++)
{
vis[i]=;
in[i]=;
pre[i]=i;
}
}
int Find(int x)
{
if(pre[x]==x)
{
return x;
}
else
{
return Find(pre[x]);
}
}
void Union(int root1,int root2)
{
int x,y;
x=Find(root1);
y=Find(root2);
if(x!=y)
{
pre[x]=y;
}
}
int main()
{
int i,root,counts,a,b,flag,ans=;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==-&&b==-)
{
break;
}
if(a==&&b==)///空树
{
printf("Case %d is a tree.\n",ans);
ans++;
continue;
}
init();
vis[a]=;
vis[b]=;
in[b]++;
Union(a,b);
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==&&b==)
{
break;
}
vis[a]=;
vis[b]=;
in[b]++;
Union(a,b);
}
flag=;
root=;
counts=;
for(i=;i<MAX;i++)
{
if(vis[i]&&in[i]==)///根节点个数
{
root++;
}
if(in[i]>=)///除根节点外,其他点入度需为1
{
flag=;
}
if(vis[i]&&pre[i]==i)///所有点都在一个集合中
{
counts++;
}
}
if(root!=||counts>)
{
flag=;
}
if(flag)
{
printf("Case %d is a tree.\n",ans);
ans++;
}
else
{
printf("Case %d is not a tree.\n",ans);
ans++;
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300