結果
問題 | No.1266 7 Colors |
ユーザー | tktk_snsn |
提出日時 | 2020-12-29 16:52:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 927 ms / 3,000 ms |
コード長 | 1,817 bytes |
コンパイル時間 | 138 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 98,640 KB |
最終ジャッジ日時 | 2024-04-15 17:18:41 |
合計ジャッジ時間 | 13,974 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,712 KB |
testcase_01 | AC | 37 ms
53,648 KB |
testcase_02 | AC | 38 ms
53,996 KB |
testcase_03 | AC | 414 ms
84,244 KB |
testcase_04 | AC | 753 ms
93,556 KB |
testcase_05 | AC | 466 ms
86,072 KB |
testcase_06 | AC | 764 ms
95,076 KB |
testcase_07 | AC | 907 ms
98,564 KB |
testcase_08 | AC | 757 ms
94,324 KB |
testcase_09 | AC | 662 ms
91,160 KB |
testcase_10 | AC | 662 ms
90,368 KB |
testcase_11 | AC | 507 ms
86,948 KB |
testcase_12 | AC | 599 ms
88,488 KB |
testcase_13 | AC | 671 ms
89,132 KB |
testcase_14 | AC | 483 ms
85,464 KB |
testcase_15 | AC | 920 ms
98,640 KB |
testcase_16 | AC | 656 ms
89,044 KB |
testcase_17 | AC | 927 ms
98,312 KB |
testcase_18 | AC | 275 ms
97,004 KB |
testcase_19 | AC | 274 ms
94,160 KB |
testcase_20 | AC | 278 ms
95,060 KB |
testcase_21 | AC | 155 ms
79,236 KB |
ソースコード
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) class UF_tree: def __init__(self, n): """ 0-indexed """ self.root = [-1] * n self.components = n def find(self, x): if self.root[x] < 0: return x self.root[x] = self.find(self.root[x]) return self.root[x] def same(self, x, y): return self.find(x) == self.find(y) def merge(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False if -self.root[x] > -self.root[y]: x, y = y, x self.root[y] += self.root[x] self.root[x] = y self.components -= 1 return True def size(self, x): return -self.root[self.find(x)] N, M, Q = map(int, input().split()) uf = UF_tree(7 * N) S = [sum(1 << i for i, s in enumerate(input().rstrip()) if s == "1") for _ in range(N)] for i, s in enumerate(S): i *= 7 for j in range(7): k = (j + 1) % 7 if (s >> j) & 1 and (s >> k) & 1: uf.merge(i + j, i + k) edge = [[] for _ in range(N)] for _ in range(M): a, b = map(int, input().split()) a -= 1 b -= 1 edge[a].append(b) edge[b].append(a) for i in range(7): if (S[a] >> i) & 1 and (S[b] >> i) & 1: uf.merge(7 * a + i, 7 * b + i) for _ in range(Q): f, x, y = map(int, input().split()) x -= 1 y -= 1 if f == 1: S[x] |= (1 << y) l = (y - 1) % 7 if (S[x] >> l) & 1: uf.merge(7 * x + y, 7 * x + l) r = (y + 1) % 7 if (S[x] >> r) & 1: uf.merge(7 * x + y, 7 * x + r) for z in edge[x]: if (S[z] >> y) & 1: uf.merge(7 * x + y, 7 * z + y) else: print(uf.size(7 * x))