guodongxiaren / OJ

4 stars 3 forks source link

HDUOJ 1325:Is It A Tree? #1

Open guodongxiaren opened 4 years ago

guodongxiaren commented 4 years ago

http://acm.hdu.edu.cn/showproblem.php?pid=1325

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. image image image

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 lineCase 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.

Source

North Central North America 1997

guodongxiaren commented 4 years ago

要点:

  1. 不能是图(存在环):入度不能大于1
  2. 不能是森林(存在多个根):不能有多个入度为0的节点。并查集更方便
  3. 输入输出格式注意一下,以负数结束,不一定是-1结束。
guodongxiaren commented 4 years ago

MAX=1034,然后rudu和parent里面直接用[MAX]会失败。。奇怪啊

#include <string.h>
#include <iostream>
using namespace std;
const int MAX = 1024;
int rudu[MAX+10];
int parent[MAX+10];

void init() {
    memset(parent, -1, sizeof(parent));
    memset(rudu, -1, sizeof(rudu));
}

int find(int a) {
    if (parent[a] != a) {
        parent[a] = find(parent[a]);
    }
    return parent[a];
}

void merge(int a,int b) {
    int p1 = find(a);
    int p2 = find(b);
    if (p1 == p2)
        return;
    parent[p1] = p2;
}

int main() {
    int a,b,c=1;

    init();
    while (cin>>a>>b, a >= 0 && b >= 0) {
        if (rudu[a] == -1) {
            rudu[a] = 0;
            parent[a] = a;
        }
        if (rudu[b] == -1) {
            rudu[b] = 0;
            parent[b] = b;
        }
        if (!a && !b) {
            int root_num = 0;
            bool flag = true;
            for (int i = 1; i <= MAX; i++) {
                if(rudu[i] > 1) {
                    flag = false;
                    break;
                }
                if(parent[i] == i) {
                    if (++root_num > 1) {
                        flag = false;
                        break;
                    }
                }
            }
            if(flag) {
                cout<<"Case "<<c++<<" is a tree."<<endl;
            } else {
                cout<<"Case "<<c++<<" is not a tree."<<endl;
            }
            init();
            continue;
        }
        if (find(a) != find(b)) {
            merge(a, b);
        }
        rudu[b]++;
    }
}
guodongxiaren commented 4 years ago

并查集寻找root的函数,也可以这样写:

int find(int a) {
    if (parent[a] != a) {
        return find(parent[a]);
    }
    return a;
}

int find(int a) {
    if (parent[a] == a) {
        return a;
    }
    return find(parent[a]);
}

本次展示的这两种,属于尾递归。尾递归都能改成迭代写法,比如:

int find(int a) {
    for (;parent[a] != a;) {
        a = parent[a];
    }
    return a;
}

尾递归会节省栈空间。但是空间几乎不是瓶颈。 并且这两种不会修改parent的指向。而之前那种,每次find会顺便做打平操作。也就是每次find之后,同属一个集合的节点都会直接挂到root下面(学名:路径压缩),后续查找效果更高。所以原先那种find。虽然不受尾递归,但是递归深度更浅,所以占用的栈空间也不多!