結果
問題 | No.1266 7 Colors |
ユーザー |
![]() |
提出日時 | 2022-06-11 16:02:24 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 942 ms / 3,000 ms |
コード長 | 2,557 bytes |
コンパイル時間 | 148 ms |
コンパイル使用メモリ | 82,196 KB |
実行使用メモリ | 116,580 KB |
最終ジャッジ日時 | 2024-09-22 03:14:45 |
合計ジャッジ時間 | 16,888 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import sysimport pypyjitimport itertoolsimport heapqimport mathfrom collections import deque, defaultdictimport bisectinput = sys.stdin.readlinesys.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) - 1def 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) - 1def 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 = Nself.par = [-1] * Ndef find(self, x):if self.par[x] < 0:return xelse: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 Falseif x > y:x, y = y, xself.par[x] += self.par[y]self.par[y] = xreturn Truedef 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 + jN, 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 -= 1v -= 1E[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 -= 1y -= 1if t == 1:S[x][y] = 1pre = (y - 1) % 7nxt = (y + 1) % 7for 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)))