結果

問題 No.1266 7 Colors
ユーザー Coki628Coki628
提出日時 2020-12-06 02:02:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,234 ms / 3,000 ms
コード長 3,339 bytes
コンパイル時間 1,932 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 270,568 KB
最終ジャッジ日時 2023-10-14 21:47:38
合計ジャッジ時間 36,877 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,680 KB
testcase_01 AC 77 ms
71,920 KB
testcase_02 AC 76 ms
71,820 KB
testcase_03 AC 1,627 ms
142,632 KB
testcase_04 AC 1,852 ms
229,924 KB
testcase_05 AC 1,651 ms
146,700 KB
testcase_06 AC 1,808 ms
244,676 KB
testcase_07 AC 2,234 ms
269,340 KB
testcase_08 AC 1,875 ms
233,676 KB
testcase_09 AC 1,736 ms
199,668 KB
testcase_10 AC 1,736 ms
186,696 KB
testcase_11 AC 1,675 ms
153,892 KB
testcase_12 AC 1,658 ms
162,416 KB
testcase_13 AC 1,646 ms
173,940 KB
testcase_14 AC 1,645 ms
145,932 KB
testcase_15 AC 2,110 ms
270,568 KB
testcase_16 AC 1,701 ms
165,388 KB
testcase_17 AC 1,907 ms
258,416 KB
testcase_18 AC 724 ms
149,364 KB
testcase_19 AC 818 ms
173,132 KB
testcase_20 AC 823 ms
173,264 KB
testcase_21 AC 477 ms
147,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10**9)
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10

class UnionFind:
    """ Union-Find木 """

    def __init__(self, n):
        self.n = n
        self.par = [i for i in range(n)]
        self.rank = [0] * n
        self.size = [1] * n
        self.tree = [True] * n
        self.grpcnt = n

    def find(self, x):
        """ 根の検索(グループ番号の取得) """
        t = []
        while self.par[x] != x:
            t.append(x)
            x = self.par[x]
        for i in t:
            self.par[i] = x
        return self.par[x]

    def union(self, x, y):
        """ 併合 """
        x = self.find(x)
        y = self.find(y)

        if x == y:
            self.tree[x] = False
            return
        if not self.tree[x] or not self.tree[y]:
            self.tree[x] = self.tree[y] = False

        self.grpcnt -= 1
        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 is_same(self, x, y):
        """ 同じ集合に属するか判定 """
        return self.find(x) == self.find(y)

    def get_size(self, x=None):
        if x is not None:
            """ あるノードの属する集合のノード数 """
            return self.size[self.find(x)]
        else:
            """ 集合の数 """
            return self.grpcnt
    
    def is_tree(self, x):
        """ 木かどうかの判定 """
        return self.tree[self.find(x)]

    def get_roots(self):
        """ 全ての根を取得 """

        return set([self.find(i) for i in range(self.n)])

N, M, Q = MAP()
L = 7
uf = UnionFind(N*L)
C = [[]] * N
for i in range(N):
    C[i] = list(map(int, input()))
    for j in range(L):
        if C[i][j] and C[i][(j+1)%L]:
            uf.union(i*L+j, i*L+(j+1)%L)
nodes = [[set() for j in range(7)] for i in range(N)]
for i in range(M):
    u, v = MAP()
    u -= 1; v -= 1
    for j in range(L):
        if C[u][j] and C[v][j]:
            uf.union(u*L+j, v*L+j)
        else:
            nodes[u][j].add(v)
            nodes[v][j].add(u)

for _ in range(Q):
    op, x, c = MAP()
    x -= 1; c -= 1
    if op == 1:
        C[x][c] = 1
        if C[x][(c-1)%L]:
            uf.union(x*L+c, x*L+(c-1)%L)
        if C[x][(c+1)%L]:
            uf.union(x*L+c, x*L+(c+1)%L)
        tmp = []
        for v in nodes[x][c]:
            if C[v][c]:
                uf.union(x*L+c, v*L+c)
                tmp.append(v)
        for v in tmp:
            nodes[x][c].remove(v)
            nodes[v][c].remove(x)
    else:
        res = uf.get_size(x*L)
        print(res)
0