并查集+欧拉

树的判定

时间限制: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 1760 A New Tetris Game 博弈论

    找sg值,可以选择暴力,也可以利用sg值的特点简化. 暴力就跟取石子一样,没什么差别,DFS搞定.把矩阵看成一个字符串,字符串就是一个状态. 其实我们也可以不暴力求sg值,因为只要当前状态能到达一个s ...

  2. web页面浮动回到顶部功能和浮动广告

    实现测试浮动回到顶部 法一:用js实现<%@ Page Language="C#" AutoEventWireup="true" CodeBehind=& ...

  3. maven更新总结与tomcat发布方法总结

    这些天来一直为不能直接把项目实时的发布到tomcat而费心思,项目使用了maven来组织,编译和运行,而maven插件的安装曾经有些问题,为此怀疑不能发布项目到tomcat是因为maven有问题,为些 ...

  4. 第二个C语言代码

    有问题,还没找出哪里出错了       输入一串字符,问号结束 统计1~9各出现的次数 ******************************************************** ...

  5. Source Insight 安装使用

    习惯了在source insight下编辑阅读源码,在linux下用vi总是用不好 ,还是在Ubuntu上用回熟悉的source insight. 在Ubuntu中,安装Windows程序用wine, ...

  6. 【HDOJ】4601 Letter Tree

    挺有意思的一道题,思路肯定是将图转化为Trie树,这样可以求得字典序.然后,按照trie的层次求解.一直wa的原因在于将树转化为线性数据结构时要从原树遍历,从trie遍历就会wa.不同结点可能映射为t ...

  7. 【HDOJ】2242 考研路茫茫——空调教室

    tarjan缩点,然后树形dp一下可解.重点是重边的处理. /* 2242 */ #include <iostream> #include <sstream> #include ...

  8. Linux下执行程序出现 Text file busy 提示时的处理方式

    使用 fuser xxx 命令查看xxx文件被哪个进程占用,然后关闭该进程,解决问题. # fuser xxxxxx:              2878# kill -9 2878 注:xxx是文件 ...

  9. mysql 触发器学习(可以将mysql数据同步到redis)

    1. 一个简单的例子 1.1. 创建表: create table t(s1 integer); 1.2. 触发器: delimiter | create trigger t_trigger befo ...

  10. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...