結果

問題 No.1266 7 Colors
ユーザー maninimanini
提出日時 2021-01-25 20:16:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,313 ms / 3,000 ms
コード長 3,430 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 144,340 KB
最終ジャッジ日時 2024-06-22 15:35:27
合計ジャッジ時間 19,326 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,408 KB
testcase_01 AC 37 ms
53,724 KB
testcase_02 AC 38 ms
54,388 KB
testcase_03 AC 663 ms
108,136 KB
testcase_04 AC 1,068 ms
130,692 KB
testcase_05 AC 697 ms
109,408 KB
testcase_06 AC 1,129 ms
134,304 KB
testcase_07 AC 1,313 ms
140,524 KB
testcase_08 AC 1,125 ms
131,860 KB
testcase_09 AC 973 ms
124,272 KB
testcase_10 AC 938 ms
121,280 KB
testcase_11 AC 750 ms
112,216 KB
testcase_12 AC 832 ms
114,944 KB
testcase_13 AC 944 ms
118,432 KB
testcase_14 AC 689 ms
109,292 KB
testcase_15 AC 1,223 ms
142,020 KB
testcase_16 AC 904 ms
116,820 KB
testcase_17 AC 1,207 ms
139,004 KB
testcase_18 AC 459 ms
144,340 KB
testcase_19 AC 467 ms
139,644 KB
testcase_20 AC 460 ms
139,572 KB
testcase_21 AC 317 ms
100,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding:UTF-8
import sys

MOD = 10 ** 9 + 7
INF = float('inf')

# ユニオンファインド木
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.rank = [0] * n

    def find(self, 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 = self.find(x)
        y = self.find(y)

        if x == y:
            return
        elif self.rank[x] > self.rank[y]:
            self.parents[x] += self.parents[y]
            self.parents[y] = x
        else:
            self.parents[y] += self.parents[x]
            self.parents[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def size(self, 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):
        d = {r: {r} for r in self.roots()}
        for i in range(self.n):
            d[self.find(i)].add(i)
        return d

    def __str__(self):
        d = self.all_group_members()
        return '\n'.join('{}: {}'.format(r, d[r]) for r in d.keys())


N, M, Q = list(map(int, input().split()))     # スペース区切り連続数字
St = [input() for _ in range(N)] # 文字
UV = [list(map(int, input().split())) for _ in range(M)]     # スペース区切り連続数字(行列)
Query = [list(map(int, input().split())) for _ in range(Q)]     # スペース区切り連続文字

S = []
for i in range(N):
    S.append([])
    for c in range(7):
        S[i].append(int(St[i][c]))

E = [[] for i in range(N)]
for u, v in UV:
    E[u-1].append(v-1)
    E[v-1].append(u-1)

uf = UnionFind(7*N)

for i in range(N):
    s = S[i]
    for c in range(7):
        if c == 6:
            if S[i][6] == 1 and S[i][0] == 1:
                uf.union(6 * N + i, i)
        else:
            if S[i][c] == 1 and S[i][c+1] == 1:
                uf.union(c * N + i, (c+1) * N + i)

for u, v in UV:
    for c in range(7):
        if S[u-1][c] == 1 and S[v-1][c] == 1:
            uf.union(c * N + (u - 1), c * N + (v - 1))

for q in Query:
    if q[0] == 1:
        i = q[1] - 1
        c = q[2] - 1
        if c == 0:
            S[i][0] = 1
            if S[i][6] == 1 and S[i][0] == 1:
                uf.union(6 * N + i, i)
            if S[i][0] == 1 and S[i][1] == 1:
                uf.union(N + i, i)
        elif c == 6:
            S[i][6] = 1
            if S[i][6] == 1 and S[i][0] == 1:
                uf.union(6 * N + i, i)
            if S[i][5] == 1 and S[i][6] == 1:
                uf.union(5 * N + i, 6 * N + i)
        else:
            S[i][c] = 1
            if S[i][c] == 1 and S[i][c-1] == 1:
                uf.union(c * N + i, (c-1) * N + i)
            if S[i][c] == 1 and S[i][c+1] == 1:
                uf.union(c * N + i, (c+1) * N + i)

        for e in E[i]:
            if S[i][c] == 1 and S[e][c] == 1:
                uf.union(c * N + i, c * N + e)

    elif q[0] == 2:
        i = q[1] - 1
        res = uf.size(i)
        print("{}".format(res))
0