結果

問題 No.1266 7 Colors
ユーザー terasaterasa
提出日時 2022-06-11 16:02:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 847 ms / 3,000 ms
コード長 2,557 bytes
コンパイル時間 990 ms
コンパイル使用メモリ 81,456 KB
実行使用メモリ 115,668 KB
最終ジャッジ日時 2023-10-22 01:32:05
合計ジャッジ時間 17,440 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,588 KB
testcase_01 AC 44 ms
55,588 KB
testcase_02 AC 43 ms
55,588 KB
testcase_03 AC 706 ms
89,600 KB
testcase_04 AC 759 ms
100,708 KB
testcase_05 AC 714 ms
90,924 KB
testcase_06 AC 766 ms
102,784 KB
testcase_07 AC 824 ms
106,912 KB
testcase_08 AC 785 ms
101,344 KB
testcase_09 AC 750 ms
96,832 KB
testcase_10 AC 762 ms
95,336 KB
testcase_11 AC 758 ms
91,564 KB
testcase_12 AC 753 ms
92,676 KB
testcase_13 AC 752 ms
93,908 KB
testcase_14 AC 749 ms
90,240 KB
testcase_15 AC 821 ms
107,036 KB
testcase_16 AC 767 ms
93,408 KB
testcase_17 AC 809 ms
105,680 KB
testcase_18 AC 847 ms
115,668 KB
testcase_19 AC 832 ms
107,532 KB
testcase_20 AC 839 ms
107,520 KB
testcase_21 AC 262 ms
83,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


class UnionFind:
    def __init__(self, N):
        self.N = N
        self.par = [-1] * N

    def find(self, x):
        if self.par[x] < 0:
            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 False
        if x > y:
            x, y = y, x

        self.par[x] += self.par[y]
        self.par[y] = x
        return True

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.par[self.find(x)]

    def roots(self):
        return [i for i in range(self.N) if self.par[i] < 0]

    def members(self, x):
        p = self.find(x)
        return [i for i in range(self.N) if self.find(i) == p]


def h(i, j):
    return i * 7 + j


N, M, Q = map(int, input().split())
S = [[int(c) for c in input()[:-1]] for _ in range(N)]
uf = UnionFind(7 * N)
A = list(range(7))
B = list(range(1, 7)) + [0]
for i in range(N):
    s = S[i]
    for a, b in zip(A, B):
        if s[a] * s[b] > 0:
            uf.unite(h(i, a), h(i, b))

E = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    E[u].append(v)
    E[v].append(u)
    for j in range(7):
        if S[u][j] * S[v][j] > 0:
            uf.unite(h(u, j), h(v, j))

for _ in range(Q):
    t, x, y = map(int, input().split())
    x -= 1
    y -= 1
    if t == 1:
        S[x][y] = 1
        pre = (y - 1) % 7
        nxt = (y + 1) % 7
        for j in [pre, nxt]:
            if S[x][j] > 0:
                uf.unite(h(x, y), h(x, j))
        for d in E[x]:
            if S[d][y]:
                uf.unite(h(x, y), h(d, y))
    else:
        print(uf.size(h(x, 0)))
0