Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23312    Accepted Submission(s): 5338

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.
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1856 1232 1233 1102 1875
/*
HDU 1272的升级版,建议先做1272.
有向图,判断是不是树。
注意点:
1 2 2 1 0 0 No
1 2 1 2 0 0 No
1 2 3 4 0 0 No
0 0 Yes
ru[i]==0的个数要等于1.
*/
#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + ;
int ru[maxn], a[maxn];
int main ()
{
int ic = ;
int u, v;
bool fg = true;
while(fg)
{
memset(ru, , sizeof(ru));
bool ans = true;
map<int, int> mp;
int nu = ;
while(true)
{
scanf("%d%d", &u, &v);
if(u == && v == )
{
if(ans)
{
int t = ;
for(int i = ; i <= nu; i++)
{
if(ru[a[i]] <= )
{
if(ru[a[i]] == )
{
t++;
if(t > )
{
ans = false;
break;
}
}
}
else
{
ans = false;
break;
}
}
}
if(ans)
printf("Case %d is a tree.\n", ++ic);
else
printf("Case %d is not a tree.\n", ++ic);
break;
}
else if(u < && v < )
{
fg = false;
break;
}
if(!mp.count(u))
{
a[++nu] = u;
mp[u] = ;
}
if(!mp.count(v))
{
a[++nu] = v;
mp[v] = ;
}
if(ans)
{
ru[v]++;
int t = u * + v;
if(mp.count(t))
ans = false;
else
mp[t] = ;
t = u + v * ;
if(mp.count(t))
ans = false;
else
mp[t] = ;
}
}
}
return ;
}

HDU 1325(并查集)的更多相关文章

  1. hdu 1325(并查集)

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

  2. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  3. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  4. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  5. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  6. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  7. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  8. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  9. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  10. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. mongoDB与sql聚合操作对应图

    SQL Terms, Functions, and Concepts MongoDB Aggregation Operators WHERE $match GROUP BY $group HAVING ...

  2. 初学者必看:.NET 中的静态与非静态的异同

    对于初学者来说,.NET 的静态和非静态一直比较难掌握,这里做一个总结,介绍静态类和普通类,静态方法和实例方法,静态构造函数和实例构造函数,静态字段和非静态字段的区别. 静态类 vs 普通类 静态类与 ...

  3. 29、filter、map、reduce的作用?

    通俗的说..都是用在一堆数据(比如一个列表)上.. map是用同样方法把所有数据都改成别的..字面意思是映射..比如把列表的每个数都换成其平方.. reduce是用某种方法依次把所有数据丢进去最后得到 ...

  4. django框架之中间件

    中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在djang ...

  5. rabbitmq和kafka怎么选?【转】

    MQ框架非常之多,今天简单说一下有代表性的两个MQ(rabbitmq和kafka).经常会有人问rabbitmq和kafka到底哪个好呢?其实没有好与不好之分,只有哪个更合适,首先要根据自己项目的业务 ...

  6. 制作一棵ztree

    我们在做web项目时,常会用到一些树形菜单.在此,我们利用ztree实现树形菜单的效果.zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTr ...

  7. cin关闭同步[转]

    很多人会说cin的速度比scanf慢很多, 其实不然. cin慢的原因主要在于默认cin与stdin总是保持同步, 这一步是消耗时间大户. 只需要加上ios::sync_with_stdio(fals ...

  8. beego学习笔记(4):开发文档阅读(5)

    controller的逻辑: 我们看下面的代码,就知道怎么传值的: import ( "github.com/astaxie/beego" ) type MainControlle ...

  9. beego学习笔记(4):开发文档阅读(2)

    bee工具的安装和使用 bee 工具是一个为了协助快速开发 beego 项目而创建的项目,通过 bee 您可以很容易的进行 beego 项目的创建.热编译.开发.测试.和部署. go get gith ...

  10. Redis学习-redis概述

    最近刚刚接触了redis技术,对此有一些了解,这是简单做一点总结. Redis简介 首先,简单了解一下NoSQL(Not only sql),不要错误的理解为:没有SQL,而是不仅仅是SQL.NoSQ ...