判断是否是树(Is It A Tree?)

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.

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=94494#problem/B

问题描述:

输入多组测试案例,每组测试案例以-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 = 0; i < max_num; i++)
    {
        node[i].vis = 0;
        node[i].root = i;
        node[i].conn = 0;
    }
}
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 = 1;
    init();
    //freopen("input.txt", "r", stdin);
    while(scanf("%d%d", &n, &m), n >= 0 && m >= 0)
    {
        if(!flag && (n != 0 && m != 0))
            continue;
        if(n == 0 && m == 0)
        {
            int root_num = 0;
            for(int i = 1; i < max_num; i++)
            {
                if(find_root(i) == i && node[i].vis)
                    root_num++;
                if(node[i].conn > 1)
                    flag = false;
            }
            if(root_num > 1)
                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 = 1;
            node[m].vis = 1;
            node[m].conn++;
            union_set(n, m);
        }
    }
    return 0;
}
时间: 2024-10-23 20:51:52

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

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes h

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

水题(并查集+判断是否是树)(详细测试数据)

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 d

[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

判断两个树是否互相镜像

// 3. 判断两个树是否互相镜像 public static boolean isMirrorRec(TreeNode r1, TreeNode r2){ // 如果两个树都是空树,则返回true if(r1==null && r2==null){ return true; } // 如果有一棵树是空树,另一颗不是,则返回false if(r1==null || r2==null){ return false; } // 如果两个树都非空树,则先比较根节点 if(r1.val != r2

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

Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可).问最少还需要建设多少条道路? Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M:随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号.为简单起见,城镇从1到N编号. 注意

判断一棵树是否是另一棵树的子树

问题 判断一棵树是否是另一棵树的子树,如图 思路 问题分两步: 找值相同的根结点(遍历解决) 判断两结点是否包含(递归:值.左孩子.右孩子分别相同) 代码 bool IsPart(TreeNode *root1, TreeNode *root2) { if (root2 == NULL) return true; if (root1 == NULL) return false; if (root1->val != root2->val) return false; return IsPart(

判断一棵树是否为完全二叉树

完全二叉树:若一棵二叉树具有具有n个节点,它的每个节点都与高度为k的满二叉树编号为0~n-1结点一一对应,则称这可二叉树为完全二叉树. 方法一:一维数组存储 根据完全二叉树的定义和性质,利用一位数组作为完全二叉树的存储,如下图 由图,节点的编号与数组元素的下标是一一对应的,可根据二叉树的性质,可方便找出下标 为i的的双亲结点a[i/2]及左右孩子结点a[i*2],a[i*2+1].这样判断一棵树是否为二叉树,应该对此二叉树从上到下,从左到右依次编号, 然后把编好的号依次存入一位数组中,在与相应深

判断一棵树是否是二叉搜索树

前两天写过一篇博文<二叉搜索树基本操作实现>,为了更深入了解二叉搜索树的性质,本文实现判断一棵树是否为二叉搜索树算法. 二叉搜索树的性质: 任意节点的键值一定大于其左子树中的每一个节点的键值,并小于其右子树中的每一个节点的键值. 构造二叉树的节点定义为: struct TreeNode{ int data; TreeNode *left; TreeNode *right; }; 方法1 (错误) 对每一个节点,检测其值是否大于左子树节点,是否小于右子树节点.思路很简单,代码实现如下: bool

判断一棵树是否是二叉平衡树

主要就是判断二叉树深度进行改造.判断条件为左树为平衡树,右树为平衡树,并且左树的高度和右树的高度插不超过-1:public class IsAVL { public static class Node{ private Node left; private Node right; private int value; public Node(int value){ this.value = value; } } public static int isAVL(Node head){ if(hea