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. 用bat 删除当前文件夹下的某类文件

    @echo on for /r %%f in (*.pdb,*.xml) do del %%f 保存为bat文件执行!

  2. OA项目设计的能力③

    1.然后来了一个,写在我们的主要要求之一,有回波数据还需要添加的方法,我们需要知道,事实上,页被传递id演出id通讯实体name,所以想要回显就是须要得到privilegeIds,假设像上一篇在jsp ...

  3. POJ 1984 Navigation Nightmare (数据结构-并检查集合)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1 ...

  4. Cocos2d-x 单点触摸--让我们用手指动起来的精灵

    转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25656673 效果图: CCTouch类装载了触摸点的信息.包含触摸点的横纵坐标值和触 ...

  5. 移动端 transition动画函数的封装(仿Zepto)以及 requestAnimationFrame动画函数封装(仿jQuery)

    移动端 css3 transition 动画 ,requestAnimationFrame 动画  对于性能的要求,h5优先考虑: 移动端 单页有时候 制作只用到简单的css3动画即可,我们封装一下, ...

  6. Visual Studio 2015使用EF6的CodeFirstFromDB模式操作Sqlite数据库时Provider问题

    传送门 什么是CodeFristFromDB 问题:查询数据是遇到 “/”应用程序中的服务器错误. No Entity Framework provider found for the ADO.NET ...

  7. MySql分析算法作品索引(马上,只是说说而已B-tree)

    刚开始学习的时候,百度搜索.但我发现很难理解了很多的太复杂,各种物品的整合总结(建议可能看到的文字,我不明白也没关系,再看看操作步骤图,然后结合文,所以,一切都清楚了很多) B-tree.B这是bal ...

  8. 使用RESTClient插件数据模拟(GET,POST)提交

    1:在Firefox下载RESTClient插件安装 2:安装界面后, 3:点击设置头文件:(设请求地址有头部文件) 4:设置界面,当然有非常多选择.依据你的须要.一般在输入的时候有智能提示,我这里以 ...

  9. 理解git经常使用命令原理

    git不同于类似SVN这样的版本号管理系统,尽管熟悉经常使用的操作就能够满足大部分需求,但为了在遇到麻烦时不至于靠蛮力去尝试,了解git的原理还是非常有必要. 文件 通过git管理的文件版本号信息所有 ...

  10. EF操作sqlite数据库时的项目兼容性问题

    问题:vs2015打不开vs2010建的操作sqlite的实体数据模型edmx文件 原因: 当前电脑必须先安装:驱动库及sqlite的vs拓展 正常情况下安装驱动和拓展后,vs2015就应该可以正常打 ...