Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Graph

There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation.

  An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$.

  An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise.

  Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively.

Input

  In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format:

$u$ $k$ $v_1$ $v_2$ ... $v_k$

 $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$.

Output

  As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$.

Constraints

$1 \leq n \leq 100$

Sample Input

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

Sample Output

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



Sample input:
4
1 2 2 4
2 1 4
3 0
4 1 3


Sample output:
0 1 0 1
0 0 0 1
0 0 0 0
0 0 1 0

Submission status: Accepted

Solution code:
n = int(input())

mat = [[0]*n for _ in range(n)]

for _ in range(n):
    In = list(map(int, input().split()))
    u, k, vs = In[0], In[1], In[2:]
    for v in vs:
        mat[u - 1][v - 1] = 1

for i in range(n):
    print(*mat[i])

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    G = [[0] * N for _ in range(N)]

    for _ in range(N):
        v, k, *U = il()
        for u in U:
            G[v - 1][u - 1] += 1

    for g in G:
        print(*g)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
Adj = [list(map(int, input().split())) for _ in range(n)]

m = [[0 for j in range(n)] for i in range(n)]
for i in range(n):
    if Adj[i][1]>0:
        for a in Adj[i][2:]:
            m[i][a-1] = 1
for e in m:
    print(*e)

Submission status: Accepted

Solution code:
n = int(input())

A = [[0] * n for i in range(n)]

for i in range(n):
    row = [ int(r) for r in input().split()]
    u = row[0] - 1
    if (row[1] > 0):
        for j in range(row[1]):
            v = row[2 + j] - 1
            A[u][v] = 1
            
for i in range(n):
    print(" ".join([str(st) for st in A[i]]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n = int(input())

ans = [[0 for _ in range(n)] for _ in range(n)]

for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] != 0:
        for j in range(2,2+adj[1]):
            ans[i][adj[j]-1] = 1
for i in range(n):
    print(*ans[i])

Submission status: Accepted

Solution code:
n = int(input())
Adj = []
for i in range(n):
    X = list(map(int, input().split()))[2:]
    for j in range(len(X)):
        X[j] -= 1
    Adj.append(X)
    
A = [[0]*n for _ in range(n)]
for i in range(n):
    for j in Adj[i]:
        A[i][j] = 1
for i in range(n):
    print(*A[i])

Submission status: Accepted

Solution code:
N = int(input())

for i in range(N):
    
    tmp = input().split()
    ans = [0]*N
    
    for j in range(int(tmp[1])):
        ans[int(tmp[2+j])-1] = 1

    print(*ans)

Submission status: Accepted

Solution code:
#演習2－10

n = int(input())
mat = [[0 for _ in range(n)] for _ in range(n)]

for i in range(n):
    cmd = input().split();
    m = int(cmd[1])
    for j in range(m):
        mat[i][int(cmd[2+j])-1] = 1

for i in mat:
    print(*i)

Submission status: Accepted

Solution code:
n=int(input())
adj=[list(map(int,input().split())) for _ in range(n)]

lst=[[0 for i in range(n)] for j in range(n)]
for x in adj:
    u,v=x[0],x[2:]
    for y in v:
        lst[u-1][y-1]=1

for x in lst:
    print(" ".join(map(str,x)))

Submission status: Accepted

Solution code:
N = int(input())
G = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
    u,k,*varray = map(int,input().split())
    for v in varray:
        G[u-1][v-1] = 1
    # for j in range(N):
    #     print(G[i][j])
for row in G:
    print(*row)

Submission status: Accepted

Solution code:
n = int(input())
l = []
tmp = 0 
for _ in range(n):
    a = list(map(int,input().split()))
    l.append(a)
    if a[len(a)-1] > tmp:
        tmp = a[len(a)-1]

ans = [[0 for i in range(tmp)] for j in range(n)]
for i in range(n):
    if l[i][1] > 0:
        for j in range(2,len(l[i])):
            tmp1 = l[i][j]
            ans[i][tmp1-1] = 1

for i in range(n):
    print(" ".join(map(str,ans[i])))

Submission status: Accepted

Solution code:
n=int(input())
graph={}

def show(nums):
    for i in range(len(nums)):
        if i!=0:
            if i!=len(nums)-1:
                print(nums[i],end=" ")
            else:
                print(nums[i])

for i in range(n):
    tmp=list(map(int,input().split()))
    graph[tmp[0]]=[0 for j in range(n+1)]
    for k in range(tmp[1]):
        graph[tmp[0]][tmp[2+k]]=1
    show(graph[tmp[0]])

Submission status: Accepted

Solution code:
n = int(input())
M = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    a,*b = map(int,input().split())
    a -= 1
    for i in b[1:]:
        M[a][i-1] = 1
for i in range(n):
    print(*M[i])

Submission status: Accepted

Solution code:
n = int(input())

for i in range(n):
    s = [int(x) for x in input().split()]
    s.reverse()

    u, k = s.pop(), s.pop()
    se = set()
    for j in range(k):
        se.add(s.pop())

    ans = []
    for i in range(1, n + 1):
        ans.append(int(i in se))

    print(*ans)

Submission status: Accepted

Solution code:
n = int(input())

for i in range(n):
    ans = [0] * n
    tmp = list(map(int, input().split()))
    u, k, v = tmp[0], tmp[1], tmp[2:]
    for j in v:
        ans[j-1] = 1
    print(' '.join(map(str, ans)))

Submission status: Accepted

Solution code:
N = int(input())
A = [[0]*N for _ in range(N)]

for _ in range(N):
    l = list(map(int, input().split()))
    u, k = l.pop(0), l.pop(0)
    
    row = A[u-1]
    for x in l:
        row[x-1] = 1

for row in A:
    print(*row)

Submission status: Accepted

Solution code:
n=int(input())
ans=[[0]*n for _ in range(n)]
for _ in range(n):
    v,k,*varray= map(int,input().split())
    for i in varray:
        ans[v-1][i-1]=1

for i in range(n):
    print(" ".join(map(str,ans[i])))

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    a = list(map(int,input().strip().split()))
    b = [str(int(i+1 in a[2:])) for i in range(n)]
    L=' '.join(b)
    print(L)

Submission status: Accepted

Solution code:
N = int(input())
ans = []
for i in range(N):
    a = list(map(int, input().split()))
    pre = [0 for _ in range(N)]
    for k in a[2:]:
        pre[k-1] = 1
    ans.append(pre)
for i in range(N):
    print(' '.join(list(map(str, ans[i]))))

Submission status: Accepted

Solution code:
N = int(input())
matrix = [[0] * N for _ in range(N)]

for i in range(N):
    tmp = list(map(int, input().split()))
    for j in range(tmp[1]):
        matrix[tmp[0]-1][tmp[j+2]-1] = 1

for i in range(N):
    print(*matrix[i])

Submission status: Accepted

Solution code:
N=int(input())
#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [[0 for j in range(N)] for i in range(N)]

for i in range(N):
    T=list(map(int,input().split()))
    if T[1]!=0:
        
        for j in range(T[1]):
            S[i][T[j+2]-1]=1

for i in S:
    print(*i)

Submission status: Accepted

Solution code:
n = int(input())

a = [[0]*n for x in range(n)]

for i in range(n):
  nood = list(map(int,input().split()))
  nood_elements = nood[0]-1
  for j in range(2,len(nood)):
    a[nood_elements][nood[j]-1] += 1
  
  for j in range(n-1):
    print(a[nood_elements][j],end=" ")
  print(a[nood_elements][n-1])

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    V = [list(map(int, input().split())) for _ in range(n)]
    ans = [[0 for _ in range(n)] for _ in range(n)]
    for v, k, *a in V:
        for j in a:
            ans[v - 1][j - 1] = 1
    for a in ans:
        print(*a)


resolve()

Submission status: Accepted

Solution code:
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()
    Adj = [[0]*n for _ in range(n)]
    for _ in range(n):
        ukv = LI()
        u = ukv[0]
        # k = ukv[1]
        v = ukv[2:]
        for i in v:
            Adj[u-1][i-1] = 1

    for i in Adj:
        print(*i)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
l = [list(map(int,input().split())) for _ in range(n)]
l1 = list( [0 for i in range(n)] for _ in range(n))
for i in l:
  a,b,*c = i
  for j in c:
    l1[a-1][j-1] = 1
for k in l1:
  print(*k)

Submission status: Accepted

Solution code:
import sys

n = int(input())
G = [[0] * n for i in range(n)]

for i in range(n):
    u, k, *A = map(int, input().split())
    for a in A:
        G[i][a-1] = 1

for g in G:
    print(*g)

Submission status: Accepted

Solution code:
n = int(input())

nodes = []
for i in range(n):
    tmp_nodes = [int(x) for x in  input().split()]

    tmp = 2
    j = 1
    while j <= n:
        if j != 1:
            print(" ", end = "")
        if tmp < len(tmp_nodes) and j == tmp_nodes[tmp]:
            print(1, end = "")
            tmp += 1
        else:
            print(0, end = "")
        j += 1
        

    print("")

Submission status: Accepted

Solution code:
n=int(input())
L=[]
for l in range(n):
    L.append([])
M=[]
for m in range(n):
    mm=[0]*n
    M.append(mm)

for i in range(n):
    inp=list(map(int,input().split()))
    #print()
    L[inp[0]-1]=inp[2:]

for i,l in enumerate(L):
    for k in l:
        M[i][k-1]=1

for m in M:
    print(' '.join(map(str,m)))

Submission status: Accepted

Solution code:
n = int(input())
ls = [[] for i in range(n)]
for i in range(n):
    s = list(map(int,input().split()))
    for j in range(s[1]):
        ls[i].append(s[j+2])
ls2 = [[0 for i in range(n)] for i in range(n)]
for i in range(len(ls)):
    for j in range(len(ls[i])):
        ls2[i][ls[i][j]-1] += 1
    print(*ls2[i])

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    edge = []
    for i in range(n):
        u,k,*v_li = map(int,input().split())
        u-= 1
        for j in range(len(v_li)):
            v_li[j] -= 1
        edge.append(v_li)
    ans_edge = [[0]*n for j in range(n)]
    
    for j in range(len(edge)):
        for k in range(len(edge[j])):
            ans_edge[j][edge[j][k]] = 1
    for j in range(len(ans_edge)):
        print(*ans_edge[j])

Submission status: Accepted

Solution code:
n = int(input())

lst = []
for i in range(n):
    lst.append(list(map(int,input().split())))

out_list = [[0 for i in range(n)] for l in range(n)]

for a,in_lst in enumerate(lst):
    for x in range(in_lst[1]):
        out_list[a][in_lst[x+2]-1] = 1


for o1 in out_list:
    print(' '.join([str(x) for x in o1]))

Submission status: Accepted

Solution code:
n = int(input())
G = [[0]*n for i in range(n)]
for i in range(n):
  u,k,*vs = map(int,input().split())
  for j in range(len(vs)):
    G[u-1][vs[j]-1] = 1 
  
for i in range(n):
  print(" ".join(map(str,G[i])))

Submission status: Accepted

Solution code:
n=int(input()) #รับจำนวน node ทั้งหมด
m=[[0]*n for i in range(n)] #สร้าง array matrix 0 ขนาดเท่าจำนวน node

for i in range(n):
    tmp=list(map(int,input().split())) #วน loop ตามจำนวน node รับค่าข้อมูลแต่ละ node
    #ตน 0 คือ node , ตน 1 คือ จำนวน vertex ตน 2 เป็นต้นไปคือ vertex
    for j in tmp[2:]:
        m[i][j-1]=1
    #แสดงผล node และ vertex ที่มีความสัมพันธ์กัน ใส่ 0 เมื่อไม่เกิดความสัมพันธ์ ใส่ 1 เมื่อเกิด
    #Show เป็น Matrix    
for i in range(n):
    print(" ".join(list(map(str,m[i]))))

Submission status: Accepted

Solution code:
n = int(input())

for i in range(n):
    u = list(map(int, input().split()))

    result = [0]*n
    count = u[1]

    for j in range(count):
        result[u[j+2]-1] = 1

    print(*result)

Submission status: Accepted

Solution code:
N=int(input())
matrix=[[0 for i in range(N)] for j in range(N)]
for i in range(N):
    a,b,*c=input().split()
    a=int(a)
    for k in c:

        matrix[a-1][int(k)-1]=1
for i in range(len(matrix)):
    print(*matrix[i])

Submission status: Accepted

Solution code:
N=int(input())

M=[[0]*N for n in range(N)]
for n in range(N):
    input_temp = list(map(int,input().split()))
    for m in range(input_temp[1]):
        ad = input_temp[m+2]
        M[n][ad-1] = 1 
        
for m in M:
    print(' '.join(map(str,m)))

Submission status: Accepted

Solution code:
n = int(input())
Adj = [[] for _ in range(n+1)]
C = [[0]*(n+1) for _ in range(n+1)]

for i in range(n):
    tmp = list(map(int,input().split()))
    for j in range(tmp[1]):
        C[tmp[0]][tmp[2+j]] = 1

for i in range(n):
    print(*C[i+1][1:])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    rows = []
    for i in range(n):
        rows.append([int(x) for x in input().split()])
    for row in rows:
        u,k = row[0],row[1]
        ul = [0]*n
        for j in range(k):
            ul[row[j+2]-1] = 1
        print(' '.join([str(u) for u in ul]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
G = [[0] * N for _ in range(N)]

for _ in range(N):
    u, n, *K = map(int, input().split())
    u -= 1
    for k in K:
        k -= 1
        G[u][k] = 1

for g in G:
    print(*g)

Submission status: Accepted

Solution code:
n = int(input())
graph = [[0]*n for _ in range(n)]

for _ in range(n):
    u, k, *V = map(int, input().split())
    if k == 0:
        continue
    for j in V:
        graph[u-1][j-1] = 1

for g in graph:
    print(' '.join(map(str, g)))

Submission status: Accepted

Solution code:
n = int(input())

graph = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    v = list(map(int,input().split()))
    for j in v[2:]:
        graph[v[0]-1][j-1] = 1

for i in graph:
    print(*i)

Submission status: Accepted

Solution code:
# 幅優先探索 Breadth first search: BFS

n = int(input())
A = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
    dat = [int(i) for i in input().split()]
    for v in dat[2:]:
        A[dat[0]-1][v-1] = 1

for i in range(n):
    for j in range(n-1):
        print(A[i][j], end=" ")
    print(A[i][n-1])

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.buffer.readline
N = int(input())
M = [[0]*N for _ in range(N)]
for _ in range(N):
    nums = list(map(int,input().split()))
    a = nums[1]
    if a == 0:
        continue
    else:
        for j in nums[2:]:
            M[nums[0]-1][j-1] = 1
for i in range(N):
    print(" ".join(map(str,M[i])))

Submission status: Accepted

Solution code:
n = int(input())
L = [[] for _ in range(n)]
for i in range(n):
    L[i] = [i for i in map(int,input().split())]
    L[i].remove(L[i][0])
    if L[i] != []:
        L[i].remove(L[i][0])
for i in range(n):
    adjacent = [0 for _ in range(n)]
    for el in L[i]:
        adjacent[el-1] = 1
    print(' '.join(map(str,adjacent)))

Submission status: Accepted

Solution code:
if __name__ == '__main__':

	n = int(input())
	
	A = [[0 for _ in range(n)] for _ in range(n)]

	for i in range(n):
		cmd = input().split();
		m = int(cmd[1])
		for j in range(m):
			A[i][int(cmd[2+j])-1] = 1

	for i in A:
		print(*i)

Submission status: Accepted

Solution code:
def main():
    n = int(input())

    adjacency_matrix = []
    for _ in range(n):
        u, k, *V = map(int, input().split())
        col = [0] * n
        for v in V:
            col[v-1] = 1
        adjacency_matrix.append(col)
    for col in adjacency_matrix:
        print(*col)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def Main():
    N = int(input())
    adj = [[0]*N for i in range(N)]

    for n in range(N):
        u, k, *V = map(int, input().split())
        
        if k > 0:
           for i in V:
            adj[u - 1][i - 1] = 1

    for i in range(N):
        print(*adj[i])

Main()

Submission status: Accepted

Solution code:
n = int(input())
M = [["0"]*n for _ in range(n)]
for _ in range(n):
    i = [int(i) for i in input().split()]
    u = i[0]
    k = i[1]
    V = i[2:]
    for v in V: M[u-1][v-1] = "1"
for i in M: print(*i)

Submission status: Accepted

Solution code:
# Graph
import sys
input = sys.stdin.readline
n = int(input())
A = [tuple(map(int, input().split())) for _ in range(n)]

G = [[0]*(n) for _ in [0]*(n)]

for i, k, *V in A:
    for v in V:
        G[i-1][v-1] = 1

for g in G:
    print(*g)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import sys

    N = int(input())
    adjacency_list = [[int(item) for item in input().split()] for _ in range(N)]

    adjacency_mat = [[0 for _ in range(N)] for _ in range(N)]

    for i, adjacency in enumerate(adjacency_list):
        if adjacency[1] != 0:
            for j in adjacency[2:]:
                adjacency_mat[i][j-1] = 1

    for item in adjacency_mat:
        print(*item)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def main():

    n = int(input())
    e = [[0]*n for _ in range(n)]

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1][m[j]-1] = 1

    for i in range(n):
        print (' '.join(map(str,e[i])))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(15000)
n = int(sys.stdin.readline())

ret = []
inp = sys.stdin.readlines()
G = [[0]*n for _ in range(n)]
for i in range(n):
  v = list(map(int,inp[i].split()))
  for j in v[2:]:
    G[v[0]-1][j-1] = 1
  print(" ".join(map(str,G[i])))

Submission status: Accepted

Solution code:
n=int(input())
a=[[0]*n for i in range(n)]
for _ in range(n):
    b=[int(i)-1 for i in input().split()]
    for i in b[2:]:
        a[b[0]][i]=1
for i in a:
    print(*i)

Submission status: Accepted

Solution code:
def parse(line):
    params = line.split(' ')
    node = params[0]
    n_node = params[1]
    nodes = [int(p) for p in params[2:]]
    return node, n_node, nodes


def main():
    n = int(input().strip())

    for _ in range(n):
        line = input().strip()
        _, _, nodes = parse(line)
        out = ''
        for i in range(n):
            if i + 1 in nodes:
                out += '1'
            else:
                out += '0'

            if i < n - 1:
                out += ' '
        print(out)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


V = int(input())

table = [['0']*V for i in range(V)]

for loop in range(V):
    tmp_array = list(map(int,input().split()))
    node_id = tmp_array[0]-1
    for i in range(tmp_array[1]):
        table[node_id][tmp_array[2+i]-1] = '1'

for row in range(V):
    print(' '.join(table[row]))

Submission status: Accepted

Solution code:
n = int(input())

A = [ [] for i in range(n)]

for i in range(n):
  A[i] = list(map(int, input().split()))

B = [[ 0 for i in range(n)] for j in range(n)]

for i in range(n):
  a = A[i][0]
  j = A[i][1]
  if j != 0:
    for k in range(j):
      b = A[i][k+2]
      B[a-1][b-1] = 1

for i in range(n):
  print(" ".join(map(str, B[i])))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v -1
            a[index_v] = 1
        print(*a)

Submission status: Accepted

Solution code:
import sys
# sys.stdin = open('input.txt')

n = int(input())
matrix = [[0]*n for i in range(n)]
for i in range(n):
    u, k, *v = map(int, input().split())
    for j in v:
        matrix[i][j-1] = 1
for line in matrix:
    line = list(map(str, line))
    print(' '.join(line))

Submission status: Accepted

Solution code:
g = int(input())
v = [[0] * g for _ in range(g)]
for i in range(g):
    inp = list(map(int, input().split()))
    for j in range(inp[1]):
        v[inp[0]-1][inp[2+j]-1] = 1
for row in range(g):
    for col in range(g):
        if col == g-1:
            print(v[row][col], end="")
        else:
            print(v[row][col], end=" ")
    print()

Submission status: Accepted

Solution code:
n = int(input())

mat = [[0] * (n) for _ in range(n)]

for _ in range(n):
    array = list(map(int, input().split()))
    u = array[0]
    k = array[1]
    for i in range(2, len(array)):
        mat[u - 1][array[i] - 1] = 1
        # mat[array[i] - 1][u - 1] = 1

for i in range(n):
    for j in range(n):
        if j == n - 1:
            print("%d" % (mat[i][j]))
        else:
            print("%d " % (mat[i][j]), end="")

Submission status: Accepted

Solution code:
n = int(input())
adj_matrix = [[0 for _ in range(n)] for _ in range(n)]
    
for i in range(n):
    l = list(map(int, input().split()))[2:]
    for j in l:
        adj_matrix[i][j-1] = 1

for l in adj_matrix:
    print(*l)

Submission status: Accepted

Solution code:
N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]

class Graph:
    def __init__(self,Vertex,Edge):
        self.V = Vertex
        self.E = Edge

Edge = [i+1 for i in range(N)]
Vertex = []
for i in range(N):
    for j in range(2,A[i][1]+2):
        Vertex.append((A[i][0],A[i][j]))

g = Graph(Vertex,Edge)

R = [[0 for j in range(N)] for i in range(N)]
for i in range(N):
    for j in range(N):
        if (i+1,j+1) in g.V:
            R[i][j] = 1

for i in range(N):
    for j in range(N):
        if j >= 1:
            print(" ",end="")
        print(R[i][j],end="")
    print()

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

n = int(input())
M = [['0']*n for _ in range(n)] # 0オリジンの隣接行列、0で初期化しておく

for _ in range(n):
    u, k, *vv = input().split()
    u = int(u)-1  # 0オリジンにする
    k = int(k)    # kは使わない
    for v in vv:
        v = int(v)-1  # 0オリジンにする
        M[u][v] = '1' #最後に出力するときstr型である必要があるので、str型で入力

for m in M:
    print(' '.join(m)) # 空白入れて出力

Submission status: Accepted

Solution code:
n = int(input())
adj_list = [ [0]*n for _ in range(n) ]

for _ in range(n):
    lst = list(map(int, input().split()))
    
    x = lst[0] - 1
    
    if lst[1] == 0:
        continue
    
    for y in lst[2:]:
        adj_list[x][y-1] = 1
    
for r in adj_list:
    print(" ".join([ str(v) for v in r]))

Submission status: Accepted

Solution code:
n=int(input())


#隣接リストtableを作る
table = [[0 for i in range(n)] for j in range(n)]


for _ in range(n):
    
    input_ls=list(map(int,input().split()))
    
    #頂点と辺の整理
    vertex=input_ls[0]
    edges_count=input_ls[1]
    edges=input_ls[2:]
    
    
    for k in edges:
        table[vertex-1][k-1]=1
        
        
        
for i in range(n):
    for j in range(n):
        if j!=n-1:
            print(table[i][j],end=' ')
        else:
            print(table[i][j])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n = int(input())

adj = [[0 for _ in range(n)] for _ in range(n)]

for _ in range(n):
    ls = list(map(int, input().split(" ")))
    f = ls[0]
    for i in range(ls[1]):
        adj[f - 1][ls[2 + i] - 1] = 1

for i in range(n):
    for j in range(n):
        print(adj[i][j], end="")
        if j < n - 1:
            print(" ", end="")
    print()

Submission status: Accepted

Solution code:
def convert_adjacency_list_to_matrices():
    n = int(input())
    for _ in range(n):
        _, _, *v = map(int, input().split())
        s = set(v)
        ans = list(map(str, [int(i in s) for i in range(1, n+1)]))
        print(" ".join(ans))

if __name__ == "__main__":
    convert_adjacency_list_to_matrices()

Submission status: Accepted

Solution code:
# Graph

N = int(input())
G = [[0]*N for i in range(N)]

for i in range(N):
    lst = list(map(int, input().split()))
    idx = lst[0]
    for j in range(lst[1]):
        G[idx-1][lst[2:][j]-1] = 1
    print(*G[idx-1])

Submission status: Accepted

Solution code:
dat = []
n = int(input())
for i in range(n):
    dat.append([0] * n)
for i in range(n):
    s = list(map(int, input().split()))
    for j in range(len(s) - 2):

        dat[i][s[j + 2] - 1] = 1
for i in range(n):
    l = list(map(str, dat[i]))
    print(" ".join(l))

Submission status: Accepted

Solution code:
"""
隣接リストで与えられる有向グラフを，隣接行列で構成しなおす
"""
n = int(input())

for i in range(n):
    mat = [0 for _ in range(n)]
    l = list(map(int, input().split()))
    deg = l[1]
    for r in range(deg):
        index = l[r+2]-1
        mat[index] = 1
    print(' '.join(map(str, mat)))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    for row in M:
        print(' '.join([str(x) for x in row]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
g = [[0 for _ in range(n)] for _ in range(n)]

for _ in range(n):
	a = list(map(int,input().split()))
	u = a.pop(0)
	a.pop(0)
	for v in a:
		g[u-1][v-1] = 1

for a in g:
	print(" ".join(map(str,a)))

Submission status: Accepted

Solution code:
n=int(input())
A=[list(map(int,input().split())) for i in range(n)]

M=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in A[i][2:]:
        M[i][j-1]=1

for i in range(n):
    print(*M[i])

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    v = list(map(int, input().split()))[2:]
    line = [0] * n
    for i in v:
        line[i - 1] = 1
    print(*line)

Submission status: Accepted

Solution code:
n = int(input())
main = [[0] * (n+1) for i in range(n+1)]

for i in range(n):
	a = input().split()
	for j in range(int(a[1])):
		main[int(a[0])][int(a[j+2])] = 1

#print(main)
for i in range(1,n+1):
	print(*main[i][1:])

del a

Submission status: Accepted

Solution code:
l = int(input())
rlist = []
for i in range(l):
    li = list(map(int, input().split()))
    rlist.append(li[2:])

result = []
for i in range(l):
    rcell = [0] * l
    for rl in rlist[i]:
        rcell[rl-1] = 1

    result.append(rcell)

for r in result:
    re = ''
    for rr in r:
        re += str(rr) + ' '

    print(re[:-1])

Submission status: Accepted

Solution code:
N = int(input())

class Matrix:
    def __init__(self, N):
        self.M = [[0 for _ in range(N)] for _ in range(N)]
    
    def set(self, i, j, val):
        self.M[i-1][j-1] = val
    
    def __repr__(self):
        str_lines = []
        for line in self.M:
            str_lines.append(' '.join(map(str, line)))
        return '\n'.join(str_lines)

matrix = Matrix(N)

for _ in range(N):
    tmp = input().split()
    node_id = int(tmp[0])
    neighbor_num = int(tmp[1])
    neighbors = list(map(int, tmp[2:]))
    
    for n in neighbors:
        matrix.set(node_id, n, 1)

print(matrix)

Submission status: Accepted

Solution code:
n = int(input())
M =[[0]*n for _ in range(n)]

for i in range(n):
    tmp = list(map(int, input().split()))
    if tmp[1] != 0:
        for adj in tmp[2:]:
            M[i][adj-1] = 1
    print(*M[i])

Submission status: Accepted

Solution code:
n = int(input())
adj = []
# adj = [[None] * n for _ in range(n)]
ans = [[0] * n for _ in range(n)]

for i in range(n):
    temp = list(map(int, input().split()))
    if len(temp) > 2:
        adj.append(temp[2:])
    else:
        adj.append([])

# print(adj)
# print(ans)

for i in range(n):
    for j in range(len(adj[i])):
        ans[i][adj[i][j]-1] = 1

# print(ans)

for i in range(n):
    print(*ans[i])

Submission status: Accepted

Solution code:
def conv_list(n):
    adj_matrix = []
    for _ in range(n):
        adj_list = input().split()
        adj_list = list(map(int, adj_list))
        adj_row = [0] * n
        if adj_list[1] > 0:
            for i in adj_list[2:]:
                adj_row[i - 1] = 1
        adj_matrix.append(adj_row)
    return adj_matrix


def main():
    n = int(input())
    adj_matrix = conv_list(n)
    for row in adj_matrix:
        print(" ".join(map(str, row)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
ans=[]
for i in range(n):
  u,k,*v=list(map(int,input().split()))
  s = [0 for i in range(n)]
  for j in v:
    s[j-1]=1
  ans.append(s) 

for i in ans:
  I=map(str,i)
  print(" ".join(I))

Submission status: Accepted

Solution code:
n=int(input())
c=[[0 for _ in range(n)]for _ in range(n)]
for i in range(n):
	a=list(map(int,input().split()))
	for j in range(2,len(a)):
		c[i][a[j]-1]=1
for i in range(n):
	print(*c[i])

Submission status: Accepted

Solution code:

n = int(input())

m = [[0 for i in range(n + 1)] for j in range(n + 1)]

for i in range(n):
    nums=list(map(int,input().split()))
    for j in range(nums[1]):
        m[i + 1][nums[j + 2]] = 1

for i in range(n):
    for j in range(n-1):
        print(m[i + 1][j + 1], end = " ")
    print(m[i + 1][n])

Submission status: Accepted

Solution code:
n = int(input())
v_matrix = [[0 for j in range(n+1)] for i in range(n+1)]
for i in range(n):
    u, k, *v = map(int, input().split())
    for vi in v:
        v_matrix[u][vi] = 1

for row in v_matrix[1:]:
    print(" ".join(map(str, row[1:])))