【编程题解】Tree or not?

Description

Please input the sequence of N pairs of numbers (for example: N = 2, the sequence of numbers is (1, 2), (2, 3)). These numbers represent the nodes of the tree, and the nodes represented by the numbers on the left are the parent nodes of the nodes represented by the numbers on the right.

Please generate a tree based on the entered number pair sequence and print the tree according to the breadth first order.

If the input cannot generate a tree, such as (1, 2), (1, 3), (2, 4), (3, 4), the graph generated from the sequence, node 2 and node3 are node 4’s parent nodes at the same time, so the graph is not a tree by definition.

Then please output the string "Not a tree" at this time.

Breadth first traverses the tree, and when printing out, the nodes at the same level are printed out according to the order in which the nodes appear at the time of input.

Input

The first line contains the integer N.

The next N lines, each line contains two different positive integers, representing apair of numbers, separated by commas between the two numbers, for example: 1,3.

Without the default input order, the first line may not be the root node, and the last line may not be the leaf node.

There may be completely duplicate rows.

Data range:1≤N≤10000

The positive integers in the integer pairs are all less than 2^31

Output

The output is one line. If spanning tree can be generated, please output the numbers corresponding to each node according to the breadth first order,separated by spaces. The nodes at the same level are output according to the input order.

If the tree cannot be generated, output the string "Not a tree".

Sample Input 1 

5
3,6
2,4
1,3
2,5
1,2

Sample Output 1

1 3 2 6 4 5

Personal Answer  (using language:JAVA)  Not necessarily right

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());

        LinkedHashSet<String> set=new LinkedHashSet<String>();
        for (int i = 0; i < n; i++) {
            set.add(scanner.nextLine());
        }

        int real_num=set.size();

        int[] first = new int[real_num];
        int[] second = new int[real_num];
        HashSet<Integer> node=new HashSet<Integer>();
        for (int i = 0; i < real_num; i++) {
            String str = (String)set.toArray()[i];
            String[] result = str.split(",");
            first[i] = Integer.parseInt(result[0]);
            second[i] = Integer.parseInt(result[1]);
            node.add(first[i]);
            node.add(second[i]);
        }

        for (int i = 0; i < real_num; i++) {
            for (int j = 0; j < real_num; j++) {
                if (i != j && second[i] == second[j] && first[i] != first[j]) {
                    System.out.println("Not a tree");
                    System.exit(1);
                }
            }
        }

        int node_num=node.size();
        int []print_array=new int[node_num];

        for (int i = 0; i < real_num; i++) {
            int root_tmp=first[i];
            boolean tip=true;
            for (int j = 0; j < real_num; j++) {
                if (root_tmp==second[j]) {
                    tip=false;
                }
            }
            if(tip){
                print_array[0]=root_tmp;
            }
        }

        int read_pointer=0;
        int write_pointer=1;
        while(write_pointer!=node_num){
            for (int i = 0; i < real_num; i++) {
                if(print_array[read_pointer]==first[i]){
                    print_array[write_pointer]=second[i];
                    write_pointer++;
                }
            }
            read_pointer++;
        }

        for (int i = 0; i < node_num; i++) {
            System.out.print(print_array[i]);
            if(i!=(node_num-1)){
                System.out.print(" ");
            }
        }

    }
}

Welcome to communicate!

猜你喜欢

转载自blog.csdn.net/Ximerr/article/details/106751386