给出一堆边给你,让你判断这是不是一棵树。边的信息以(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. Map遍历的几种方式

    代码示例 /** * @author liaowenhui * @date 2020/6/25 11:15 */ public class TestMap { public static void m ...

  2. [leetcode] 周赛 223

    比赛题目:https://leetcode-cn.com/contest/weekly-contest-223/. 解码异或后的数组 题目:1720. 解码异或后的数组. 还记得数列求和的「累加法」? ...

  3. PHP 插件资源

    PHP   jsonRPC  百度云网盘地址  https://pan.baidu.com/s/1itCIhrdd5bPGJMefNUuKvw   提取码 : ax4d PHP Excel 百度云网盘 ...

  4. LeetCode344 反转字符串

    编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man, a p ...

  5. 静默(命令行)安装oracle 11g

    CentOS 6 静默安装oracle 11g 我参考的这个,他非常详细:https://blog.csdn.net/JIANG123456T/article/details/77745892 我只是 ...

  6. Deep Learn I'm back.

    Intorduction: 时隔好几个月,我准备重新进入Deep Learning 的领域.昨天和老师聊了很多,之前觉得我做的工作就是排列组合,在水论文,灌水.但老师却说:这也是为将来的研究打基础. ...

  7. Mac Navicat premium 12 连接mysql8.0.21出现 'caching_sha2_password' 解决方案

    1.通过命令 select user,plugin from user where user='root'; 我们可以发现加密方式是caching_sha2_password. 2.  修改查看加密方 ...

  8. linux线程数限制与zabbix监控

    Linux最大线程数限制及当前线程数查询 最大线程数计算方式: n = total_memory/128k; Linux用户线程数限制而导致的程序异常为 java.lang.OutOfMemoryEr ...

  9. 【Docker】Docker启动停止重启 Redirecting to /bin/systemctl start docker.service

    [root@liuawen local]# docker -v Docker version 1.13.1, build cccb291/1.13.1 [root@liuawen local]# 启动 ...

  10. mysql的逻辑备份和恢复

    备份指定的数据库或此数据库中的某些表 mysqldump [options] db_name [tables] >backup.sql 备份指定的一个或多个数据库 mysqldump --dat ...