首页 > 专题系列 > Java解POJ > POJ 1308 Is It A Tree? [解题报告] Java
2013
11-09

POJ 1308 Is It A Tree? [解题报告] Java

Is It A Tree?

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

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

样例输出:

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

解题代码:

import java.io.BufferedInputStream;   
import java.util.Scanner;   
public class Main {   
  
    public static void main(String[] args) {   
        final int MAXSIZE = 10000;   
        int[] edge1 = new int[MAXSIZE];   
        int[] edge2 = new int[MAXSIZE];   
        Scanner scan = new Scanner(new BufferedInputStream(System.in));   
        String r1 = "is a tree.";   
        String r2 = "is not a tree.";   
        int caseI = 1;   
        int root = 0;
        while (scan.hasNext()) {   
            int a = scan.nextInt();   
            int b = scan.nextInt();   
            if (a == -1 && b == -1) {   
                break;   
            }   
            int j = 0;   
            for (j = 0;; j++) {   
                if (a == 0 && b == 0) {   
                    break;   
                }   
                edge1[j] = a;   
                edge2[j] = b;   
                a = scan.nextInt();   
                b = scan.nextInt();   
            }   
            if (j == 0) {   
                System.out.println("Case " + caseI + " " + r1);   
                caseI++;   
            } else {   
                String result = "";   
                boolean resultFlag = true;   
                DisjointSet dj = new DisjointSet(MAXSIZE);   
                for (int jj = 0; jj < j; jj++) {   
                    if (dj.parent[edge1[jj]] == 0) {   
                        dj.init(edge1[jj]);   
                    }   
                    if (dj.parent[edge2[jj]] == 0) {   
                        dj.init(edge2[jj]);   
                    }   
                    root = edge1[jj];   
                    int ra = dj.find(edge1[jj]);   
                    int rb = dj.find(edge2[jj]);   
                    if (ra == rb) {   
                        resultFlag = false;   
                        break; 
                    } else {   
                        dj.union(ra, rb);   
                    }   
                }   
                result = "Case " + caseI + " ";   
                if (resultFlag) {   
                    int k;   
                    for (k = 0; k < MAXSIZE; k++) {   
                        if (dj.parent[k] != 0) {   
                            if (dj.find(k) != dj.find(root)) {   
                                result += r2;   
                                break;   
                            }   
                        }   
                    }   
                    if (k == MAXSIZE) {   
                        result += r1;   
                    }   
                } else {   
                    result += r2;   
                }   
                System.out.println(result);   
                caseI++;   
            }   
  
        }   
    }   
}   
  
class DisjointSet {   
  
    protected int n;   
    protected int[] parent;   
    protected int[] rank;   
  
    public DisjointSet(int n) {   
        this.n = n;   
        init();   
    }   
  
    private void init() {   
        this.parent = new int[this.n + 1];   
        this.rank = new int[this.n + 1];   
    }   
  
    protected void init(int i) {   
        parent[i] = i;   
        rank[i] = 1;   
    }   
  
    protected int find(int x) { 
        if (parent[x] != x) {   
            parent[x] = find(parent[x]);   
        }   
        return parent[x];   
    }   
  
    protected void union(int ra, int rb) {   
        if (rank[ra] > rank[rb]) {   
            parent[rb] = ra;   
        } else if (rank[ra] < rank[rb]) {   
            parent[ra] = rb;   
        } else {   
            rank[rb]++;   
            parent[ra] = rb;   
        }   
    }   
}

  1. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  2. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept