結果

問題 No.1266 7 Colors
ユーザー NatsubiSoganNatsubiSogan
提出日時 2020-10-24 00:45:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,707 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 138,136 KB
最終ジャッジ日時 2023-09-28 19:48:33
合計ジャッジ時間 22,123 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
71,124 KB
testcase_01 AC 80 ms
71,012 KB
testcase_02 WA -
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 WA -
testcase_19 AC 447 ms
135,456 KB
testcase_20 AC 444 ms
135,176 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#Union-Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * n
        self.count = n
    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):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.count -= 1
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def size(self, x):
        return self.rank[x]
    def count(self):
        return self.count
n, m, q = map(int, input().split())
s = [list(input()) for i in range(n)]
UF = UnionFind(n * 7)
for i in range(n):
    for j in range(7):
        if s[i][j] == "1" and s[i][(j + 1) % 7] == "1":
            UF.unite(i * 7 + j, i * 7 + (j + 1) % 7)
edges = [[] for i in range(n)]
for i in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    edges[x].append(y)
    edges[y].append(x)
    for j in range(7):
        if s[x][j] == "1" and s[y][j] == "1":
            UF.unite(x * 7 + j, y * 7 + j)
for i in range(q):
    qu, a, b = map(int, input().split())
    a -= 1
    b -= 1
    if qu == 1:
        s[a][b] = "1"
        if s[a][(b - 1) % 7] == "1":
            UF.unite(a * 7 + (b - 1) % 7, a * 7 + b)
        if s[a][(b + 1) % 7] == "1":
            UF.unite(a * 7 + (b + 1) % 7, a * 7 + b)
        for v in edges[a]:
            if s[v][b] == "1":
                UF.unite(a * 7 + b, v * 7 + b)
    else:
        print(UF.size(a * 7))
0