結果

問題 No.1266 7 Colors
ユーザー hir355hir355
提出日時 2020-10-23 23:17:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,821 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 126,724 KB
最終ジャッジ日時 2023-09-28 18:29:59
合計ジャッジ時間 21,376 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,396 KB
testcase_01 AC 72 ms
71,336 KB
testcase_02 AC 73 ms
71,468 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 612 ms
122,220 KB
testcase_19 AC 441 ms
120,160 KB
testcase_20 AC 446 ms
121,208 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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:
        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