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. iOS Jenkins 自动化打包构建

    前言 在测试app项目过程中,通常都是需要开发打测试包给到测试,但是无论是iOS还是Android的打包过程都是相当漫长的,频繁的回归测试需要频繁的打包,对于开发同学影响还是蛮大的.因此在这种情况下, ...

  2. 微信小程序之简单记账本开发记录(六)

    昨天虽然将页面成功的搭建出来 但是其中的增删改查功能没有实现,需要到逻辑页面,即js页面注册一下 效果如下图

  3. js 数组 删除第一个和最后一个

    .shift();   删除第一个 .pop();   删除最后一个

  4. global 和 nonlocal关键字

    当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字了. def fun(): global num1 num1=2 print("函数内修改后num1=&qu ...

  5. YouTube 网站的架构演进——阅读心得

    基础平台 Apache Python Linux(SuSe) MySQL psyco,一个动态的Python到C的编译器 lighttpd代替Apache做视频播放 状态 支持每天超过5亿的视频点击量 ...

  6. vue cli 安装element-ui

    1.安装elment-ui --save 参数:上线打包 MacBookPro:vue_test zhangxm$ npm install element-ui axios --save npm WA ...

  7. Async and Await (Stephen Cleary)

    https://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...

  8. [转][C#].Net反编译利器

    来自:https://www.cnblogs.com/zsuxiong/p/5117465.html 有以下8款非常不错的.Net反编译利器: 1.Reflector Reflector是最为流行的. ...

  9. vue+webpack 实现懒加载的三种方式

    第一种: 引入方式 就是正常的路由引入方式 const router = new Router({ routes: [ { path: '/hyh', component: hyh, name: 'h ...

  10. Splinter自动登录

    默认用foxfire浏览器,如果用chrome请到官网下载 chromedriver驱动,解压后放到python目录scripts下 然后添加环境变量,在Path下添加chromedrvier的路径. ...