結果
問題 | No.977 アリス仕掛けの摩天楼 |
ユーザー | hiragn |
提出日時 | 2022-11-29 14:14:04 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 670 ms / 2,000 ms |
コード長 | 2,264 bytes |
コンパイル時間 | 273 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 40,704 KB |
最終ジャッジ日時 | 2024-10-07 01:29:17 |
合計ジャッジ時間 | 5,702 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 28 ms
10,752 KB |
testcase_01 | AC | 26 ms
10,880 KB |
testcase_02 | AC | 27 ms
10,880 KB |
testcase_03 | AC | 28 ms
10,752 KB |
testcase_04 | AC | 27 ms
10,880 KB |
testcase_05 | AC | 26 ms
11,008 KB |
testcase_06 | AC | 28 ms
10,752 KB |
testcase_07 | AC | 26 ms
10,880 KB |
testcase_08 | AC | 27 ms
10,880 KB |
testcase_09 | AC | 27 ms
11,008 KB |
testcase_10 | AC | 29 ms
11,008 KB |
testcase_11 | AC | 28 ms
11,008 KB |
testcase_12 | AC | 29 ms
10,880 KB |
testcase_13 | AC | 86 ms
13,824 KB |
testcase_14 | AC | 78 ms
13,824 KB |
testcase_15 | AC | 84 ms
13,696 KB |
testcase_16 | AC | 89 ms
13,824 KB |
testcase_17 | AC | 88 ms
13,952 KB |
testcase_18 | AC | 197 ms
19,840 KB |
testcase_19 | AC | 202 ms
19,712 KB |
testcase_20 | AC | 311 ms
25,344 KB |
testcase_21 | AC | 499 ms
33,408 KB |
testcase_22 | AC | 662 ms
39,680 KB |
testcase_23 | AC | 658 ms
40,576 KB |
testcase_24 | AC | 653 ms
40,704 KB |
testcase_25 | AC | 670 ms
40,576 KB |
ソースコード
class UnionFind: n = 1 parent_or_size = [-1 for i in range(n)] def __init__(self, N): self.n = N self.parent_or_size = [-1 for _ in range(N)] def merge(self, a, b): assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n) assert 0 <= b < self.n, "0<=b<n,b={0},n={1}".format(b, self.n) x = self.leader(a) y = self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: 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): assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n) assert 0 <= b < self.n, "0<=b<n,b={0},n={1}".format(b, self.n) return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n) if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [0 for _ in range(self.n)] group_size = [0 for _ in range(self.n)] for i in range(self.n): leader_buf[i] = self.leader(i) group_size[leader_buf[i]] += 1 result = [[] for _ in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2 = [] for i in range(self.n): if len(result[i]) > 0: result2.append(result[i]) return result2 def main(): n = int(input()) uf = UnionFind(n) adj = [[] for _ in range(n)] for _ in range(n - 1): a, b = map(int, input().split()) uf.merge(a, b) adj[a].append(b) adj[b].append(a) k = len(uf.groups()) if k == 1: exit(print("Bob")) if k > 2: exit(print("Alice")) if k == 2: if 1 in map(len, adj): print("Alice") else: print("Bob") if __name__ == "__main__": main()