Is It A Tree?

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

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
 
题意:
给出若干对单向联通的数,问这些数能否组成一棵树。输入两个负数结束。
代码:
 //判断两点,无环,非森林。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int fat[];
int nod[];
int find(int x)
{
if(fat[x]!=x)
fat[x]=find(fat[x]);
return fat[x];
}
void connect(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
fat[xx]=yy;
}
int main()
{
int a,b;
for(int i=;i<;i++)
fat[i]=i;
int t=,k=,flag=;
memset(nod,,sizeof(nod));
while(scanf("%d%d",&a,&b))
{
if(a<&&b<) break;
if(a==&&b==)
{
if(!flag) //判断是否是森林
{
int tem=;
for(int i=;i<;i++)
{
if(!nod[i]) continue;
if(tem==) tem=find(i);
else if(tem!=&&find(i)!=tem)
{
flag=;
break;
}
}
}
if(flag) printf("Case %d is not a tree.\n",k);
else printf("Case %d is a tree.\n",k);
t=;
flag=;
k++;
for(int i=;i<;i++)
fat[i]=i;
memset(nod,,sizeof(nod));
continue;
}
nod[a]=;nod[b]=;
if(flag) continue;
if(fat[b]!=b||find(a)==b) //不能是环:b之前不能有父亲,a,b不能互相连接
{
flag=;
continue;
}
fat[b]=a;
connect(b,a);
}
return ;
}

*HDU1325 并查集的更多相关文章

  1. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  2. POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28838   Accepted: 9843 -& ...

  3. 并查集树数据结构hdu1325

    我的解法就是去构造了一棵树 以数组的存储方式 数组的值存放节点的根. 排除空树 剩下的就是出现环和多根节点的情况 也就是排除森林和有一个节点多个入度的情况 排除森林就用到了并查集 也就是便利数组让其仅 ...

  4. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

  5. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  6. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  7. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  8. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  9. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

随机推荐

  1. vue2.0学习(一)

    1.解决双花括号在初始化时的闪烁,两种方式,一种是<div v-text="name"></div>,将用v-text指令来显示,类似于angular的ng ...

  2. shell--4.echo和printf

    1. echo (1) echo ,显示普通字符串 echo "HelloWorld" 打印:HelloWorld (2) \ ,显示转义字符串 echo "\" ...

  3. C和指针 第十三章 高级指针话题

    高级声明: int (*f)(); 这里声明有两个括号,第二个括号是函数调用,第一个括号是聚组作用.(*f)是一个函数,所以f是指向返回整型的函数的指针.程序中的每个函数都位于,内存中某个位置,所以存 ...

  4. 交叉编译中的build、host和target

    build.host和target    在交叉编译中比较常见的一些参数就是build.host和target了,正确的理解这三者的含义对于交叉编译是非常重要的,下面就此进行解释 --build=编译 ...

  5. HahsRouter hash 路由

    无刷新页面,切换视图,用hash 实现路由切换,本身附带history记录,简单舒服. 最近用vue,看到vue-route的路由,做单页应用切换视图真心易如反掌,分分钟爽到不行.为了加深理解其内涵原 ...

  6. 听说awk语言也可以编写脚本

    导读 从 awk 系列开始,我们都是在命令行或者脚本文件里写一些简短的 awk 命令和程序.然而 awk 和 shell 一样也是一个解释型语言.通过从开始到现在的一系列的学习,你现在能写可以执行的 ...

  7. PYTHON 深拷贝,浅拷贝

    声明:本篇笔记,模仿与其它博客中的内容 浅拷贝 浅拷贝,在内存中只额外创建第一层数据 import copy n1 = {"k1": "wu", "k ...

  8. ToolStripMenuItem

    MenuStrip 类 为窗体提供菜单系统. 继承层次结构 System.Object  System.MarshalByRefObject    System.ComponentModel.Comp ...

  9. WPF三大模板简介(Z)

    WPF三大模板简介   WPF支持以下类型的模板: (1) 控件模板.控件模板可以将自定义模板应用到某一特定类型的所有控件,或是控件的某一实例.决定控件外观的是ControlTemplate,它决定了 ...

  10. effective OC2.0 52阅读笔记(六 块与大中枢派发)

    派发队列:dispatch_queue 操作队列:NSOperationQueue  组:dispathc_group_t 37 理解“块”这一概念 总结:块就是一个值,且自有其相关类型.块的强大之处 ...