给出一堆边给你,让你判断这是不是一棵树。边的信息以(start , end)的形式给出.
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.

方法一:直接使用树的定义来做。
    树的性质

        1)树的节点数比边数多1(空树除外)

        2) 树中除根节点的每个节点只有唯一的父节点。

      条件1可以排除有环的图,条件2可以排除多个根的图。

      特别注意的是,空树不满足条件1,需单独判断!

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int pre[100010];
int main()
{
int s=0;
while(1)
{
s++;
int x,y,s1=0,k=0;
memset(pre,0,sizeof(pre));
while(~scanf("%d%d",&x,&y)&&(x+y))
{
if(x==-1&&y==-1) return 0;
pre[x]=1;
pre[y]=1;
k++;
}
for(int i=0;i<100010;i++)
{
if(pre[i])
s1++;
}
if(s1==0||s1==k+1)//树的节点数比边数多1(空树除外)
printf("Case %d is a tree.\n",s);
else
printf("Case %d is not a tree.\n",s);
}
}

方法二:并查集

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1000; int p[maxn]; //存父亲节点
int r[maxn]; //存入度
bool used[maxn]; //判断下标是否使用
int Max, Min; //判断树中使用的下标的最值 void set() //初始化
{
for(int x = 0; x < maxn; x++)
{
p[x] = x; //自己为自己的父亲节点
r[x] = 0; //各点独立 入度为 0
used[x] = false; //各点均未使用
}
Min = maxn;
Max = -maxn;
} int find(int x) //找根节点
{
return x == p[x] ? x : p[x] = find(p[x]);
} void Union(int x, int y) //合并两点所在的树
{
int fx = find(x);
int fy = find(y);
p[fy] = fx;
}
int main()
{
int a, b;
int C = 0; //记录是第几组数据
while(scanf("%d%d", &a, &b) != EOF)
{
if(a == -1 && b == -1) break;
set(); //初始化 p[b] = a; r[b]++; //处理第一对数据
Min = min(a, b);
Max = max(a, b);
used[a] = true; //标记使用
used[b] = true; int x, y;
bool flag = true;
while(scanf("%d%d", &x, &y) != EOF)
{
if(x == 0 && y == 0)
{
int root = 0, index; //记录根节点个数 和下标
for(int i = Min; i <= Max; i++)
{
if(used[i] && i == p[i]) //判断有几个根
{
root++; index = i;
}
} if(root != 1) flag = false; //一棵树只能有一个根 if(flag)
{
for(int i = Min; i <= Max; i++) //判断入度情况,除了根节点,其它节点入度均为1
{
if(i != index && used[i] && r[i] != 1) flag = false;
}
} if(flag) printf("Case %d is a tree.\n", ++C);
else printf("Case %d is not a tree.\n", ++C);
//printf("%d\n", root); //开始有些小错误 输出中间变量,找错误
break; //注意:易忘记
} if(find(x) != find(y)) Union(x, y); //如果不在同一棵树中,则合并 r[y]++; //入度+1
used[x] = true; //标记使用
used[y] = true;
Min = min(Min, x); Min = min(Min, y); //更新最值
Max = max(Max, x); Max = max(Max, y);
}
}
return 0;
}

B - B(Is It A Tree?)的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  10. Tree树节点选中及取消和指定节点的隐藏

    指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...

随机推荐

  1. String、StringBuffer 和 StringBuilder 的区别

    面试简答 区别: 1) String 长度大小不可变 2) StringBuffer 和 StringBuilder 长度可变 3) StringBuffer 线程安全 StringBuilder 线 ...

  2. Label_strange_labels

    空格符号  特殊符号名 注释语句 , align = "left right center" 粗体 斜体 也可能是控制标签 上标 下标 大字号 小字号 下划线 删除线 等宽 键盘输 ...

  3. 关于软件架构中的b/s

    **B/S架构 b/s只需要一个浏览器,用户就可以通过不同的网址访问不同的服务器程序. 优点:开发,安装,部署,维护简单 缺点:对硬件要求过高,用户的体验会受到影响 首先是资源分类:**可以分为静态资 ...

  4. 解决Establishing SSL connection without server‘s identity verification is not recommended.

    每次从数据库中进行查询或者其他操作控制台都会出现以下警告,虽说不是error,但是很显眼.. WARN: Establishing SSL connection without server's id ...

  5. python学习笔记 | 递归思想

    1.引子 大师 L. Peter Deutsch 说过: To Iterate is Human, to Recurse, Divine. 中文译为:人理解迭代,神理解递归 2.什么是递归 简单理解: ...

  6. Hdfs手动执行Balance

    问题发现: 经巡检,服务器中一台节点的hadoop磁盘占用过多,是其它节点的三倍,导致数据严重不均衡. 解决过程: 两种命令: hadoop的bin目录下,运行命令start-balancer.sh ...

  7. Java设计模式精讲之UML急速入门

    简单记录 - 慕课网 - Java设计模式精讲 Debug方式+内存分析 文章目录 第2章 UML急速入门 2-1.UML简单入门 UML定义 UML特点 UML 2.2分类 UML类图 理解泛化.实 ...

  8. 【Oracle】修改列的大小

    alter table 表名 modify column_name varchar2(32)  alter table 表名 modify (column_name1 varchar(20) defa ...

  9. 汇编学习笔记——DOS及DEBUG介绍

    转自:https://www.shiyanlou.com/courses/running/332 一.课程简介 声明:该课程基于<汇编语言(第2版)>郑晓薇 编著,机械工业出版社.本节实验 ...

  10. 今天聊点干货—关于CSS样式来源

    样式来源 CSS样式共有5个来源,它们分别是\(\color{#FF3030}{浏览器默认样式}\).\(\color{#FF3030}{用户样式}\).\(\color{#FF3030}{链接样式} ...