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. 谈谈对MapTask任务分配和Shuffle的理解

    一.切片与MapTask的关系 1.概述 大家要注意区分切片与切块的区别: 切块Block是HDFS物理上把数据分成一块一块的,默认是128M: 数据切片:只是在逻辑上对输入进行分片,并不会在磁盘上分 ...

  2. NO1 ip-systemctl-fdisk

    一.IP相关·man·man:show manual info   查看一个命令的帮助信息:man ip·ip命令: show device显示设备,device address显示地址,route ...

  3. UVA - 10305 Ordering Tasks(拓扑排序)

    题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...

  4. POJ 3007:Organize Your Train part II

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7561   Acce ...

  5. Asp.net mvc+EF+Sql Server2008数据库缓存依赖

    1.开启数据库缓存依赖功能(开启对数据库中表Article和ArticleType的缓存) (注:)如果要配置SqlCacheDependency,则需要以命令行的方式执行. aspnet_regsq ...

  6. 二、环境安装:yarn

    依赖管理工具安装yarn 可参考前几篇文章 1.必须安装nodejs 注意:安装nodejs稳定版本 2.安装cnpm用cnpm替代npm 地址:http://npm.taobao.org/安装cnp ...

  7. vue学习(四)插槽

    一 匿名插槽 // 语法 Vue.component('MBtn', { template: ` <button> <slot></slot> </butto ...

  8. ArryList的使用方法(基础使用-将来会不断添加)

    转自:http://www.cnblogs.com/ysz12300/p/5595907.html 引入ArryList的方法->命名空间:using System.Collections; A ...

  9. file:///D:/Program%20Files/Microsoft%20Visual%20Studio%2011.0/VC/VCWizards/CodeWiz/MFC/Variable/HTML

    title VS2005  VS2008添加变量,添加函数,添加类时弹出 Script Error  解决办法 问现象描述 : 问题大家都清楚了.不赘述 错误提示 :file:///C:/Progra ...

  10. 1 ~ express ~ 初始化。安装第三方模块express。中间件

    一,初始化 二,安装第三方模块express 三,安装中间件 1,bodyParser : 解析 post 请求数据 2,cookies : 读写 cookie 3,swig :模板解析引擎 4,mo ...