Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14197    Accepted Submission(s): 3170
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.

题意:给定一个图,推断是否为树。

题解:须要推断森林,推断回路,推断入度是否大于1.

#include <stdio.h>
#include <string.h>
#define maxn 10002 int in[maxn], pre[maxn], count;
bool vis[maxn]; int ufind(int k)
{
int a = k;
while(pre[k] != -1) k = pre[k];
int b;
while(pre[a] != -1){
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} int main()
{
//freopen("stdin.txt", "r", stdin);
int a, b, ok = 1, cas = 1;
memset(pre, -1, sizeof(pre));
while(scanf("%d%d", &a, &b)){
if(a < 0) break;
if(a == 0){
printf("Case %d is ", cas++);
if(count != 1) ok = 0;
printf(ok ? "a tree.\n" : "not a tree.\n");
ok = 1; count = 0;
memset(in, 0, sizeof(in));
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
continue;
}
if(!ok) continue;
if(!vis[a]){ ++count; vis[a] = 1; }
if(!vis[b]){ ++count; vis[b] = 1; }
if(++in[b] > 1) ok = 0;
a = ufind(a); b = ufind(b);
if(a == b) ok = 0;
else { pre[a] = b; --count; }
}
return 0;
}

HDU1325 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. Is It A Tree?(并查集)

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

  3. CF109 C. Lucky Tree 并查集

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

  4. HDU 5606 tree 并查集

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

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

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

  6. 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 ...

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

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

  8. tree(并查集)

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

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

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

  10. 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. windows API与C#的数据类型对应关系表

    API与C#的数据类型对应关系表 API数据类型 类型描述 C#类型 API数据类型 类型描述 C#类型 WORD 16位无符号整数 ushort CHAR 字符 char LONG 32位无符号整数 ...

  2. String构造函数originalValue.length&gt;size 它发生

    最近观看Jdk6于String源代码被发现String这种施工方法有.源内容如下面: public String(String original) { int size = original.coun ...

  3. android 视频通话开启呼叫等待后,来第三方的视频通话,接通后通话时间一直显示为0,过几秒之后视频通话自己主动挂断

    开启通话设置视频通话的"来电等待"; 步骤1:測试机和配合机A处于视频通话过程中; 步骤2:配合机B向測试机呼出视频电话; 步骤3:測试机接听配合机B的视频来电; 现象:视频通话过 ...

  4. C# 字段、属性、成员变量

    引言: C#与java,C++中的这些基本概念略有不同. 由于easy混淆,所以这里总结下差别. 希望能对刚開始学习的人有帮助! 一.定义与作用 1.字段(field):是C#类级别定义的,和方法同一 ...

  5. poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)

    http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...

  6. STM32F4xx时钟理解

    理解力STM32时钟是我们的应用定时器等的基础,据总结近期工作: 以下是一STM32时钟树: 1.首先注意的的是图中画绿色圈圈的两个,HSE和HSI分别表示外部时钟和内部时钟,当中HSE 是是快速外部 ...

  7. Android studio 中国的垃圾问题解决

    为了获得良好的刚安装Android studio, 实例importproject时刻,你会发现很多中国的文件夹显示异常.例如下面的附图: 为什么会出现这个问题呢,事实上原因非常easy,由于Andr ...

  8. Android网络通信Volley框架源代码浅析(三)

    尊重原创 http://write.blog.csdn.net/postedit/26002961 通过前面浅析(一)和浅析(二)的分析.相信大家对于Volley有了初步的认识,可是假设想更深入的理解 ...

  9. 吐槽一下Activiti用户手册和一本书

    业余没事的时候,读点Java轮廓,无意中发现Activiti.我们打算跑了几个例子来看看它是如何. 我们一直从事低层次.我们在上面的照顾偶尔有精确地的程度不是什么. 这个过程是如此悲惨开始.第一Act ...

  10. js 性能优化整理之 高频优化

    mousemove 拖拽操作 var count = 0; elem.onmousemove = function(){ count++; // 当计数器为偶数的时候不执行mousemove if( ...