結果

問題 No.1266 7 Colors
ユーザー AEnAEn
提出日時 2022-11-25 23:14:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,458 ms / 3,000 ms
コード長 3,023 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 136,272 KB
最終ジャッジ日時 2024-04-10 04:19:20
合計ジャッジ時間 22,855 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
68,648 KB
testcase_01 AC 59 ms
67,740 KB
testcase_02 AC 60 ms
69,052 KB
testcase_03 AC 992 ms
93,152 KB
testcase_04 AC 1,090 ms
121,144 KB
testcase_05 AC 1,148 ms
98,036 KB
testcase_06 AC 1,405 ms
128,336 KB
testcase_07 AC 1,145 ms
131,212 KB
testcase_08 AC 1,390 ms
126,324 KB
testcase_09 AC 1,068 ms
111,864 KB
testcase_10 AC 1,381 ms
112,468 KB
testcase_11 AC 1,214 ms
102,136 KB
testcase_12 AC 1,103 ms
103,392 KB
testcase_13 AC 1,254 ms
108,788 KB
testcase_14 AC 1,219 ms
98,092 KB
testcase_15 AC 1,458 ms
136,272 KB
testcase_16 AC 1,233 ms
105,340 KB
testcase_17 AC 1,119 ms
129,836 KB
testcase_18 AC 545 ms
135,668 KB
testcase_19 AC 400 ms
132,756 KB
testcase_20 AC 401 ms
133,564 KB
testcase_21 AC 398 ms
82,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

N, M, Q = map(int, input().split())
uf = UnionFind(7*N)
col = []
for i in range(N):
    s = list(input())
    for j in range(7):
        if s[j]=='1' and s[(j+1)%7]=='1':
            uf.unite(i*7+j,i*7+((j+1)%7))
    col.append(s)
G = [list() for _ in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    u-=1;v-=1
    G[u].append(v)
    G[v].append(u)
    for j in range(7):
        if col[u][j]=='1' and col[v][j]=='1':
            uf.unite(7*u+j, 7*v+j)

for i in range(Q):
    t, x, y = map(int, input().split())
    if t==1:
        col[x-1][y-1] = '1'
        if col[x-1][y%7]=='1':
            uf.unite(7*(x-1)+(y%7),7*(x-1)+y-1)
        if col[x-1][(y-2)%7]=='1':
            uf.unite(7*(x-1)+((y-2)%7), 7*(x-1)+y-1)
        for nex in G[x-1]:
            if col[nex][y-1] == '1':
                uf.unite(7*(x-1)+y-1,7*nex+y-1)
    else:
        print(uf.size(7*(x-1)))
0