結果

問題 No.1266 7 Colors
ユーザー H3PO4H3PO4
提出日時 2022-02-04 22:30:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,610 ms / 3,000 ms
コード長 2,412 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 124,784 KB
最終ジャッジ日時 2023-09-02 05:07:08
合計ジャッジ時間 22,536 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,300 KB
testcase_01 AC 72 ms
71,264 KB
testcase_02 AC 74 ms
71,324 KB
testcase_03 AC 828 ms
91,396 KB
testcase_04 AC 1,279 ms
115,332 KB
testcase_05 AC 873 ms
94,552 KB
testcase_06 AC 1,372 ms
117,172 KB
testcase_07 AC 1,610 ms
124,784 KB
testcase_08 AC 1,288 ms
114,744 KB
testcase_09 AC 1,146 ms
106,752 KB
testcase_10 AC 1,137 ms
103,496 KB
testcase_11 AC 1,016 ms
96,556 KB
testcase_12 AC 982 ms
97,684 KB
testcase_13 AC 1,078 ms
101,456 KB
testcase_14 AC 837 ms
93,456 KB
testcase_15 AC 1,497 ms
124,460 KB
testcase_16 AC 1,105 ms
101,360 KB
testcase_17 AC 1,378 ms
120,432 KB
testcase_18 AC 670 ms
121,988 KB
testcase_19 AC 479 ms
118,824 KB
testcase_20 AC 482 ms
120,192 KB
testcase_21 AC 457 ms
83,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    __slots__ = ["n", "parent", "height", "size"]

    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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

    def group_size(self, x):
        return self.size[self.find(x)]

    def group_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.parent) if i == x]

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


N, M, Q = map(int, input().split())
uf = UnionFind(7 * N)
pair2int = lambda i, n: 7 * i + n
colors = [[s == "1" for s in input()] for _ in range(N)]
G = [[] for _ in range(N)]
for _ in range(M):
    u, v = (int(x) - 1 for x in input().split())
    G[u].append(v)
    G[v].append(u)

for i in range(N):
    for a in range(7):
        b = (a + 1) % 7
        if colors[i][a] == colors[i][b] == 1:
            uf.unite(pair2int(i, a), pair2int(i, b))

for i in range(N):
    for a in range(7):
        if not colors[i][a]:
            continue
        for j in G[i]:
            if colors[j][a]:
                uf.unite(pair2int(i, a), pair2int(j, a))
for _ in range(Q):
    q, x, y = map(int, input().split())
    x -= 1
    if q == 1:
        y -= 1
        colors[x][y] = 1

        l = (y - 1) % 7
        r = (y + 1) % 7
        if colors[x][l] == 1:
            uf.unite(pair2int(x, l), pair2int(x, y))
        if colors[x][r] == 1:
            uf.unite(pair2int(x, y), pair2int(x, r))

        for j in G[x]:
            if colors[j][y] == 1:
                uf.unite(pair2int(x, y), pair2int(j, y))
    else:
        assert q == 2
        print(uf.group_size(pair2int(x, 0)))
0