結果

問題 No.1266 7 Colors
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-10-23 23:41:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,154 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 202,300 KB
最終ジャッジ日時 2023-09-28 19:01:44
合計ジャッジ時間 18,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,312 KB
testcase_01 AC 77 ms
71,220 KB
testcase_02 AC 77 ms
71,260 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 681 ms
202,300 KB
testcase_19 AC 636 ms
186,540 KB
testcase_20 AC 645 ms
186,692 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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())
nn = N*7
nodes = [UnionFindNode(i) for i in range(nn)]
ss = []
for i in range(N):
    s = input().strip()
    ss.append(s)
    n = i*7
    for j in range(6):
        if s[j] == "1" and s[j+1] == "1":
            nodes[n+j].unite(nodes[n+j+1])
    if s[0] == "1" and s[6] == "1":
        nodes[n].unite(nodes[n+6])

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] == 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
        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[u*7+c].unite(nodes[b*7+c])
0