結果

問題 No.1266 7 Colors
ユーザー tpynerivertpyneriver
提出日時 2020-10-23 22:39:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 850 ms / 3,000 ms
コード長 1,733 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 100,552 KB
最終ジャッジ日時 2024-07-21 11:31:26
合計ジャッジ時間 13,128 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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[vf]>>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