并查集+欧拉

树的判定

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述

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.

 
输入
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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1.

输出
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).
样例输入
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
样例输出
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree. 此题判断是否为树,但是又加入了方向,首先什么是树?
1、只有一个根节点
2、只有唯一的一条路从根节点到其余节点
3、任意两点之间也只有一条通路
再来判断方向
1、每个节点只能有一个入度
#include<stdio.h>
#include<string.h>
#define MAX 10100
#define maxn(a,b)(a>b?a:b)
int set[MAX],ru[MAX],chu[MAX];
int find(int fa)
{
int t;
int ch=fa;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int n,m,j,i,sum,a,b,k,max,wrong,mistake;
k=1;
while(1)
{
max=0;
memset(set,0,sizeof(set));
memset(ru,0,sizeof(ru));
mistake=0;
while(scanf("%d%d",&a,&b)&&a!=0&&b!=0)
{
if(a==-1&&b==-1)
return 0;
if(set[a]==0)
set[a]=a;
if(set[b]==0)
set[b]=b;
if(max<maxn(a,b))
max=maxn(a,b);
ru[b]++;
if(find(a)==find(b))//两个节点在合并之前已经联通
{
mistake=1;
}
mix(a,b);
}
if(mistake)
printf("Case %d is not a tree.\n",k++);
else
{
sum=0;wrong=0;
for(i=1;i<=max;i++)
{
if(ru[i]>1) //节点的入度大于1不符合树的要求
{
wrong=1;
break;
}
if(set[i]==i)//判断根节点个数
{
sum++;
if(sum>1)
{
wrong=1;
break;
}
}
}
if(wrong)
printf("Case %d is not a tree.\n",k++);
else
printf("Case %d is a tree.\n",k++);
}
}
return 0;
}

  

nyoj 129 树的判定的更多相关文章

  1. NYOJ 129 树的判定 (并查集)

    题目链接 描述 A tree is a well-known data structure that is either empty (null, void, nothing) or is a set ...

  2. hihoCoder#1322(树的判定)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶点 M 条边的无向图 G ,判断 G 是不是一棵树. 输入 第一个是一个整数 T ,代表测试数据的组 ...

  3. [HIHO1322]树结构判定(并查集)

    题目链接:http://hihocoder.com/problemset/problem/1322 给一个图,判断这个图是不是一棵树. 判定的方法:首先是连通图,其次所有点的入度都小于等于1. /* ...

  4. 笔试算法题(03):最小第K个数 & 判定BST后序序列

    出题:输入N个整数,要求输出其中最小的K个数: 分析: 快速排序和最小堆都可以解决最小(大)K个数的问题(时间复杂度为O(NlogN)):另外可以建立大小为K的最大堆,将前K个数不断插入最大堆,对于之 ...

  5. 二级C语言题集

    时间:2015-5-13 18:01 在131题之后是按考点分类的题集,有需要的朋友可以看一下 ---------------------------------------------------- ...

  6. 图书馆管理系统SRS

    1.任务概述 1.1目标 主要提供图书信息和读者基本信息的维护以及借阅等功能.本系统是提高图书管理工作的效率,减少相关人员的工作量,使学校的图书管理工作真正做到科学.合理的规划,系统.高效的实施. 1 ...

  7. bzoj1758

    好题显然是分数规划,二分答案之后我们要找是否存在一条边数在[l,u]长度和为正的路径可以用树的分治来解决这个问题我们假设当前处理的是过点root的路径显然我们不好像之前男人八题里先算出所有答案,然后再 ...

  8. NYOJ129 决策树 【并检查集合】

    树的判定 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 A tree is a well-known data structure that is either e ...

  9. myapp——自动生成小学四则运算题目的命令行程序(侯国鑫 谢嘉帆)

    1.Github项目地址 https://github.com/baiyexing/myapp.git 2.功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 功能(已全部实现) 使用 -n ...

随机推荐

  1. hdu 4557 非诚勿扰

    水题…… 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include<io ...

  2. Android 国际化文字

    本例演示中英文切换 在resource文件夹添加values-zh,这个文件夹对应中文环境的values文件夹,然后添加strings.xml: <?xml version="1.0& ...

  3. [模拟]ZOJ3480 Duck Typing

    题意:给了一坨...按题目意思输出就好了... 给一组案例 begin class d class c:d class b:c class a:b def d.m def d.n call a.m e ...

  4. Java List详解

    就是一种集合对象,将所有的对象集中到一起存储. list里面可以放java对象,可以直接放值. List list = new ArrayList(); list.add("AAA" ...

  5. R语言学习笔记:取数据子集

    上文介绍了,如何生成序列,本文介绍一下如何取出其数据子集 取出元素的逻辑值 > x<-c(0,-3,4,-1,45,90,5) > x>0 [1] FALSE FALSE  T ...

  6. 解决git Push时请求username和password,而不是ssh-key验证

    转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...

  7. UVa 11082 (网络流建模) Matrix Decompressing

    网络流不难写,难的建一个能解决问题的模型.. 即使我知道这是网络流专题的题目,也绝不会能想出这种解法,=_=|| 题意: 给出一个矩阵的 前i行和 以及 前i列和,然后找到一个满足要求的矩阵,而且每个 ...

  8. 利用Merge Into 更新表,集合数据到数据库中

    使用Merge INTO 将表数据更新到数据库中 创建User-Defined Table Types   创建要更新的UserDetails表 创建更新存储过程 程序调用存储过程 查看结果

  9. DB2创建序列

    一.创建序列 序列是按照一定的规则生产的数值,序列的作用非常的大,比如银行交易中的流水号,就是记录每笔交易的关键字段. 通过create sequence语句创建序列,具体语法如下: >> ...

  10. JDBC 与ODBC的区别

    一.ODBC(Open   DataBase   Connectivity   :  开放数据库连接)         ODBC  总体结构  应用程序    执行处理并调用odbc函数,提交sql语 ...