并查集+欧拉

树的判定

时间限制: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. ADT(android-bundler) HTML EDIT 编辑 xml HTML

    逗比的ADT,安装个html一直不成功,最后发现了如下方法 Helper_Install New Software_ http://download.eclipse.org/releases/indi ...

  2. [Unity菜鸟] 材质

    1. 材质定义: 2. 把材质都改成支持透明通道 因为物体太多了,比如树跟房子材质必须用不一样的.所以办法还是你得改每个材质的Shader,都改成支持透明通道的. 在Project的搜索窗口输入t: ...

  3. inflate方法与findViewById的区别

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来找 re ...

  4. DBNull.Value 字段的用法

    DBNull 是一个单独的类,这意味着该类只能存在此实例.它指数据库中数据为空(<NULL>)时,在.net中的值 如果数据库字段的数据缺失,则您可以使用 DBNull.Value 属性将 ...

  5. Jdk5.0中出现的新特性

    掌握jdk5.0中出现的新特性1.泛型(Generics)2.增强的"for"循环(Enhanced For loop)3.自动装箱/自动拆箱(Autoboxing/unboxin ...

  6. Android安全问题 静音拍照与被拍

    之前写过一些android手机root之后的安全问题的(文章列表见末尾),其实我是想到哪就写到了,今天整理硬盘,发现一年之前写过一段代码,所以今天我们就一起来讨论一下. 注:这里不是讨论多nb的技术, ...

  7. c++编写webui内核 .

    http://blog.csdn.net/sx1989827/article/details/8068779 #pragma once #include <mshtmhst.h> #inc ...

  8. PHP 日期计算函数【差异天数】

    function count_days($a,$b){ $a_dt=getdate($a); $b_dt=getdate($b); $a_new=mktime(12,0,0,$a_dt['mon'], ...

  9. 如何写mysql的定时任务

     什么是事件: 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 查看是否开启: show variables li ...

  10. poj 1035 Spell checker(水题)

    题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...