結果

問題 No.1266 7 Colors
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-10-24 00:16:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,430 ms / 3,000 ms
コード長 2,123 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 86,908 KB
実行使用メモリ 233,308 KB
最終ジャッジ日時 2023-09-28 19:30:42
合計ジャッジ時間 22,105 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
70,940 KB
testcase_01 AC 74 ms
71,352 KB
testcase_02 AC 73 ms
71,380 KB
testcase_03 AC 923 ms
106,360 KB
testcase_04 AC 1,224 ms
176,904 KB
testcase_05 AC 936 ms
112,772 KB
testcase_06 AC 1,271 ms
189,776 KB
testcase_07 AC 1,376 ms
207,132 KB
testcase_08 AC 1,236 ms
179,332 KB
testcase_09 AC 1,158 ms
154,368 KB
testcase_10 AC 1,101 ms
144,916 KB
testcase_11 AC 976 ms
119,420 KB
testcase_12 AC 1,037 ms
126,704 KB
testcase_13 AC 1,059 ms
135,788 KB
testcase_14 AC 921 ms
110,828 KB
testcase_15 AC 1,430 ms
212,748 KB
testcase_16 AC 1,028 ms
128,136 KB
testcase_17 AC 1,355 ms
204,180 KB
testcase_18 AC 787 ms
233,308 KB
testcase_19 AC 749 ms
219,320 KB
testcase_20 AC 756 ms
219,580 KB
testcase_21 AC 239 ms
85,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindNode:
    def __init__(self, group_id, parent=None, value=None):
        self.group_id_ = group_id
        self.parent_ = parent
        self.value = value
        self.rank_ = 1
        self.member_num_ = 1

    def is_root(self):
        return not self.parent_

    def root(self):
        parent = self
        while not parent.is_root():
            parent = parent.parent_
            self.parent_ = parent
        return parent

    def find(self):
        root = self.root()
        return root.group_id_

    def rank(self):
        root = self.root()
        return root.rank_

    def unite(self, unite_node):
        root = self.root()
        unite_root = unite_node.root()

        if root.group_id_ != unite_root.group_id_:
            if root.rank() > unite_root.rank():
                n_root, child = root, unite_root
            else:
                n_root, child = unite_root, root
            child.parent_ = n_root
            n_root.rank_ = max(n_root.rank_, child.rank_ + 1)
            n_root.member_num_ = n_root.member_num_ + child.member_num_


import sys;input=sys.stdin.readline
N, M, Q = map(int, input().split())
nodes = [UnionFindNode(i) for i in range(N*7)]
ss = []
for i in range(N):
    s = input().strip()
    ss.append([c for c in s])
    n = i*7
    for j in range(7):
        if s[j] == "1" and s[(j+1)%7] == "1":
            nodes[n+j].unite(nodes[n+(j+1)%7])

G = [set() for _ in range(N)]
for _ in range(M):
    u, v = map(int, input().split())
    u-=1;v-=1;
    G[u].add(v)
    G[v].add(u)
    for i in range(7):
        if ss[u][i] == "1" and ss[v][i] == "1":
            nodes[u*7+i].unite(nodes[v*7+i])
for _ in range(Q):
    a, b, c = map(int, input().split())
    if a == 2:
        print(nodes[(b-1)*7].root().member_num_)
    else:
        b-=1
        c-=1
        ss[b][c] = "1"
        if ss[b][(c+1)%7] == "1":
            nodes[b*7+c].unite(nodes[b*7+(c+1)%7])
        if ss[b][(c-1)%7] == "1":
            nodes[b*7+c].unite(nodes[b*7+(c-1)%7])
        for u in G[b]:
            if ss[u][c] == "1":
                nodes[b*7+c].unite(nodes[u*7+c])
0