Problem 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.

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 0 8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0 3 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.

题目大意:

给出多组边,让你判断这是不是一棵树。边的信息以( 始点, 末点)的形式给出以(0,0)结束.,数据以(-1,-1)输入结束;

树的定义:树是指任意两个结点之间有且仅有一条路径的无向图。或者说,只要是没有回路的连通无向图就是树。

树的特性:

⒈必有一个特定的称为根(ROOT)的结点;
2.一棵树如果有n个节点,那么它一定恰好有n-1条边。
3.一棵树中的任意两个节点有且仅有唯一的一条路径连通。
4.在一棵树中加一条边将会构成回路;

思路:应用并查集的知识来判断,将节点以此加入看是否与下面几种情况是否吻合。

不满足条件的情况:

1.要加入的子节点已经有父节点。
2.要加入的父节点的子节点,与父节点有相同的父节点。
3.建树成功,查看树不仅仅只有一颗。

如果1,2不满足了,其实第三步,不需要再进行判断了;

代码:

#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxx=;
int f[maxx];
int getf(int v)
{
if(f[v]==v)
return v;
return f[v]=getf(f[v]);
}
int main()
{
int a,b,Case=;
while(cin>>a>>b)
{
if(a==-&&b==-)
break;
if(a==&&b==)
{
printf("Case %d is a tree.\n",++Case);
continue;
}
for(int i=;i<=maxx;i++)
f[i]=i;
map<int,int>mp;
mp[a]=mp[b]=;
f[b]=a;
int flag=;
if(a==b)
flag=;
while(scanf("%d%d",&a,&b)&&a&&b)
{
mp[a]=mp[b]=;
int t1=getf(a);
int t2=getf(b);
if(t1!=t2&&a!=b)
f[t2]=t1;
else
flag=;
}
if(flag)
printf("Case %d is not a tree.\n",++Case);
else
{
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
if(f[it->first]==it->first)
flag++;
}
if(flag==)
printf("Case %d is a tree.\n",++Case);
else
printf("Case %d is not a tree.\n",++Case);
}
}
return ;
}

Is It A Tree? POJ - 1308(并查集判树)的更多相关文章

  1. 并查集判树 poj 1308

    例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...

  2. POJ 1308/并查集

    题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...

  3. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  4. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  5. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  6. UVA1455 - Kingdom(并查集 + 线段树)

    UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...

  7. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  8. 【bzoj5133】[CodePlus2017年12月]白金元首与独舞 并查集+矩阵树定理

    题目描述 给定一个 $n\times m$ 的方格图,每个格子有 ↑.↓.←.→,表示从该格子能够走到相邻的哪个格子.有一些格子是空着的,需要填上四者之一,需要满足:最终的方格图中,从任意一个位置出发 ...

  9. 并查集&线段树&树状数组&排序二叉树

    超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...

随机推荐

  1. 顺序模型api

    Compile:配置模型,然后进行训练 compile(optimizer, loss=None, metrics=None, loss_weights=None, sample_weight_mod ...

  2. JavaEE项目开发所需要的包(Struts2+Spring5+Hibernate5)

    在这里我只整理了轻量级JavaEE项目开发所需的包 @Auther MrZhangxd 2019-04-29  23:07:21 链接:https://pan.baidu.com/s/16I4KYah ...

  3. CF1214D

    CF1214D 题意: 给你一个 $ n \times m $ 的矩阵,求最少用多少个障碍,将 $ (1,1) $ 到 $ (n,m) $ 的路径堵死. 题意: 因为可以将起点两边堵死,所以答案最多是 ...

  4. databinding 填坑 绑定动作是延后生效

    binding = FragmentNewsMainLayout750Binding.inflate(inflater); homePageViewModel = new HomePageViewMo ...

  5. MapReduce On Yarn的配置详解和日常维护

    MapReduce On Yarn的配置详解和日常维护 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MapReduce运维概述 MapReduce on YARN的运维主要是 ...

  6. app微信支付的集成步骤

    1.引用地址 //微信支付 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' 2.注册 private IWXAPI api ...

  7. <JavaScript> 关于闭包和this对象

    1.this指向windows是如何得出的 var name = "The Window"; var object = { name : "My Object" ...

  8. SQL-W3School-高级:SQL 通配符

    ylbtech-SQL-W3School-高级:SQL 通配符 1.返回顶部 1. 在搜索数据库中的数据时,您可以使用 SQL 通配符. SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代 ...

  9. Android 显示系统:SurfaceFlinger详解

    一.Android系统启动 Android设备从按下开机键到桌面显示画面,大致过程如下图流程: 开机显示桌面.从桌面点击 App 图标到 Activity显示在屏幕上的过程又是怎样的呢?下面介绍And ...

  10. 关于Jetson Kit开发相关资料

    首先是nVidia官方对于Jetson Kit的介绍: http://www.nvidia.com/object/jetson-tk1-embedded-dev-kit.html https://de ...