結果

問題 No.1266 7 Colors
ユーザー hir355hir355
提出日時 2020-10-23 23:23:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,476 ms / 3,000 ms
コード長 1,841 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 126,036 KB
最終ジャッジ日時 2023-09-28 18:39:20
合計ジャッジ時間 21,427 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,088 KB
testcase_01 AC 74 ms
71,152 KB
testcase_02 AC 78 ms
71,436 KB
testcase_03 AC 782 ms
90,708 KB
testcase_04 AC 1,309 ms
114,216 KB
testcase_05 AC 780 ms
91,816 KB
testcase_06 AC 1,361 ms
119,880 KB
testcase_07 AC 1,476 ms
123,524 KB
testcase_08 AC 1,351 ms
115,668 KB
testcase_09 AC 1,138 ms
107,116 KB
testcase_10 AC 1,136 ms
105,640 KB
testcase_11 AC 890 ms
94,796 KB
testcase_12 AC 926 ms
99,240 KB
testcase_13 AC 1,036 ms
99,592 KB
testcase_14 AC 762 ms
92,104 KB
testcase_15 AC 1,427 ms
126,036 KB
testcase_16 AC 1,088 ms
101,792 KB
testcase_17 AC 1,375 ms
121,548 KB
testcase_18 AC 620 ms
122,004 KB
testcase_19 AC 440 ms
120,040 KB
testcase_20 AC 436 ms
121,208 KB
testcase_21 AC 437 ms
80,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


n, m, q = map(int, input().split())
s = [list(map(int, input())) for _ in range(n)]
uf = UnionFind(n * 7)
g = [[] for _ in range(n)]
for i in range(m):
    u, v = map(int, input().split())
    g[u - 1].append(v - 1)
    g[v - 1].append(u - 1)
for i in range(n * 7):
    v, c = i // 7, i % 7
    if s[v][c]:
        if s[v][(c + 1) % 7]:
            uf.unite(i, v * 7 + (c + 1) % 7)
        if s[v][(c - 1) % 7]:
            uf.unite(i, v * 7 + (c - 1) % 7)
        for node in g[v]:
            if s[node][c]:
                uf.unite(i, node * 7 + c)
for _ in range(q):
    t, x, y = map(int, input().split())
    x -= 1
    y -= 1
    if t == 1:
        s[x][y] = 1
        for node in g[x]:
            if s[node][y]:
                uf.unite(x * 7 + y, node * 7 + y)
        if s[x][(y + 1) % 7]:
            uf.unite(x * 7 + y, x * 7 + (y + 1) % 7)
        if s[x][(y - 1) % 7]:
            uf.unite(x * 7 + y, x * 7 + (y - 1) % 7)
    else:
        print(uf.size[uf.find(x * 7)])
0