結果

問題 No.1266 7 Colors
ユーザー nagissnagiss
提出日時 2020-10-24 03:49:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,438 ms / 3,000 ms
コード長 2,170 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 11,256 KB
実行使用メモリ 40,428 KB
最終ジャッジ日時 2023-09-28 20:05:06
合計ジャッジ時間 21,295 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,380 KB
testcase_01 AC 18 ms
8,376 KB
testcase_02 AC 17 ms
8,560 KB
testcase_03 AC 855 ms
26,788 KB
testcase_04 AC 1,027 ms
34,492 KB
testcase_05 AC 855 ms
27,096 KB
testcase_06 AC 1,061 ms
36,044 KB
testcase_07 AC 1,119 ms
38,588 KB
testcase_08 AC 1,045 ms
35,076 KB
testcase_09 AC 962 ms
31,648 KB
testcase_10 AC 951 ms
30,628 KB
testcase_11 AC 884 ms
27,644 KB
testcase_12 AC 900 ms
28,368 KB
testcase_13 AC 916 ms
29,288 KB
testcase_14 AC 865 ms
26,832 KB
testcase_15 AC 1,119 ms
38,916 KB
testcase_16 AC 908 ms
28,644 KB
testcase_17 AC 1,102 ms
37,800 KB
testcase_18 AC 1,438 ms
40,428 KB
testcase_19 AC 1,425 ms
31,040 KB
testcase_20 AC 1,426 ms
31,108 KB
testcase_21 AC 280 ms
23,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

class UnionFind:
    def __init__(self, N):
        p = [-1] * N
 
        def root(x):
            while p[x] >= 0:
                x = p[x]
            return x
 
        def same(x, y):
            return root(x) == root(y)
 
        def unite(x, y):
            x = root(x)
            y = root(y)
            if x == y:
                return
            if p[x] > p[y]:
                p[y] += p[x]
                p[x] = y
            else:
                p[x] += p[y]
                p[y] = x
 
        def count(x):
            return -p[root(x)]
 
        self.root = root
        self.same = same
        self.unite = unite
        self.count = count


def main():
    input = sys.stdin.buffer.readline
    N, M, Q = map(int, input().split())
    S = [int(input(), 2) for _ in range(N)]
    G = [[] for _ in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        G[u].append(v)
        G[v].append(u)
    uf = UnionFind(7 * N)
    for v, s in enumerate(S):
        ones = [i for i in range(7) if s>>(6-i)&1]
        v_offset = 7 * v
        if len(ones) >= 2:
            for o1, o2 in zip(ones, ones[1:]):
                if o1 + 1 == o2:
                    uf.unite(v_offset+o1, v_offset+o2)
            if ones[0] == 0 and ones[-1] == 6:
                uf.unite(v_offset, v_offset+6)
        for u in G[v]:
            for o in ones:
                if S[u]>>(6-o)&1:
                    uf.unite(v_offset+o, u*7+o)
    Ans = []
    for _ in range(Q):
        t, x, y = map(int, input().split())
        x -= 1
        if t == 1:
            v_offset = x * 7
            d = 7-y
            S[x] |= 1 << d
            v = v_offset + (y-1)
            d2 = (d-1)%7
            if S[x] >> d2 & 1:
                uf.unite(v, v_offset+y%7)
            d2 = (d+1)%7
            if S[x] >> d2 & 1:
                uf.unite(v, v_offset+(y-2)%7)
            for u in G[x]:
                if S[u]>>d&1:
                    u = u * 7 + y-1
                    uf.unite(v, u)
        else:
            ans = uf.count(x*7)
            Ans.append(ans)
    print("\n".join(map(str, Ans)))

main()
0