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())
g = [[0] * n for _ in range(n)]

while n:
    l = list(map(int, input().split()))
    column_g = g[l[0] - 1]
    for i in l[2:]:
        column_g[i-1] = 1
    n -= 1
for v in g:
    print(*v)

Submission status: Accepted

Solution code:

N = int(input())

# initialize
ans = [[0 for i in range(N)] for j in range(N)]

for i in range(N):
    a = list(map(int, input().split()))
    id = a[0]
    jisu = a[1]
    for j in range(jisu):
        c = a[j+2]
        ans[i][c-1] = 1

for i in range(N):
    for j in range(N):
        if j != N-1:
            print(ans[i][j], end=" ")
        else:
            print(ans[i][j])

Submission status: Accepted

Solution code:

"""
Main??????????????????
"""
# Node??°?????????
N = int(input())

# Node??????????????????????????????
nodes = [[0 for i in range(N)] for j in range(N)]

# Node??????????¨????
for i in range(N):
    j = list(map(int, input().split())) # ??\??????????§£
    no = j[0] - 1                       # Node???????????????
    childNos = j[2:]                    # ??????????´??????????

    for j in range(len(childNos)):
        childNo = childNos[j] - 1
        nodes[no][childNo] = 1


# ??¨?????????
for i in range(N):
    for j in range(N):
        if j != N -1:
            print(nodes[i][j], end=" ")
        else:
            print(nodes[i][j])

Submission status: Accepted

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

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

for i in G:
  print(*i)

Submission status: Accepted

Solution code:
n = int(input())
Adj = [[0 for i in range(n)] for i in range(n)]

for i in range(n):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1

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

Submission status: Accepted

Solution code:
n=int(input())
V=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
	v=[int(j) for j in input().split(" ")]
	u=v.pop(0)
	k=v.pop(0)
	for j in range(k):
		V[u-1][v[j]-1]=1



for i in range(n):
	print(" ".join([str(j) for j in V[i]]))

Submission status: Accepted

Solution code:
def convertList(G):
    n = len(G)
    GL = [ [0 for _ in range(n)] for _ in range(n)]
    for key, values in G.items():
        GL[key - 1][key - 1] = 0
        for v in values:
            GL[key - 1][v - 1] = 1

    return GL


if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
    GL = convertList(G)
    for i in range(n):
        print(*GL[i])

Submission status: Accepted

Solution code:
n = int(input())
adj = [[0 for i in range(n)] for j in range(n)]

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

Submission status: Accepted

Solution code:
def main():

    n = int(input())
    adj = [[0 for i in range(n)] for j in range(n)]

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

    for i in adj:
        print(*i)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())
v = [[0] * (n + 1) for i in range(n + 1)]
for u in range(1, n + 1):
    U = [int(j) for j in stdin.readline().split()]
    for k in U[2:]:
        v[u][k] += 1
for i in range(1, n + 1):
    print(*v[i][1:])

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())
v = [[0] * n for _ in range(n)]
for u in range(n):
    l = list(map(int, stdin.readline().split()))
    for k in l[2:]:
        v[u][k - 1] = 1
    print(*v[u])

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())
v = [[0] * n for _ in range(n)]
for u in range(n):
    l = [int(j) for j in stdin.readline().split()]
    for k in l[2:]:
        v[u][k - 1] = 1
    print(*v[u])

Submission status: Accepted

Solution code:
import sys


if __name__ == "__main__":
    n = int(sys.stdin.readline())
    ANS = [[0] * n for i in range(n)]
    for inp in sys.stdin.readlines():
        inp = list(map(int, inp.split()))
        for i in inp[2:]:
            ANS[inp[0]-1][i-1] = 1
    for row in ANS:
        print(" ".join(map(str, row)))

Submission status: Accepted

Solution code:
# coding=utf-8

n = int(input())
M = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    u, k, *v = map(int, input().split())
    u = u-1 #for index
    if k == 0:
        continue
    for num in v:
        num = num-1 #for index
        M[u][num] = 1
for row in M:
    print(*row)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    tmp = map(int, raw_input().split())
    if tmp[1]:
        for j in range(1, tmp[1]+1):
            a[i][tmp[j+1]-1] = 1
for i in range(n):
    print ' '.join(map(str, a[i]))

Submission status: Accepted

Solution code:
N = int(input())
dp = [[0 for i in range(N)] for j in range(N)] 

for _ in range(N):
    inputs = list(map(int, input().split()))
    u = inputs[0]
    k = inputs[1]
    v = inputs[2:]
    #print(u, k, v)
    for each in v:
        dp[u-1][each-1] = 1

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

Submission status: Accepted

Solution code:
def adjmatrices():
    n = int(input())
    for i in range(n):
        node_list = list(map(int, input().split()))
        matrix = [0]*n
        for j in node_list[2:]:
            matrix[j-1] = 1
        print(*matrix)

if __name__ == '__main__':
    adjmatrices()

Submission status: Accepted

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

N = int(input())
adj = [["0"]*N for _ in range(N)]

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

for a in adj:
    print(" ".join(a))

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input().rstrip())

for i in range(n):
    line = input().rstrip().split(" ")
    lineMax = int(line[1])
    lineIndex = 0
    for j in range(n):
        if lineIndex >= lineMax:
            print(0, end = "")
        else:
            if j + 1 == int(line[lineIndex + 2]):
                print(1, end = "")
                lineIndex += 1
            else:
                print(0, end = "")
        if j + 1 != n:
            print(" ", end ="")
    print()

Submission status: Accepted

Solution code:
n = int(input())
M = [[0] * n for i in range(n)]
for i in range(n):
    t = list(map(int, input().split()))
    v = t[0]
    k = t[1]
    for j in range(k):
        M[v - 1][t[2+j] - 1] = 1

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

Submission status: Accepted

Solution code:
# Aizu Problem ALDS_1_10_B: Graph
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


n = int(input())
G = [[0 for _ in range(n)] for __ in range(n)]
for row in range(n):
    inp = [int(_) for _ in input().split()]
    a = inp[0]
    if inp[1] > 0:
        for b in inp[2:]:
            G[a-1][b-1] = 1

for row in G:
    print(' '.join([str(_) for _ in row]))

Submission status: Accepted

Solution code:
# your code goes here
#graph
#????????????????¨?????????¨
n=int(input())
L=[]
for i in range(n):
    L.append([])
    for j in range(n):
        L[i].append("0")
#rint(L)
for i in range(n):
    v=[int(i) for i in input().split()]
#    print(v)
    for j in range(2,2+v[1]):
        L[v[0]-1][v[j]-1]="1"#??????????????????1????????????i???i-1
#        print(L)
     #   L[v[j]-1][v[0]-1]="1"
for i in range(n):
    print(" ".join(L[i]))

Submission status: Accepted

Solution code:
def main():
    num = int(input())
    T = [[0] * num for i in range(num)]
    for n in range(num):
        a = list(map(int,input().split()))
        u = a[0] - 1
        if a[1] > 0:
            for i in a[2:]:
                T[u][i-1] = 1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = input()
    Adj = [[0]*n for m in xrange(n)]
    for i in xrange(n):
        a = map(int,raw_input().split())
        for j in xrange(a[1]):
            Adj[i][a[2:][j]-1] = 1

    for i in xrange(n):
        for j in xrange(n):
            if j == n-1:
                print Adj[i][j]
            else:
                print Adj[i][j],

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = input()

    m = [[0] * N for i in range(N)]

    for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i][j-1] = 1

    for row in m:
        for i in row:
            print i,
        print

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    u, k, *v = map(int, input().split())
    row = ""
    for j in range(1, n + 1):
        if j in v:
            row += "1"
        else:
            row += "0"
        if j != n:
            row += " "
    print(row)

Submission status: Accepted

Solution code:
n = int(raw_input())
G = [[0 for i in xrange(n)] for j in xrange(n)]

for i in xrange(n):
    data = map(int, raw_input().split())
    u = data[0]-1
    k = data[1]
    for j in xrange(k):
        v = data[j+2]-1
        G[u][v] = 1

for i in xrange(n):
    print " ".join([str(x) for x in G[i]])

Submission status: Accepted

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

# N*N?????£??\??????????????°?????????
graph_table = [[0]*n for i in range(n)]

# ??£??\??????????????£??\???????????????
for d in data:
    node_id = d[0]
    num_of_e = d[1]
    for e in d[2:]:#?????????????????????????????£?????????
        graph_table[node_id-1][e-1] = 1

# ??????
for g in graph_table:
    print(*g)

Submission status: Accepted

Solution code:
n = int(input())
A = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    tmp_u = list(map(int, input().split()))
    vs = tmp_u[2:]
    for v in vs: 
        A[tmp_u[0]-1][v-1] = 1
       
for i in range(n):
    tmp_A = A[i]
    tmp_A = map(str, tmp_A)
    tmp_A = ' '.join(tmp_A)
    print(tmp_A)

Submission status: Accepted

Solution code:
v_num = int(input())
adjacent = [[0 for n in range(v_num)] for m in range(v_num)]
for _ in range(v_num):
    inp = [int(n) for n in input().split(" ")]
    for i in inp[2:]:
        adjacent[inp[0] - 1][i - 1] = 1
for i in range(v_num):
    print(*adjacent[i])

Submission status: Accepted

Solution code:
n = int(input())
a = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    inp = list(map(int, input().split()))
    u = inp[0]
    k = inp[1]
    for j in range(k):
        a[u - 1][inp[2 + j] - 1] = 1
    print(*a[u - 1])

Submission status: Accepted

Solution code:

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

a=[[0 for i2 in range(n)]for i1 in range(n)]

for i in range(n):
    for j in range(adj[i][1]):
        a[i][adj[i][j+2]-1]=1

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

Submission status: Accepted

Solution code:
n = int(input())
a = [[0 for i in range(n)] for j in range(n)]

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

Submission status: Accepted

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

for i in M:
    print(*i)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: UTF-8
n = int(input())
Ans =[ [0 for i in range(n)] for j in range(n)]
for l in range(n):
  raw = list(map(int,input().split()))
  i = raw[0]
  d = raw[1]
  for m in range(2,d+2):
    Ans[i-1][raw[m]-1] = 1

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

Submission status: Accepted

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


if __name__ == '__main__':

    n = int(input())
    M = [[0 for i in range(n)] for j in range(n)]
    for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u-1][v-1] = 1
        
    for r in M:
        print(" ".join(map(str, r)))

Submission status: Accepted

Solution code:
n = int(input())
MAP = [[0] * n for _ in range(n)]
for i in range(n):
    u, k, *v = map(int, input().split())
    for j in v:
        MAP[u - 1][j - 1] = 1
for m in MAP:
    print(*m)

Submission status: Accepted

Solution code:
n = int(input())
Adj = [0 for i in range(n)]

for i in range(n):
    row = list(map(int, input().split(' ')))
    Adj[row[0] - 1] = row

for adj in Adj:
    k = adj[1]
    E = adj[2:2+k]
    row = [0 for i in range(n)]
    for i in E:
        row[i - 1] = 1
    print(' '.join(map(str, row)))

Submission status: Accepted

Solution code:
n = int(input())
Adj = [0 for i in range(n)]

for i in range(n):
    row = list(map(int, input().split(' ')))
    Adj[row[0] - 1] = row

for adj in Adj:
    row = [0 for i in range(n)]
    for i in adj[2:2+adj[1]]:
        row[i - 1] = 1
    print(' '.join(map(str, row)))

Submission status: Accepted

Solution code:
import sys
data_list = []
for line in sys.stdin:
	data_list.append(line.split())

class Node():
    def __init__(self, name, transaction_list = None):
        self.transaction_list = transaction_list or []

    def add_node(self, node):
        self.transaction_list.append(node)

def convert_adjencentList_to_adjencentMatrix(adjencentList):
    adjencent_matrix = [[0 for i in range(len(adjencentList))] for j in range(len(adjencentList))]
    count = 0
    for targetList in adjencentList:
        targetList.transaction_list.sort()
        for target_index in targetList.transaction_list :
            adjencent_matrix[count][int(target_index) - 1] = 1
        count = count + 1

    for i in adjencent_matrix:
        print(*i)

if __name__ == "__main__":
        node_data_list = []

        for node_data in data_list[1:]:
            node = Node(node_data[1])
            for transaction_node in node_data[2:]:
                node.add_node(transaction_node)

            node_data_list.append(node)

        convert_adjencentList_to_adjencentMatrix(node_data_list)

Submission status: Accepted

Solution code:
N=int(input())
M=[[0]*N for _ in[0]*N]
for e in[list(map(int,input().split()))for _ in[0]*N]:
 for v in e[2:]:M[e[0]-1][v-1]=1
for r in M:print(*r)

Submission status: Accepted

Solution code:
N=int(input())
for _ in[0]*N:
 r=[0]*N
 for i in input().split()[2:]:r[int(i)-1]=1
 print(*r)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
A = [[0 for j in range(n)] for i in range(n)]
for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1

for li in A:
    print(' '.join(list(map(str, li))))

Submission status: Accepted

Solution code:
class AdjacentGraph:
    """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """
    def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

    def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

    def __iter__(self):
        self._id = 0
        return self

    def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()


def run():
    n = int(input())
    g = AdjacentGraph(n)

    for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

    for id_, nodes in g:
        print(" ".join([str(x) for x in nodes]))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
n = int(input())

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

for _ in range(n):
    tmp = [int(x) for x in input().split()]
    u = tmp[0]
    k = tmp[1]
    l = tmp[2:]
    for x in l:
        m[u-1][x-1] = 1

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
input_lines = [[int(i) for i in lines.split()] for lines in sys.stdin.read().splitlines()]
for i in range(n):
    out_list = ['0'] * n
    m = len(input_lines[i])
    for j in range(m-1, 1, -1):
        out_list[input_lines[i][j]-1] = '1'
    print(' '.join(out_list))

Submission status: Accepted

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

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

    for i in range(1, n+1):
        print(" ".join([str(m[i][j]) for j in range(1, len(m[i]))]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
adm = []
for i in range(n):
    x = list(map(int,input().split()))
    tmp = [0]*n
    if x[1] == 0:
        adm.append(tmp)
    else:
        a = x[2:]
        for j in a:
            tmp[j-1] = 1
        adm.append(tmp)
for i in range(len(adm)):
    print(*adm[i])

Submission status: Accepted

Solution code:
#16D8103010K Ko Okasaki
#ALDS11a

Al=[]
n=int(input())
for i in range(n):
    a=list(map(int,input().split()))
    Al.append(a[2:])
Am=[[0 for j in range(n)]for i in range(n)]

for i in range(n):
    for k in Al[i]:
            Am[i][k-1]=1
for i in range(n):
    print(*Am[i])

Submission status: Accepted

Solution code:
class Node:
    def __init__(self, num, deg, neib):
        self.num = num
        self.deg = deg
        self.neib = neib


dnum = int(input())
N = []
for i in range(dnum):
    data = input().split()
    
    u = int(data[0])
    k = int(data[1])
    if k > 0:
        v = list(map(int, data[2:]))
        node = Node(u,k,v)
    else:
        node = Node(u,k,0)

    N.append(node)

O = []
for e in N:
    out = [0]*dnum
    if e.neib != 0:
        for j in e.neib:
            out[j-1] = 1
    else: pass

    O.append(out)

for i in O:
    print(*i)

Submission status: Accepted

Solution code:
n = int(input())
G = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    S = list(map(int, input().split()))
    tmp_n = S[1]
    for j in range(tmp_n):
        G[i][S[j+2]-1] = 1
for i in range(n):
    print(*G[i])

Submission status: Accepted

Solution code:
n=int(input())
G=[[0 for i in range(n)]for j in range(n)]
u=[0 for i in range(n)]
for i in range(n):
    A=input().split()
    u[i]=int(A[0])
    k=int(A[1])
    for j in range(k):
        x=int(A[j+2])-1
        G[i][x]=1
for i in range(n):
    for j in range(n):
        if j<n-1:
            print(G[i][j],end=" ")
        else:
            print(G[i][j])

Submission status: Accepted

Solution code:
n = int(input())
B = [[0 for i in range(n)] for j in range(n)]

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

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

Submission status: Accepted

Solution code:
n=int(input())
g=[[0 for i in range(n)]for j in range(n)] 
for i in range(n):
    s=input()
    l=s.split()
    for i in range(len(l)):
        l[i]=int(l[i])
    for i in l[2:]:
        g[l[0]-1][i-1]+=1
for i in range(n):
    for j in range(n):
        print(g[i][j],end="")
        if j!=len(g[i])-1:
            print(" ",end="")
    print()

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    v=[]
    array=[]
    for i in range(n):
        zero=[0 for i in range(n)]
        a=list(map(int,input().split()))
        a=a[2:]
        for j in a:
            zero[j-1]=1
        array.append(zero)
    for i in array:
        print(*i)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n):

	B = input().split()

	if B[1]!=0:
		for j in range(int(B[1])):
			#B[2+j]
			A[i][int(B[2+j])-1] = 1

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

Submission status: Accepted

Solution code:
n=int(input())

l=[[0 for i in range(n)] for j in range(n)]##静的確保

for i in range(n):
    p=[int(x) for x in input().split()]
    
    for j in range(n):
        if j+1 in p[2:]:
            l[i][j]=1

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

Submission status: Accepted

Solution code:
n=int(input());
G=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    list=[int(m) for m in input().split()]
    u=list[0]
    k=list[1]
    for j in range(2,len(list)):
        v=list[j]
        G[u-1][v-1]=1
for i in range(n):
    for j in range(n):
        if j!=n-1:
            print(G[i][j],end=" ")
        else:
            print(G[i][j],end="\n")

Submission status: Accepted

Solution code:
n=int(input())
a=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    v=list(input().split())
    for j in range(len(v)):
        v[j]=int(v[j])
    x=2
    for k in range(v[1]):
        a[i][v[x+k]-1]=1
for i in range(n):
    for j in range(n):
        if j!=n-1:
            print(a[i][j],end=" ")
        else:
            print(a[i][j])

Submission status: Accepted

Solution code:
#16D8101014F KurumeRyunosuke 2018/6/20
#import numpy as np
n=int(input())
#ls=np.zeros((n,n))
ls=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
	tmp=input().split()
	for j in range(int(tmp[1])):
		#print("*")
		ls[i][int(tmp[j+2])-1]=1


for i in range(n):
    for j in range(n):
        if(j!=n-1):
            print(int(ls[i][j]), end=' ')
        else:
            print(int(ls[i][j]))

Submission status: Accepted

Solution code:
N= int(input())

b=[[0 for i in range(N)]for j in range(N)]


for i in range(N):
    a= list(map(int, input().split()))
    for j in range(a[1]):
        b[i][a[2+j]-1]=1
    
for i in range(N):
    print(*b[i])

Submission status: Accepted

Solution code:

n=int(input())

arr = [[0 for i in range(n)] for j in range(n)]
#for i in arr:
    #print(' '.join(map(str, i)))

#print(arr)

for i in range(0,n):
    l=raw_input().split()
    if(int(l[1])>=1):
        x=int(l[0])
        y=int(l[1])
        p=len(l)
        d=0
        while(2+d<p):
            z=int(l[2+d])
            #print(x,y,z)
            arr[x-1][z-1]=1
            #print(arr)
            d=d+1

#print(arr)
for i in arr:
    print(' '.join(map(str, i)))

Submission status: Accepted

Solution code:
n=int(input())
G=[[0 for j in range(n)] for j in range(n)]

for i in range(n):
	l=list(map(int,input().split()))
	u=l[0]
	k=l[1]
	for j in range(2,k+2):
		v=l[j]
		G[u-1][v-1]=1
for i in range(n):
	print(*G[i])

Submission status: Accepted

Solution code:
def print_list(a):
    for x in a[:-1]:
        print(x, end=" ")
    print(a[-1])


def print_mat(a):
    for i in range(len(a)):
        print_list(a[i])


n = int(input())
adj = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    s = [int(x) for x in input().split()]
    u = s[0]
    for v in s[2:]:
        adj[u - 1][v - 1] = 1


print_mat(adj)

Submission status: Accepted

Solution code:

n=int(input())
list1=[[0 for i in range(n)] for j in range(n)]
#print(list1)

for i in range(n):
	a=list(map(int,input().split()))
	#print(a)
	if a[1]>0:
		for j in range(2,len(a)):
			list1[i][a[j]-1]=1
			
for i in range(n):
	print(*list1[i])

Submission status: Accepted

Solution code:
n=int(input())

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

for i in range(n):
    p=[int(x) for x in input().split()]
    for j in range(n):
        if j+1 in p[2:]:
            a[i][j]=1
"""for i in range(n):
    for j in range(n):
        print(a[i][j],"",end="")
    print("")
"""
for i in range(n):
    print(*a[i])

Submission status: Accepted

Solution code:
n=int(input())
v=[]
u=0
e=0

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

for i in range(n):
    S=input().split()
    if S[1]!=0:
        for j in range(int(S[1])):
            A[i][int(S[2+j])-1]=1

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

Submission status: Accepted

Solution code:
n=int(input())
mat=[[0] * n for _ in range(n)]
for _ in range(n):
    adj=list(map(int, input().split()))
    i=adj[0]
    for j in adj[2:]:
        mat[i - 1][j - 1]=1
for m in mat:
    print(' '.join(map(str, m)))

Submission status: Accepted

Solution code:
N=int(input())
ad_ma=[[0 for i in range(N)] for j in range(N)]
for i in range(N):
    nums=list(map(lambda x:int(x)-1,input().split()))
    f=nums[0]
    nums=nums[2:]
    for num in nums:
        ad_ma[f][num]=1
 
  
for i in range(N):
    for j in range(N):
        if(j!=N-1):
            print(ad_ma[i][j],end=' ')
        else:
            print(ad_ma[i][j])

Submission status: Accepted

Solution code:
#coding:utf-8

n = int(input())
mtrx = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    data = list(input().split())
    adjs = int(data[1])
    for adj in range(adjs):
        j = int(data[adj+2]) - 1
        mtrx[i][j] = 1
for col in mtrx:
    print(" ".join([str(num) for num in col]))

Submission status: Accepted

Solution code:
import sys

n = int(raw_input())
x = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    seq = map(int,raw_input().split())
    tmp = seq[0]-1
    n2 = seq[1]
    if n2!=0:
        for e in seq[2:]:
            x[tmp][e-1]=1
        
for i in range(n):
    print " ".join(map(str,x[i]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def main():
    num = int(stdin.readline())

    for _ in xrange(num):
        L = [int(s) for s in stdin.readline().split()][2:]
        print(*(int(i in L) for i in xrange(1, num+1)))


main()

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
n=int(raw_input())
l=[[0 for jc in range(n)] for _ in range(n)]
for a in range(n):
    jk=map(int, raw_input().split())
    for x in range(jk[1]):
        l[jk[0]-1][jk[x+2]-1]=1
 
for i in l:
    print ' '.join(map(str, i))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
G = {i: [] for i in range(1, n+1)}
for _ in range(n):
    tmp = list(map(int, input().split()))
    G[tmp[0]] += tmp[2:] if tmp[1]>0 else []

adj = [[0]*(1+n) for _ in range(1+n)]
for i in range(1, n+1):
    for j in G[i]:
        adj[i][j] = 1
for i in range(1, n+1):
    print(*adj[i][1:])

Submission status: Accepted

Solution code:
def main(N,A):
    A_ans = [[0]*N for _ in range(N)]

    for i in range(N):
        for j in range(A[i][1]):
            tmp = A[i][j+2]
            #print(tmp)
            A_ans[i][tmp-1] = 1

    return A_ans

N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]
A_ans = main(N,A)
for i in range(N):
    A_ans[i] = map(str,A_ans[i])
    print(' '.join(A_ans[i]))

Submission status: Accepted

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

for i in range(n):
    data = [int(i) for i in input().split()]
    for j in range(data[1]):
        node = data[2 + j] - 1
        graph[i][node] = 1

for line in graph:
    print(" ".join(str(i) for i in line))

Submission status: Accepted

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

matrix = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
    if len(a[i])>2:
        b = a[i][2:]   
        for j in b:
            matrix[i][j-1] = 1
    print(*matrix[i])

Submission status: Accepted

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

def getNeighbor(A, i):
    a = len(A)
    for k in range(2, a):
        C[i][A[k]-1] = 1

N = int(input())
C = [[0]*N for _ in range(N)]
for i in range(N):
    A = list(map(int, input().split()))
    getNeighbor(A,i)
for i in range(N):
    print(*C[i])