結果
問題 | No.1719 Tree and Permutation |
ユーザー | zkou |
提出日時 | 2021-09-23 17:17:52 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,986 bytes |
コンパイル時間 | 169 ms |
コンパイル使用メモリ | 81,820 KB |
実行使用メモリ | 108,320 KB |
最終ジャッジ日時 | 2023-10-24 11:29:12 |
合計ジャッジ時間 | 3,424 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
ソースコード
import sys input = sys.stdin.buffer.readline read = sys.stdin.buffer.read class dsu: """Data structures and algorithms for disjoint set union problems. Given an undirected graph, it processes the following queries in O(alpha(n)) time (amortized). > Edge addition > Deciding whether given two vertices are in the same connected component Each connected component internally has a representative vertex. When two connected components are merged by edge addition, one of the two representatives of these connected components becomes the representative of the new connected component. """ __slots__ = ["n", "parent_or_size"] def __init__(self, n): """It creates an undirected graph with n vertices and 0 edges. Constraints ----------- > 0 <= n <= 10 ** 8 Complexity ---------- > O(n) """ self.n = n self.parent_or_size = [-1] * n def merge(self, a, b): """It adds an edge (a, b). If the vertices a and b were in the same connected component, it returns the representative of this connected component. Otherwise, it returns the representative of the new connected component. Constraints ----------- > 0 <= a < n > 0 <= b < n Complexity ---------- > O(alpha(n)) amortized """ # assert 0 <= a < self.n # assert 0 <= b < self.n x = self.leader(a) y = self.leader(b) if x == y: return x if self.parent_or_size[y] < self.parent_or_size[x]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a, b): """It returns whether the vertices a and b are in the same connected component. Constraints ----------- > 0 <= a < n > 0 <= b < n Complexity ---------- > O(alpha(n)) amortized """ # assert 0 <= a < self.n # assert 0 <= b < self.n return self.leader(a) == self.leader(b) def leader(self, a): """It returns the representative of the connected component that contains the vertex a. Constraints ----------- > 0 <= a < n Complexity ---------- > O(alpha(n)) amortized """ # assert 0 <= a < self.n path = [] while self.parent_or_size[a] >= 0: path.append(a) a = self.parent_or_size[a] for child in path: self.parent_or_size[child] = a return a def size(self, a): """It returns the size of the connected component that contains the vertex a. Constraints ----------- > 0 <= a < n Complexity ---------- > O(alpha(n)) amortized """ # assert 0 <= a < self.n return -self.parent_or_size[self.leader(a)] def groups(self): """It divides the graph into connected components and returns the list of them. More precisely, it returns the list of the "list of the vertices in a connected component". Both of the orders of the connected components and the vertices are undefined. Complexity ---------- > O(n) """ result = [[] for _ in range(self.n)] for i in range(self.n): result[self.leader(i)].append(i) return [g for g in result if g] T = int(input()) assert 1 <= T <= 10 ** 5 N_sum = 0 for _ in range(T): N = int(input()) N_sum += N assert 3 <= N <= 10 ** 5 uf = dsu(N) for _ in range(N - 1): u, v = map(int, input().split()) u -= 1; v -= 1 assert u != v assert 0 <= u < N assert 0 <= v < N uf.merge(u, v) assert len(uf.groups()) == 1 assert N_sum <= 5 * 10 ** 5