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. Java AIO 入门实例(转)

    Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...

  2. Android Framework 其中A记录

    一个简短的引论 以往的研究太偏应用层的功能,实现了,原则上不进入非常理解,现在,研究人员framework该框架层. 创纪录的 1.下载源代码,文件夹例如以下: 2.Android系统的层次例如以下: ...

  3. oracle db于,一个特定的数据字典pct miss其计算公式

    这篇文章是原创文章,转载请注明出处: http://blog.csdn.net/msdnchina/article/details/38766801 本文提到的数据字典.以dc_histogram_d ...

  4. Extjs Web Desktop申请书

    今天我Web Desktop应用基本完成.多语言支持.现有asp,php,jsp版本号. 废话拍了几张照片让大家有一个直观的了解: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  5. POJ 2217 Secretary (后缀数组)

    标题效果: 计算两个公共串串最长的字符串的长度. IDEAS: 这两个组合的字符串. 然后直接确定运行后缀数组height 然后,你可以直接扫描一次height .加个是不是在一个串中的推断就能够了. ...

  6. UiAutomator喷射事件的源代码分析

    上一篇文章<UiAutomator源代码分析之UiAutomatorBridge框架>中我们把UiAutomatorBridge以及它相关的类进行的描写叙述,往下我们会尝试依据两个实例将这 ...

  7. SQLSERVER手动增长日志文件和数据文件

    原文:SQLSERVER手动增长日志文件和数据文件 SQLSERVER手动增长日志文件和数据文件 手动增长日志文件,实际上就是修改日志文件的大小  size 的单位是MB 下面设置日志文件大小是204 ...

  8. JAVA学习课第五 — IO流程(九)文件分割器合成器

    文件分割器 private static final int SIZE = 1024 *1024; public static void splitFile(File file) throws IOE ...

  9. BZOJ 1096 ZJOI2007 仓库建设 边坡优化

    标题效果:特定n植物,其中一些建筑仓库,有一点使,假设没有仓库仓库向右仓库.最低消费要求 非常easy边坡优化--在此之前刷坡优化的情况下,即使这道题怎么错过 订购f[i]作为i点建设化妆i花费所有安 ...

  10. Script:SQL调优健康检查脚本

    Script:SQL调优健康检查脚本 http://www.askmaclean.com/archives/sql-tuning-health-check-script.html 以下脚本可以用于收集 ...