zoukankan      html  css  js  c++  java
  • Is It A Tree?[HDU1325][PKU1308]

    Is It A Tree?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 22720    Accepted Submission(s): 5168


    Problem 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。
    每个点的入度要检查一下。
    在PKU上提交的注意空树也是一棵树,HDU上没有空树数据。
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>
    #include <string.h>
    using namespace std;
    class Union_Find_Set {
    #define MAX_UNION_FIND_SET_SIZE 100005
    public:
        int setSize;
        int father[MAX_UNION_FIND_SET_SIZE];
        Union_Find_Set() {
            setSize = 0;
        }
        Union_Find_Set(int x) {
            setSize = x;
            clear(x);
        }
        void clear(int x) {
            for (int i = 0; i < x; i++) {
                father[i] = i;
            }
        }
        int getFather(int x) {
            if (x != father[x]) {
                father[x] = getFather(father[x]);
            }
            return father[x];
        }
        bool merge(int a, int b) {
            a = getFather(a);
            b = getFather(b);
            if (a != b) {
                father[a] = b;
                return true;
            } else {
                return false;
            }
        }
        int countRoot() {
            int ret = 0;
            for (int i = 0; i < setSize; i++) {
                if (father[i] = i) {
                    ret++;
                }
            }
            return ret;
        }
    };
    Union_Find_Set ufs(100001);
    bool v[100005];
    int in[100005];
    int main() {
        int a, b, k = 0, n = 0, m = 0;
        bool f = true;
        memset(v, false, sizeof(v));
        memset(in, 0, sizeof(in));
        while (scanf("%d%d", &a, &b) != EOF) {
            if (a == 0 && b == 0) {
                if (n != m + 1) {
                    f = false;
                }
                printf("Case %d is %sa tree.
    ", ++k, f ? "" : "not ");
                ufs.clear(100001);
                n = m = 0;
                memset(v, false, sizeof(v));
                memset(in, 0, sizeof(in));
                f = true;
            } else if (a < 0 && b < 0) {
                break;
            } else {
                m++;
                if (!v[a]) {
                    v[a] = true;
                    n++;
                }
                if (!v[b]) {
                    v[b] = true;
                    n++;
                }
                in[b]++;
                if (in[b] > 1) {
                    f = false;
                }
                if (!ufs.merge(b, a)) {
                    f = false;
                }
            }
        }
        return 0;
    }
    View Code
     
  • 相关阅读:
    kicad 基本操作
    RedHat centos中的上传下推脚本
    使用Node.js爬虫存储MySQL数据库
    解决vue和vue-template-compiler版本不同报错的问题
    Vue组件通信(父子组件通信)-学习笔记
    git 简单操作
    错误解决:redis.exceptions.ResponseError: unknown command 'SENTINEL'
    nginx做泛域名解析的域名的正则判断
    postman对字符串进行base64编码方法和变量的使用
    pymysql报错OperationalError: (2013, 'Lost connection to MySQL server during query')
  • 原文地址:https://www.cnblogs.com/dramstadt/p/6187155.html
Copyright © 2011-2022 走看看