結果

問題 No.1266 7 Colors
ユーザー lloyzlloyz
提出日時 2022-10-20 00:14:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,211 ms / 3,000 ms
コード長 2,710 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 119,240 KB
最終ジャッジ日時 2023-09-12 11:27:17
合計ジャッジ時間 20,784 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,220 KB
testcase_01 AC 91 ms
71,380 KB
testcase_02 AC 92 ms
71,496 KB
testcase_03 AC 708 ms
88,548 KB
testcase_04 AC 1,108 ms
107,356 KB
testcase_05 AC 711 ms
91,148 KB
testcase_06 AC 1,142 ms
113,456 KB
testcase_07 AC 1,168 ms
116,384 KB
testcase_08 AC 1,098 ms
108,916 KB
testcase_09 AC 988 ms
103,380 KB
testcase_10 AC 983 ms
103,660 KB
testcase_11 AC 810 ms
94,004 KB
testcase_12 AC 879 ms
97,604 KB
testcase_13 AC 891 ms
96,420 KB
testcase_14 AC 708 ms
89,636 KB
testcase_15 AC 1,211 ms
119,240 KB
testcase_16 AC 876 ms
96,340 KB
testcase_17 AC 1,172 ms
115,516 KB
testcase_18 AC 606 ms
118,264 KB
testcase_19 AC 453 ms
113,828 KB
testcase_20 AC 456 ms
113,928 KB
testcase_21 AC 473 ms
82,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

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

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n, m, q = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(n)]
G = defaultdict(list)
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

UF = UnionFind(7 * n)
for i in range(n):
    for j in G[i]:
        for c in range(7):
            if S[i][c] == S[j][c] == 1:
                UF.union(7 * i + c, 7 * j + c)
    for c in range(7):
        c2 = (c + 1) % 7
        if S[i][c] == S[i][c2] == 1:
            UF.union(7 * i + c, 7 * i + c2)

for _ in range(q):
    i, x, y = map(int, input().split())
    x -= 1
    y -= 1
    if i == 1:
        S[x][y] += 1
        for v in G[x]:
            if S[v][y] == 1:
                UF.union(7 * x + y, 7 * v + y)
        for c2 in [(y + 1) % 7, (y - 1) % 7]:
            if S[x][c2] == 1:
                UF.union(7 * x + y, 7 * x + c2)
    else:
        print(UF.size(7 * x))
0