結果

問題 No.1266 7 Colors
ユーザー tpynerivertpyneriver
提出日時 2020-10-23 22:36:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,732 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 86,716 KB
実行使用メモリ 101,852 KB
最終ジャッジ日時 2023-09-28 16:44:23
合計ジャッジ時間 13,791 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,288 KB
testcase_01 AC 76 ms
71,416 KB
testcase_02 AC 74 ms
71,536 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 445 ms
101,852 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

class UF():
    def __init__(self, num):
        self.par = [-1]*num
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            stack = []
            while self.par[x] >= 0:
                stack.append(x)
                x = self.par[x]
            for xi in stack:
                self.par[xi] = x
            return x
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] > self.par[ry]:
                rx, ry = ry, rx
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
            return True
        return False

N, M, Q = map(int, readline().split())
color = [int(readline().strip()[::-1], 2) for _ in range(N)]

C = 7

Edge = [[] for _ in range(N)]

T = UF(N*C)
for _ in range(M):
    u, v = map(int, readline().split())
    u -= 1
    v -= 1
    Edge[u].append(v)
    Edge[v].append(u)
    
    for i in range(C):
        if (color[u]>>i) & 1 and (color[v]>>i) & 1:
            T.union(u*C+i, v*C+i)

for i in range(N):
    for c in range(C):
        cf = (c+1)%C
        if (color[i]>>c) & 1 and (color[i]>>cf) & 1:
            T.union(i*C+c, i*C+cf)


Ans = []
for _ in range(Q):
    t, x, y = map(int, readline().split())
    x -= 1
    y -= 1
    if t == 1:
        color[x] |= 1<<y
        for vf in Edge[x]:
            if (color[y]>>y)&1:
                T.union(x*C+y, vf*C+y)
        cm = ((y-1)%C)
        if (color[x]>>cm)&1:
            T.union(x*C+cm, x*C+y)
        cp = ((y+1)%C)
        if (color[x]>>cp)&1:
            T.union(x*C+cp, x*C+y)
    else:
        Ans.append(-T.par[T.find(x*C)])

print('\n'.join(map(str, Ans)))
0