并查集+欧拉

树的判定

时间限制: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. javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)

    function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFun ...

  2. Java Web开发 之小张老师总结EL、JSP、Servlet变量

    EL 11 JSP 9 Servlet JSP类别 pageContext pageContext * 作用域 pageScope pageContext.getAttribute() * reque ...

  3. perl杂项

    perl -pi -e 's|googleapis.com|useso.com|g' `find ./ -type f` yingc@yingc:~/gcyin/test/thirdparty/ffm ...

  4. ajax readyState的五种状态详解

    通过ajax的readyState的值,我们可以知道当前的这个http请求处于什么状态.对于web的调试是比较重要的. readyState 状态说明: (0)未初始化 此阶段确认XMLHttpReq ...

  5. 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!

    启动 Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误的解决方法 http://blog.csdn.net/z ...

  6. P112、面试题16:反转链表

    题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点.链表结点定义如下:struct ListNode{      int    m_nKey;      ListNode* ...

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

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

  8. js设置datagriad的行移动

    //                    ,//                formatter: function(value,row,index){//                    ...

  9. 2013 ACM/ICPC Asia Regional Changsha Online - E

    第一个被板刷的题 取余 依次算在周几 #include <iostream> #include<cstdio> #include<cstring> #include ...

  10. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...