結果

問題 No.1078 I love Matrix Construction
ユーザー rlangevinrlangevin
提出日時 2024-02-04 14:52:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,203 ms / 2,000 ms
コード長 4,205 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 229,656 KB
最終ジャッジ日時 2024-02-04 14:53:15
合計ジャッジ時間 15,518 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 39 ms
53,460 KB
testcase_02 AC 367 ms
100,648 KB
testcase_03 AC 575 ms
134,248 KB
testcase_04 AC 719 ms
155,596 KB
testcase_05 AC 696 ms
146,236 KB
testcase_06 AC 365 ms
100,676 KB
testcase_07 AC 272 ms
86,232 KB
testcase_08 AC 697 ms
144,244 KB
testcase_09 AC 192 ms
80,732 KB
testcase_10 AC 1,203 ms
229,656 KB
testcase_11 AC 771 ms
159,620 KB
testcase_12 AC 1,032 ms
202,044 KB
testcase_13 AC 1,121 ms
226,112 KB
testcase_14 AC 852 ms
176,008 KB
testcase_15 AC 1,076 ms
215,372 KB
testcase_16 AC 248 ms
84,536 KB
testcase_17 AC 38 ms
53,460 KB
testcase_18 AC 328 ms
94,300 KB
testcase_19 AC 453 ms
114,404 KB
testcase_20 AC 446 ms
113,048 KB
testcase_21 AC 111 ms
77,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DirectedGraph():
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        self.rG = [[] for i in range(N)]
        self.order = []
        self.used1 = [0] * N
        self.used2 = [0] * N
        self.group = [-1] * N
        self.label = 0
        self.seen = [0] * N
        self.Edge = set()

    def add_edge(self, u, v):
        #多重辺は排除する
        if (u, v) not in self.Edge:
            self.G[u].append(v)
            self.rG[v].append(u)
            self.Edge.add((u, v))

    def dfs(self, s):
        stack = [~s, s]
        while stack:
            u = stack.pop()
            if u >= 0:
                if self.used1[u]:
                    continue
                self.used1[u] = 1
                for v in self.G[u]:
                    if self.used1[v]:
                        continue
                    stack.append(~v)
                    stack.append(v)
            else:
                u = ~u
                if self.seen[u]:
                    continue
                self.seen[u]= 1
                self.order.append(u)

    def rdfs(self, s, num):
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                self.used2[u] = 1
                self.group[u] = num
                for v in self.rG[u]:
                    if self.used2[v]:
                        continue
                    stack.append(v)

    def scc(self):
        for i in range(self.N):
            if self.used1[i]:
                continue
            self.dfs(i)
        for s in reversed(self.order):
            if self.used2[s]:
                continue
            self.rdfs(s, self.label)
            self.label += 1
        return self.label, self.group

    def construct(self):
        nG = [set() for _ in range(self.label)]
        mem = [[] for i in range(self.label)]
        for s in range(self.N):
            now = self.group[s]
            for u in self.G[s]:
                if now == self.group[u]:
                    continue
                nG[now].add(self.group[u])
            mem[now].append(s)
        return nG, mem


class TwoSAT():
    def __init__(self, N):
        self.N = N
        self.G = DirectedGraph(2 * N)
        
    def add(self, x1, x2, f1, f2):
        if f1 == True and f2 == True:
            # ¬x1∪¬x2
            # (x1⇒¬x2)∩(x2⇒¬x1)
            self.G.add_edge(x1, x2 + self.N)
            self.G.add_edge(x2, x1 + self.N)
            
        if f1 == True and f2 == False:
            # ¬x1∪x2
            # (x1⇒x2)∩(¬x2⇒¬x1)
            self.G.add_edge(x1, x2)
            self.G.add_edge(x2 + self.N, x1 + self.N)
        
        if f1 == False and f2 == True:
            # x1∪¬x2
            # (¬x1⇒¬x2)∩(x2⇒x1)
            self.G.add_edge(x1 + self.N, x2 + self.N)
            self.G.add_edge(x2, x1)
            
        if f1 == False and f2 == False:
            # x1∪x2
            # (¬x1⇒x2)∩(¬x2⇒x1)
            self.G.add_edge(x1 + self.N, x2)
            self.G.add_edge(x2 + self.N, x1)
            
    def check(self):
        _, group = self.G.scc()
        ans = []
        for i in range(self.N):
            if group[i] == group[i + self.N]:
                print("-1")
                exit()
            if group[i] > group[i + self.N]:
                ans.append(1)
            else:
                ans.append(0)
        return ans
    
    
N = int(input())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
U = list(map(int, input().split()))
for i in range(N):
    S[i], T[i] = S[i] - 1, T[i] - 1

TS = TwoSAT(N*N)
for i in range(N):
    for j in range(N):
        if U[i] == 3:
            TS.add(S[i]*N+j, j*N+T[i], True, True)
        elif U[i] == 2:
            TS.add(S[i]*N+j, j*N+T[i], False, True)
        elif U[i] == 1:
            TS.add(S[i]*N+j, j*N+T[i], True, False)
        else:
            TS.add(S[i]*N+j, j*N+T[i], False, False)
            
flag = TS.check()
for i in range(N):
    ans = []
    for j in range(N):
        if flag[i*N+j]:
            ans.append(1)
        else:
            ans.append(0)
    print(*ans)
0