結果

問題 No.1266 7 Colors
ユーザー 👑 tamatotamato
提出日時 2020-10-23 22:44:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,113 ms / 3,000 ms
コード長 2,353 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 87,368 KB
実行使用メモリ 144,400 KB
最終ジャッジ日時 2023-09-28 17:04:52
合計ジャッジ時間 18,939 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,712 KB
testcase_01 AC 79 ms
71,708 KB
testcase_02 AC 81 ms
71,392 KB
testcase_03 AC 836 ms
97,596 KB
testcase_04 AC 970 ms
126,412 KB
testcase_05 AC 880 ms
100,824 KB
testcase_06 AC 1,042 ms
133,372 KB
testcase_07 AC 1,113 ms
141,772 KB
testcase_08 AC 1,008 ms
127,996 KB
testcase_09 AC 948 ms
118,532 KB
testcase_10 AC 925 ms
115,048 KB
testcase_11 AC 881 ms
103,200 KB
testcase_12 AC 939 ms
108,036 KB
testcase_13 AC 929 ms
110,516 KB
testcase_14 AC 865 ms
99,448 KB
testcase_15 AC 1,106 ms
142,964 KB
testcase_16 AC 950 ms
108,536 KB
testcase_17 AC 1,094 ms
138,892 KB
testcase_18 AC 541 ms
144,400 KB
testcase_19 AC 549 ms
142,104 KB
testcase_20 AC 541 ms
141,812 KB
testcase_21 AC 252 ms
86,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    N, M, Q = map(int, input().split())
    S = []
    for _ in range(N):
        S.append(list(input().rstrip('\n')))
    E = []
    adj = [[] for _ in range(N+1)]
    for _ in range(M):
        u, v = map(int, input().split())
        E.append((u, v))
        adj[u].append(v)
        adj[v].append(u)

    UF = UnionFind(N * 7)
    for i in range(N):
        si = S[i]
        for j in range(6):
            if si[j] == si[j+1] == "1":
                UF.unite(i+1 + j*N, i+1 + (j+1)*N)
        if si[0] == si[-1] == "1":
            UF.unite(i+1, i+1 + 6*N)
    for u, v in E:
        su = S[u-1]
        sv = S[v-1]
        for j in range(7):
            if su[j] == sv[j] == "1":
                UF.unite(u + j*N, v + j*N)

    for _ in range(Q):
        t, x, y = map(int, input().split())
        if t == 1:
            # self city
            S[x-1][y-1] = "1"
            prev = y-1 if y > 1 else 7
            nxt = y+1 if y < 7 else 1
            if S[x-1][prev-1] == "1":
                UF.unite(x + (y-1)*N, x + (prev-1)*N)
            if S[x-1][nxt-1] == "1":
                UF.unite(x + (y - 1) * N, x + (nxt - 1) * N)

            # adj
            for z in adj[x]:
                if S[z-1][y-1] == "1":
                    UF.unite(x + (y-1)*N, z + (y-1)*N)
        else:
            print(UF.size(x))


if __name__ == '__main__':
    main()
0