結果
問題 | No.1266 7 Colors |
ユーザー | nagiss |
提出日時 | 2020-10-24 02:40:19 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 2,064 ms / 3,000 ms |
コード長 | 2,316 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 61,768 KB |
最終ジャッジ日時 | 2024-07-21 14:49:31 |
合計ジャッジ時間 | 26,136 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 28 ms
10,880 KB |
testcase_01 | AC | 29 ms
10,880 KB |
testcase_02 | AC | 34 ms
11,008 KB |
testcase_03 | AC | 1,085 ms
29,824 KB |
testcase_04 | AC | 1,207 ms
51,752 KB |
testcase_05 | AC | 1,068 ms
31,488 KB |
testcase_06 | AC | 1,250 ms
55,572 KB |
testcase_07 | AC | 1,335 ms
60,964 KB |
testcase_08 | AC | 1,262 ms
52,884 KB |
testcase_09 | AC | 1,212 ms
44,800 KB |
testcase_10 | AC | 1,190 ms
41,984 KB |
testcase_11 | AC | 1,064 ms
33,792 KB |
testcase_12 | AC | 1,059 ms
36,480 KB |
testcase_13 | AC | 1,087 ms
38,784 KB |
testcase_14 | AC | 1,046 ms
31,360 KB |
testcase_15 | AC | 1,346 ms
61,768 KB |
testcase_16 | AC | 1,054 ms
36,992 KB |
testcase_17 | AC | 1,350 ms
59,588 KB |
testcase_18 | AC | 2,041 ms
58,768 KB |
testcase_19 | AC | 2,064 ms
61,188 KB |
testcase_20 | AC | 2,062 ms
61,184 KB |
testcase_21 | AC | 380 ms
22,400 KB |
ソースコード
import sys class UnionFind: def __init__(self, N): self.p = list(range(N)) self.size = [1] * N def root(self, x): p = self.p[x] stk = [] while x != p: stk.append(x) p, x = self.p[p], p for c in stk: self.p[c] = p return p def same(self, x, y): return self.root(x) == self.root(y) def unite(self, x, y): u = self.root(x) v = self.root(y) if u == v: return if self.size[u] < self.size[v]: self.p[u] = v self.size[v] += self.size[u] self.size[u] = 0 else: self.p[v] = u self.size[u] += self.size[v] self.size[v] = 0 def count(self, x): return self.size[self.root(x)] 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()