12065 - Tsai_DS_1115 Transitive Closure   

Description

For a given a directed graph with at most ten nodes, represented in an adjacency matrix, please find out whether you can reach from any node to another node

Note: you may apply any one of the following algorithms. 

1. Floyd-Warshall

2. DFS

3. BFS

Input

The first line has an integer T ( 0 < T <= 10 ), indicating the number of nodes.

After the first line, it is a list of the T*T matrix with each row correspondings to the matrix row and the data sequence on each row corresponding to each column element on the row. For each (row#, column#) non-zero data value, there exists a directed edge from the node of id row# to the node of id column#.

The last line is a text  "end".

The input format is shown below.

           2

    i      0   1

 i+1   1   0

             j    j+1 

 

The output format is similar to the input format, except you do not need to out the matrix size and the last line "end". Each element with value one implies that you find a path from the node of id row# to the node of id column#.

Note: There is only one test graph in one test case.

Output

For each test case, after your program read in “end”, please print out your answer in the form of the transitive closure matrix.

You have to print a space between every number (but don't print a space after the last digit of each line), and you have to print '\n' to separate lines.

 

Sample Input  Download

Sample Output  Download

Tags




Discuss