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. min_25筛学习笔记【待填坑】

    看见ntf和pb两位大佬都来学了,然后就不自觉的来学了. 我们考虑这样一个问题. $$ans=\sum_{i=1}^nf(i)$$其中$1\leq n\leq 10^{10}$ 其中$f(i)$是一个 ...

  2. Grafana +Zabbix 系列二

    Grafana +Zabbix 系列二 Grafana 简介补充 Grafana自身并不存储数据,数据从其他地方获取.需要配置数据源 Grafana支持从Zabbix中获取数据 Grafana优化图形 ...

  3. html预加载之link标签

    我们之前提及过link rel 里面有preload和prefetch.modulepreload,都是用于预加载资源 <link rel="preload" href=&q ...

  4. jquer绑定和获取属性

    最近每天都在熬夜,今天感觉眼睛特别涩,我决定,今天早睡,哈哈哈,上次总结了jquery控制节点,今天总结jquery控制属性,学习完基础知识,看看下面的案例练习一下,掌握的会更好   属性绑定和获取 ...

  5. Python学习日记(九)—— 模块二(logging、json&pickle、xml、requests、configparser、shutil、subprocess)

    logging模块 用于便捷记录日志且线程安全的模块(便捷的写文件的模块,不允许多个人同时操作文件) 1.单文件日志 import logging logging.basicConfig(filena ...

  6. Python学习日记(三)——Python基本数据类型(运算符、int、str、tuple、dict、range)

    运算符 1.算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2 ...

  7. Jenkins修改显示语言为中文

    1 安装插件 主界面 -> 系统管理 -> 插件管理 -> 可选插件 1.1 安装插件Locale plugin 1.2 安装插件Localization: Chinese(Simp ...

  8. WIN10环境下点击通知栏图标时自动切换输入法导致图标位置变动

    这个问题由来已久,每当点击系统右下角任务栏中的按钮时,原本是搜狗输入法就会自动变成“US [ 中文(简体,中国) ]”,图标会自动错位,导致响应的是其他功能. 假设上图是正常的环境,此时我点击电池图标 ...

  9. CISCO实验记录十:switch基本配置

    1.交换机IP配置 2.配置telnet 1.交换机IP配置 #interface vlan 1 #ip address 192.168.0.3 255.255.255.0 #no shutdown ...

  10. Java操作Cookie方法

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...