結果
問題 | No.1078 I love Matrix Construction |
ユーザー |
![]() |
提出日時 | 2022-01-28 00:10:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,729 ms / 2,000 ms |
コード長 | 2,025 bytes |
コンパイル時間 | 399 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 268,308 KB |
最終ジャッジ日時 | 2024-12-26 09:10:08 |
合計ジャッジ時間 | 17,821 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
from collections import deque, defaultdictdef scc(N, G, RG):order = []seen = [0]*Ngroup = [None]*Ndef dfs(s):task = deque([(s, 1), (s, 0)])while task:v, t = task.pop()if t:if seen[v] == 2:continueseen[v] = 2order.append(v)else:if seen[v]:continueseen[v] = 1for 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]:continueseen[v] = 1group[v] = colfor e in RG[v]:if not seen[e]:task.append(e)for i in range(N):if not seen[i]:dfs(i)seen = [0]*Nlabel = 0for s in reversed(order):if not seen[s]:rdfs(s, label)label += 1return label, groupN = 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+jy = j*N+tnx = 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+jif sccgraph[n] == sccgraph[n+N**2]:print(-1)exit()elif sccgraph[n] < sccgraph[n+N**2]:ans[i][j] = 1for a in ans:print(*a)