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.

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 ;
}

-

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

  1. E - Is It A Tree? 并查集判断是否为树

    题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...

  2. hdu 1325 && poj 1308 Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  3. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24237   Accepted: 8311 De ...

  4. POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18778   Accepted: 6395 De ...

  5. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  6. POJ - 3657 Haybale Guessing(二分+并查集)

    题意:有N个大小各不相同的点,给定Q个询问,格式为q1,q2,A,表示区间q1~q2的最小值是A,问第一个与之前询问结果出现冲突的询问. 分析: 1.二分询问的标号mid,查询1~mid是否出现询问冲 ...

  7. hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)

     题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...

  8. 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并

    题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...

  9. HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

随机推荐

  1. 7.4 Varnish VCL的子程序

  2. 0101-ioc

    背景 ioc是spring的基础,即控制反转.springboot基于注解使用ioc. ioc spring称所有被管理的对象为bean, spring ioc主要通过描述的方式完成3类bean的管理 ...

  3. Ado.NET SQLHelper(2)

    测试发现前面发的那个功能太简单,不能调用getdate()等内部函数.  完善后重载了insert和update两个功能,将函数作为字符串传入SQL语句构造,需要的可以试用一下   using Sys ...

  4. JAVA 怎么链接 SQL Server 数据库(半详细)

    哇今天闲着没事准备把前几天链接数据库碰到的问题写到博客,写到一半浏览器切出去了,这个浏览器也没保存,晚上又写了一遍,唉 蛋疼呀╮(╯▽╰)╭!! 说一说连接数据库遇到的问题,唉问题繁多,原来自己刷的精 ...

  5. python基础(三)闭包与装饰器

    闭包(closure): 内嵌函数通过调用外部嵌套函数作用域内的变量,则这个内嵌函数就是闭包. 闭包必须满足三个条件: 必须有一个内嵌函数 内嵌函数必须引用外部嵌套函数中的变量 外部函数的返回值必须是 ...

  6. redis : Can't save in background: fork: Cannot allocate memory

    redis : Can't save in background: fork: Cannot allocate memory JAVA程序报错信息: MISCONF Redis is configur ...

  7. spring boot 使用swagger

    在pom.xml中添加maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId> ...

  8. 转载-Logistic回归总结

     Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2013年11月) 1.引言 看了Stanford的Andrew Ng老师的机器学习公开课中关于Logistic Regress ...

  9. Tomcat JDK MySql 安装配置

    Tomcat 7绿色版指定jdk并注册服务  https://blog.csdn.net/weixin_43976019/article/details/89386171   例如:service.b ...

  10. UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)

    题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和,求最小总开销.所有数均小于10^5. 分析:按 ...