結果

問題 No.1078 I love Matrix Construction
ユーザー gr1msl3ygr1msl3y
提出日時 2022-01-28 00:10:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,381 ms / 2,000 ms
コード長 2,025 bytes
コンパイル時間 1,452 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 266,164 KB
最終ジャッジ日時 2023-08-27 02:53:10
合計ジャッジ時間 17,080 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,796 KB
testcase_01 AC 94 ms
71,868 KB
testcase_02 AC 323 ms
102,408 KB
testcase_03 AC 603 ms
149,096 KB
testcase_04 AC 778 ms
177,984 KB
testcase_05 AC 683 ms
155,088 KB
testcase_06 AC 310 ms
103,120 KB
testcase_07 AC 223 ms
86,552 KB
testcase_08 AC 653 ms
154,508 KB
testcase_09 AC 206 ms
82,380 KB
testcase_10 AC 1,381 ms
266,164 KB
testcase_11 AC 842 ms
180,136 KB
testcase_12 AC 1,140 ms
243,792 KB
testcase_13 AC 1,277 ms
264,196 KB
testcase_14 AC 967 ms
206,088 KB
testcase_15 AC 1,206 ms
245,560 KB
testcase_16 AC 225 ms
84,608 KB
testcase_17 AC 95 ms
71,852 KB
testcase_18 AC 284 ms
96,068 KB
testcase_19 AC 440 ms
119,464 KB
testcase_20 AC 432 ms
120,124 KB
testcase_21 AC 160 ms
78,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict


def scc(N, G, RG):
    order = []
    seen = [0]*N
    group = [None]*N

    def dfs(s):
        task = deque([(s, 1), (s, 0)])
        while task:
            v, t = task.pop()
            if t:
                if seen[v] == 2:
                    continue
                seen[v] = 2
                order.append(v)
            else:
                if seen[v]:
                    continue
                seen[v] = 1
                for e in G[v]:
                    if not seen[e]:
                        task.append((e, 1))
                        task.append((e, 0))

    def rdfs(s, col):
        task = deque([s])
        while task:
            v = task.pop()
            if seen[v]:
                continue
            seen[v] = 1
            group[v] = col
            for e in RG[v]:
                if not seen[e]:
                    task.append(e)
    for i in range(N):
        if not seen[i]:
            dfs(i)
    seen = [0]*N
    label = 0
    for s in reversed(order):
        if not seen[s]:
            rdfs(s, label)
            label += 1
    return label, group


N = int(input())
S = list(map(lambda x: int(x)-1, input().split()))
T = list(map(lambda x: int(x)-1, input().split()))
U = list(map(int, input().split()))

graph = defaultdict(list)
rgraph = defaultdict(list)
for i in range(N):
    for j in range(N):
        s, t, u = S[i], T[i], U[i]
        x = s*N+j
        y = j*N+t
        nx = x+N**2*(u % 2)
        ny = y+N**2*(u//2)
        graph[nx].append((ny+N**2) % (2*N**2))
        rgraph[(ny+N**2) % (2*N**2)].append(nx)
        graph[ny].append((nx+N**2) % (2*N**2))
        rgraph[(nx+N**2) % (2*N**2)].append(ny)

M, sccgraph = scc(2*N**2, graph, rgraph)
ans = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        n = i*N+j
        if sccgraph[n] == sccgraph[n+N**2]:
            print(-1)
            exit()
        elif sccgraph[n] < sccgraph[n+N**2]:
            ans[i][j] = 1

for a in ans:
    print(*a)
0