Description

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结束
每组案例中输入多组数据n,和m, 其中n指向m, 输入以0,0结束
判断上述输入得到的图是否是树
 
解题思路:
   满足树的条件:
        1:只能有一个根节点
        2:不能有环
        3:根节点入度为0, 其余节点入度为1(判断的时候直接判断是否所有节点的入度都小于等于1即可)
 
  一直wrong answer的原因:
              1:刚开始一直忘记删除重定向语句,我哩个去,一直纠结,够了,够了,这都发现不了
              2:判断树的时候只用了两个条件 
                 a:只能有一个树根
                 b:不能有环
               我原来以为满足这两个条件,所有节点的入度就都满足条件了,结果发现也可能存在节点入度大于1的情况
#include <stdio.h>
#define max_num 100000+10
typedef struct
{
int vis, root, conn;
}Node;
Node node[max_num];
void init()
{
for(int i = ; i < max_num; i++)
{
node[i].vis = ;
node[i].root = i;
node[i].conn = ;
}
}
int find_root(int x)
{
while(x != node[x].root)
x = node[x].root;
return x;
}
void union_set(int x, int y)
{
x = find_root(x);
y = find_root(y);
if(x != y)
node[y].root = x;
}
int main()
{
int n, m;
bool flag = true;
int case_num = ;
init();
//freopen("input.txt", "r", stdin);
while(scanf("%d%d", &n, &m), n >= && m >= )
{
if(!flag && (n != && m != ))
continue;
if(n == && m == )
{
int root_num = ;
for(int i = ; i < max_num; i++)
{
if(find_root(i) == i && node[i].vis)
root_num++;
if(node[i].conn > )
flag = false;
}
if(root_num > )
flag = false;
if(flag)
printf("Case %d is a tree.\n", case_num++);
else
printf("Case %d is not a tree.\n", case_num++);
flag = true;
init();
continue;
}
if(n != m && find_root(n) == find_root(m))
flag = false;
else
{
node[n].vis = ;
node[m].vis = ;
node[m].conn++;
union_set(n, m);
}
}
return ;
}

判断是否是树(Is It A Tree?)的更多相关文章

  1. poj1308+HOJ1325,判断是否为树

    POJ 应该是判断是否为简单无环连通图,用并查集直接秒杀即可,而HOJ的是有向树,还需判断所有点的入度必需小于2,用一个类似hash[]数组判断一下即可, ////判断树之一:入度<=1:三:点 ...

  2. 动态树之LCT(link-cut tree)讲解

    动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...

  3. [剑指Offer]判断一棵树为平衡二叉树(递归)

    题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...

  4. HDU-1232 畅通工程 (并查集、判断图中树的棵数)

    Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相 ...

  5. 判断一棵树是否为二叉搜索树(二叉排序树) python

    输入一棵树,判断这棵树是否为二叉搜索树.首先要知道什么是排序二叉树,二叉排序树是这样定义的,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的 ...

  6. 【UOJ#388】【UNR#3】配对树(线段树,dsu on tree)

    [UOJ#388][UNR#3]配对树(线段树,dsu on tree) 题面 UOJ 题解 考虑一个固定区间怎么计算答案,把这些点搞下来建树,然后\(dp\),不难发现一个点如果子树内能够匹配的话就 ...

  7. E - Is It A Tree? 并查集判断是否为树

    题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...

  8. 15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)

    Level:   Easy 题目描述: Given two non-empty binary trees s and t, check whether tree t has exactly the s ...

  9. POJ-1308 Is It A Tree?(并查集判断是否是树)

    http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empt ...

随机推荐

  1. UVa1339 Ancient Cipher

    #include <iostream>#include <string>#include <cstring> // for memset#include <a ...

  2. Trie implementation

    在学习 Aho - Corasick Automation Algorithm 之前,先学习一下,Trie 的实现过程: The name trie comes from its use for re ...

  3. cocos2d-x教程1 hello world

    HelloworldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...

  4. codility上的练习(5)

    codility出了lesson 5了. (1) 合法括号序列,包括( [ { ) ] }这6种字符的字符串,长度N在[0..200000]范围内,为其是否合法. 要求时间复杂度O(N),空间复杂度O ...

  5. ThinkPHP第十六天(redirect、join、视图模型)

    1.redirect /** * Action跳转(URL重定向) 支持指定模块和延时跳转 * access protected * @param string $url 跳转的URL表达式 * @p ...

  6. IE 弹出提示:由于无法验证发布者,所以Windows 已经阻止此软件

    由于无法验证发布者,所以Windows 已经阻止此软件 按如下步骤:1.打开Internet Explorer---菜单栏点“工具”---Internet选项--安全---自定义级别---安全设置-- ...

  7. mybatis日记配置Log4j

    拷贝log4j-1.2.16.jar到项目lib下 方式一是在src下新建一个log4j.xml文件,其具体内容如下 <?xml version="1.0" encoding ...

  8. MyEclipse 怎样手动编译整个项目

    去掉自动编译的对勾,项目上就有build project,在菜单project->>build automatically. MyEclipse 菜单 Project->Build ...

  9. Protel 99SE PCB 打印技巧

    1. 打开 Protel99SE PCB 设计文档.从菜单File 下单击Print/Preview 打印预览菜单.出现PCB 打印预览介面. 2.从File 下单击 Setup Printer 设置 ...

  10. Java GUI图形界面开发工具

    Applet 应用程序     一种可以在 Web 浏览器中执行的小程序,扩展了浏览器中的网页功能. 缺: 1.需要下载 Applet 及其相关文件 2.Applet 的功能是受限制的 优: 3.无需 ...