首页 技术 正文
技术 2022年11月9日
0 收藏 365 点赞 2,600 浏览 2339 个字

http://poj.org/problem?id=1308

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.
POJ-1308 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

- -

Sample Output

Case  is a tree.
Case is a tree.
Case is not a tree.

树应该都很熟悉,简单并查集即可解决此题,做该题主要注意下面几点:

1、判断有没有“环”,即出现“多对一”

2、判断是否有唯一的根结点,即不是多棵树

3、空树也是树

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; int fa[];
vector<int> vt; void init(int n)
{
for(int i=;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
return x==fa[x]? x:fa[x]=Find(fa[x]);
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
// ios_base::sync_with_stdio(false);
// cin.tie(NULL); int a,b;
int flag=;//判断是否有环,1表示无环
init();
int T=;//样例个数
while(~scanf("%d %d",&a,&b)&&!(a==-&&b==-))
{
if(a==&&b==)//一个样例结束,判断输出并初始化
{
int num=;
for(int i=;i<vt.size();i++)
if(vt[i]==fa[vt[i]]) num++;
if(flag&&num<=) printf("Case %d is a tree.\n",++T);//num=0为空树
else printf("Case %d is not a tree.\n",++T);
flag=;
vt.clear();
init();
continue;
}
int aa=Find(a);
int bb=Find(b);
if(aa==bb) flag=;//有环
else if(flag)//无环再操作,已经判断有环就不用再进行了
{
fa[aa]=bb;
vt.push_back(a);
vt.push_back(b);
}
} return ;
}

相关推荐
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