Adjacency List to Adjacency Matrix

Easy

Given the nodes and adjacency list of a graph, calculate the adjacency matrix for the it.

You have a graph with n nodes indexed from 0 to n-1. You are also given the adjacency list for each node.

You have to return the adjacency matrix for the given graph.

Example
adjacency-list-to-adjacency-matrix

Testing

Input Format

The first line contains an integer T denoting the number of test cases.

For each test case, the input has the following lines:

  • The first line contains the integer n.
  • The next n lines descibes the adjacent nodes for the ith node.
    • The first integer m denotes the number of adjacent nodes.
    • The next m integers denote adjacent nodes.

Output Format

For each test case, the output has n lines denoting the adjacency matrix of the graph.

Sample Input

2
4
3 1 2 3
1 0
2 0 3
2 0 2
4
3 1 2 3
1 0
2 0 3
3 0 2 3

Expected Output

0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 1

Constraints

1 <= T <= 10
1 <= n <= 500
0 <= m <= n

Editorial Link: Editorial