結果

問題 No.1266 7 Colors
ユーザー maninimanini
提出日時 2021-01-25 20:16:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,356 ms / 3,000 ms
コード長 3,430 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 146,068 KB
最終ジャッジ日時 2023-09-04 17:35:49
合計ジャッジ時間 21,242 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,432 KB
testcase_01 AC 74 ms
71,312 KB
testcase_02 AC 73 ms
71,372 KB
testcase_03 AC 728 ms
110,540 KB
testcase_04 AC 1,123 ms
132,920 KB
testcase_05 AC 729 ms
111,692 KB
testcase_06 AC 1,177 ms
138,076 KB
testcase_07 AC 1,356 ms
146,068 KB
testcase_08 AC 1,141 ms
133,852 KB
testcase_09 AC 1,029 ms
127,480 KB
testcase_10 AC 995 ms
122,716 KB
testcase_11 AC 796 ms
114,324 KB
testcase_12 AC 888 ms
118,852 KB
testcase_13 AC 968 ms
122,296 KB
testcase_14 AC 738 ms
112,224 KB
testcase_15 AC 1,265 ms
144,712 KB
testcase_16 AC 990 ms
119,900 KB
testcase_17 AC 1,273 ms
145,232 KB
testcase_18 AC 490 ms
145,380 KB
testcase_19 AC 495 ms
140,448 KB
testcase_20 AC 488 ms
141,056 KB
testcase_21 AC 346 ms
101,176 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