結果

問題 No.1266 7 Colors
ユーザー NatsubiSogan
提出日時 2020-10-24 00:49:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,433 ms / 3,000 ms
コード長 1,598 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 131,852 KB
最終ジャッジ日時 2024-07-21 14:33:51
合計ジャッジ時間 21,370 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#Union-Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = [-1] * n
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
        return self.par[x]
    def unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if self.par[p] > self.par[q]:
            p, q = q, p
        self.par[p] += self.par[q]
        self.par[q] = p
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def size(self, x):
        return -self.par[self.find(x)]
n, m, q = map(int, input().split())
s = [list(input()) for i in range(n)]
UF = UnionFind(n * 7)
for i in range(n):
    for j in range(7):
        if s[i][j] == "1" and s[i][(j + 1) % 7] == "1":
            UF.unite(i * 7 + j, i * 7 + (j + 1) % 7)
edges = [[] for i in range(n)]
for i in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    edges[x].append(y)
    edges[y].append(x)
    for j in range(7):
        if s[x][j] == "1" and s[y][j] == "1":
            UF.unite(x * 7 + j, y * 7 + j)
for i in range(q):
    qu, a, b = map(int, input().split())
    a -= 1
    b -= 1
    if qu == 1:
        s[a][b] = "1"
        if s[a][(b - 1) % 7] == "1":
            UF.unite(a * 7 + (b - 1) % 7, a * 7 + b)
        if s[a][(b + 1) % 7] == "1":
            UF.unite(a * 7 + (b + 1) % 7, a * 7 + b)
        for v in edges[a]:
            if s[v][b] == "1":
                UF.unite(a * 7 + b, v * 7 + b)
    else:
        print(UF.size(a * 7))
0