给出一堆边给你,让你判断这是不是一棵树。边的信息以(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. Spring Boot -- 外部配置的属性使用

    Spring Boot允许使用propertities文件.yaml文件或者命令行参数作为外部配置. 命令行参数配置 Spring Boot可以基于jar包运行,打成jar包的程序可以直接通过下面的命 ...

  2. (数据科学学习手札102)Python+Dash快速web应用开发——基础概念篇

    本文示例代码与数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的新系列教程Python+Dash快 ...

  3. ORA-28001: the password has expired解决方法

    Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...

  4. 科来网络通讯协议图2019版(OSI七层模型)

    来源:http://www.colasoft.com.cn/download/protocols_map.php 自己把它转成了图片,好做查看:https://www.lanzous.com/ib5h ...

  5. selenium自动化 | 借助百度AI开放平台识别验证码登录职教云

    #通过借助百度AI开放平台识别验证码登录职教云 from PIL import Image from aip import AipOcr import unittest # driver.get(zj ...

  6. 环境变量IFS

    环境变量IFS的值是由1个空格.1个制表符.1个换行符依序构成的字符串,也就是" \t\n"字符串. #查看IFS变量值的长度: test ~ # expr length &quo ...

  7. ELK一个优秀的日志收集、搜索、分析的解决方案

    1 什么是ELK? ELK,是Elastaicsearch.Logstash和Kibana三款软件的简称.Elastaicsearch是一个开源的全文搜索引擎.Logstash则是一个开源的数据收集引 ...

  8. Java 栈的使用

    讲栈之前,要先讲一下Deque双端队列 既可以添加到队尾,也可以添加到队首 既可以从队首获取又可以从队尾获取 public interface Deque<E> extends Queue ...

  9. CMU数据库(15-445)实验2-B+树索引实现(下+课上笔记)

    4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...

  10. 前端工程构建之谈:gulp3要不要升级到Gulp4

    关于升级还是不升级,这是一个哲学问题. gulp4的语法更加现代,支持ES6的大部分写法,使用exports的方式去暴露任务组合,更加灵活和便捷. gulp4同时也提供了很多强大的API,例如para ...