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. scrapy框架之进阶

    五大核心组件 - 引擎(Scrapy) 用来处理整个系统的数据流处理, 触发事务(框架核心) - 调度器(Scheduler) 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. ...

  2. tomcat发布web项目

    转:https://www.cnblogs.com/skyblue-li/p/7888951.html Tomcat是一种Web服务器,我们自己做好了一个Web项目,就可以通过Tomcat来发布.服务 ...

  3. fluent中统计颗粒信息【转载】

    转载自:http://mp.weixin.qq.com/s?__biz=MjM5MDkyMjA1Nw==&mid=502657725&idx=1&sn=120703a69e56 ...

  4. Raspberry Pi 4B FTP服务器配置

    目录 1. 安装vsftpd并启动 2. 编辑配置文件 3. 重启服务 4. 测试 5. 为Web服务器添加管理员账户,便于通过ftp网站信息 参考资料:树莓派(raspberry pi)学习之安装f ...

  5. 有效管理进程的几个linux命令

    一般来说,应用程序进程的生命周期有三种主要状态:启动.运行和停止.如果我们想成为称职的管理员,每个状态都可以而且应该得到认真的管理.这八个命令可用于管理进程的整个生命周期. 启动进程 启动进程的最简单 ...

  6. suduku

    github地址 PSP: PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 30 Estimate 估 ...

  7. 讨论SQLite数据库损坏与修复

      版权声明:博客将逐步迁移到 http://cwqqq.com https://blog.csdn.net/cwqcwk1/article/details/45541409 昨晚,朋友和我反馈SQL ...

  8. spring项目启动错误——java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

    最近在搭spring项目框架的时候,遇到一个很伤的问题,翻了很多帖,都报告说什么少spring-context包啊之类的,但实际上spring的那些依赖我根本没漏,下面是我的pom: <depe ...

  9. Android——coredump解析

    撰写不易,转载需注明出处:http://blog.csdn.net/jscese/article/details/46916869本文来自 [jscese]的博客! coredump文件生成前文And ...

  10. fork() 成为负担,需要淘汰 spawn

    A fork() in the road - Microsoft Research https://www.microsoft.com/en-us/research/publication/a-for ...