Path Finding
InterviewTime limit1sMemory limit256 MB
Decide for every ordered pair of vertices in a directed graph with up to 100 vertices whether a path of at least one edge connects them.
- Level
Easy3 of 10
- Topics
- Graph, Dynamic programming
- Solved
- No attempts yet
Problem
You are given an unweighted directed graph . Write a program that decides, for every pair of vertices , whether a path of positive length runs from to .
A path of positive length uses at least one edge. So the answer for is 1 only when some path leaves and comes back to .
Input
The first line contains the number of vertices ().
Each of the next lines contains one row of the adjacency matrix. If the -th number on the -th line is 1, an edge goes from to ; if it is 0, no such edge exists. The -th number on the -th line is always 0.
Output
Print the answer as an adjacency matrix over lines. If a path of positive length runs from vertex to vertex , print 1 as the -th number on the -th line, otherwise print 0. Separate the numbers on a line with a single space.