Is It A Tree?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26002   Accepted: 8879

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.

Source

并查集的简单应用,就是给的是不是一棵树,就在节点插入集合的时候看看两个节点的根节点是不是相同,如果相同,则说明这不是一棵树,如果都符合,则判断是不是形成了森林,比较坑的是空树也是一棵树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef unsigned long long LL; const int MAX = 1e5+10; int pre[11000]; int a[11000]; bool vis[11000]; int top;
bool flag; int Find(int x)
{
return pre[x]==-1?x:pre[x]=Find(pre[x]);
} void Join(int u,int v)
{
int Fx=Find(u);
int Fy=Find(v);
if(Fx!=Fy)
{
pre[Fx]=Fy;
}
else
{
flag=true;
}
} int main()
{
int u,v; int w=1;
while(scanf("%d %d",&u,&v))
{
if(u==-1&&v==-1)
{
break;
}
if(u==0&&v==0)//空树
{
printf("Case %d is a tree.\n",w++);
continue;
}
top=0;
memset(pre,-1,sizeof(pre));
memset(vis,false,sizeof(vis));
flag=false;
if(!vis[u])
{
a[top++]=u;
vis[u]=true;
}
if(!vis[v])
{
a[top++]=v;
vis[v]=true;
}
Join(u,v);
while(scanf("%d %d",&u,&v)&&(u||v))
{
if(!vis[u])
{
a[top++]=u;
vis[u]=true;
}
if(!vis[v])
{
a[top++]=v;
vis[v]=true;
}
Join(u,v);
}
printf("Case %d ",w++);
if(flag)
{
printf("is not a tree.\n");
}
else
{
int ans=0;
for(int i=0;i<top;i++)
{
if(pre[a[i]]==-1)
{
ans++;
if(ans>1)
{
break;
}
}
}
if(ans==1)
{
printf("is a tree.\n");
}
else
{
printf("is not a tree.\n");
}
}
}
return 0;
}

Is It A Tree?(并查集)的更多相关文章

  1. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  3. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  4. [Swust OJ 856]--Huge Tree(并查集)

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  5. Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Is It A Tree?(并查集)(dfs也可以解决)

    Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...

  7. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  8. 树上统计treecnt(dsu on tree 并查集 正难则反)

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

  9. hdu 1325 Is It A Tree? 并查集

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. (Protype Pattern)原型模式

    定义: 原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建新的对象 适用性: 当我们系统中有一些类,在使用的时候都有同样需要大量的创建,而这样的创建是复杂的而且是浪费CPU,内存资源的 ...

  2. Swift实战-豆瓣电台(九)简单手势控制暂停播放(全文完)

    Swift实战-豆瓣电台(九)简单手势控制暂停播放 全屏清晰观看地址:http://www.tudou.com/programs/view/tANnovvxR8U/ 这节我们主要讲UITapGestu ...

  3. 某个点到其他点的曼哈顿距离之和最小(HDU4311)

    Meeting point-1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. web1

    <!DOCTYPE html> <html> <head> <title>web0923</title> <script> fu ...

  5. linux第6天 流协议-粘包

    今天学习的主要是对第5天的加强. 比如服务器的多进程,点对点应用聊天程序.父进程子进程互发消息.等等. 流协议-粘包 一般TCP协议会出现粘包,粘包产生的原因一般为.TCP协议是流式传输,不会根据用户 ...

  6. bean在容器上的生命周期

            初始化两种方法:         1,使用init-method属性指定那个方法在bean依赖关系设置好后自动执行.         2,实现initializingBean接口 实现 ...

  7. 夺命雷公狗ThinkPHP项目之----企业网站22之网站前台中间层(解决代码冗余)

    我们如果这样写代码虽然可以实现头部二级分类的显示,但是如果再别的控制器下那么会出现显示不了.. 如果再加多一段一样的代码也可以实现出一样的效果: 但是这样会导致代码冗余现象,所以我们为了解决这个问题, ...

  8. Power Gating的设计(架构)

    switching network的层次: 一般选择flatted的形式,hierarchy的结构对voltage drop和performance delay有影响. Power network的结 ...

  9. 10个免费的PHP编辑器/开发工具

    转自: http://www.iteye.com/news/22672 一个好的编辑器或开发工具,能够极大提高我们的开发效率.下面介绍10个免费.强大的PHP编辑器/开发工具.这些编辑器拥有调试器.增 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON SetGray

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetGray SetGray_Delphi.PNG unit Unit1;interfaceuses Windows, Messages, ...