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. Oracle-数据库

    Oracle 1.特点 关系型数据库 采用二维表的行使管理数据库 具有行和列  表间存在关联关系 2.安装 数据库(11g) 版本类型 32位    64位  安装类型 桌面类 本机开发 服务器类 生 ...

  2. [转] FastJson---高性能JSON开发包

    原文地址: FastJson---高性能JSON开发包 Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发.1.遵循http://json.org标准,为 ...

  3. 小项目:mini资源管理器【使用IO流:包含(Directory与DirectoryInfo、File与FileInfo的用法)】

    主界面是这样的 1.在点击查询引发的事件时把TextBox.Text输入的目录地址加载到TreeList控件里. private void btnSelect_Click(object sender, ...

  4. CCF真题之窗口

    201403-2 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示 ...

  5. angular 依赖注入

    依赖注入    依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题.关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/art ...

  6. PTPX Power Analysis Flow

    PrimeTime PX工具是PrimeTime工具内的一个feature. PTPX的功耗分析,可以报告出chip,block,cell的各个level的功耗. 使用PTPX可以分析的功耗的方式: ...

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

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

  8. 【rails3教材】博客构建过程2

    2. 使用脚手架快速搭建网页 rails的脚手架可以快速生成应用程序的一些片段,如果你需要为一个资源创建一系列的控制器视图模型,那么脚手架就是你需要的工具 3. 创建资源 对于一个博客程序,你可以以生 ...

  9. SQLServer中在视图上使用索引(转载)

    在SQL Server中,视图是一个保存的T-SQL查询.视图定义由SQL Server保存,以便它能够用作一个虚拟表来简化查询,并给基表增加另一层安全.但是,它并不占用数据库的任何空间.实际上,在你 ...

  10. sp_addlinkedserver 方法应用

    EXEC  sp_addlinkedserver      @server='DBVIP',--被访问的服务器别名       @srvproduct='',      @provider='SQLO ...