結果
問題 | No.1023 Cyclic Tour |
ユーザー | H3PO4 |
提出日時 | 2021-09-04 09:16:53 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 981 ms / 2,000 ms |
コード長 | 2,082 bytes |
コンパイル時間 | 280 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 62,700 KB |
最終ジャッジ日時 | 2024-05-09 21:36:58 |
合計ジャッジ時間 | 37,350 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 29 ms
11,136 KB |
testcase_01 | AC | 31 ms
11,008 KB |
testcase_02 | AC | 29 ms
11,136 KB |
testcase_03 | AC | 30 ms
10,880 KB |
testcase_04 | AC | 317 ms
16,128 KB |
testcase_05 | AC | 322 ms
16,128 KB |
testcase_06 | AC | 345 ms
16,512 KB |
testcase_07 | AC | 331 ms
16,384 KB |
testcase_08 | AC | 515 ms
40,476 KB |
testcase_09 | AC | 496 ms
39,540 KB |
testcase_10 | AC | 513 ms
39,420 KB |
testcase_11 | AC | 540 ms
40,792 KB |
testcase_12 | AC | 585 ms
51,180 KB |
testcase_13 | AC | 549 ms
49,060 KB |
testcase_14 | AC | 542 ms
48,856 KB |
testcase_15 | AC | 567 ms
49,752 KB |
testcase_16 | AC | 852 ms
62,412 KB |
testcase_17 | AC | 901 ms
62,700 KB |
testcase_18 | AC | 869 ms
59,408 KB |
testcase_19 | AC | 841 ms
60,624 KB |
testcase_20 | AC | 704 ms
28,288 KB |
testcase_21 | AC | 699 ms
28,032 KB |
testcase_22 | AC | 813 ms
37,992 KB |
testcase_23 | AC | 840 ms
40,824 KB |
testcase_24 | AC | 981 ms
54,684 KB |
testcase_25 | AC | 703 ms
28,416 KB |
testcase_26 | AC | 698 ms
29,184 KB |
testcase_27 | AC | 654 ms
28,544 KB |
testcase_28 | AC | 915 ms
53,232 KB |
testcase_29 | AC | 956 ms
57,388 KB |
testcase_30 | AC | 956 ms
51,920 KB |
testcase_31 | AC | 847 ms
47,504 KB |
testcase_32 | AC | 857 ms
51,148 KB |
testcase_33 | AC | 860 ms
49,580 KB |
testcase_34 | AC | 191 ms
21,760 KB |
testcase_35 | AC | 438 ms
26,368 KB |
testcase_36 | AC | 690 ms
32,344 KB |
testcase_37 | AC | 870 ms
44,284 KB |
testcase_38 | AC | 878 ms
53,920 KB |
testcase_39 | AC | 838 ms
42,000 KB |
testcase_40 | AC | 864 ms
44,972 KB |
testcase_41 | AC | 861 ms
43,412 KB |
testcase_42 | AC | 697 ms
28,800 KB |
testcase_43 | AC | 895 ms
54,268 KB |
testcase_44 | AC | 340 ms
16,512 KB |
testcase_45 | AC | 555 ms
52,144 KB |
testcase_46 | AC | 479 ms
51,752 KB |
testcase_47 | AC | 318 ms
16,512 KB |
testcase_48 | AC | 651 ms
30,976 KB |
testcase_49 | AC | 674 ms
30,720 KB |
testcase_50 | AC | 675 ms
30,720 KB |
testcase_51 | AC | 673 ms
31,104 KB |
testcase_52 | AC | 663 ms
27,392 KB |
ソースコード
import sys from collections import deque input = sys.stdin.buffer.readline class UnionFind: def __init__(self, n): self.n = n self.parent = [i for i in range(n)] self.height = [1] * n self.size = [1] * n def find(self, x): if self.parent[x] == x: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.height[x] < self.height[y]: self.parent[x] = y self.size[y] += self.size[x] else: self.parent[y] = x self.size[x] += self.size[y] if self.height[x] == self.height[y]: self.height[x] += 1 def issame(self, x, y): return self.find(x) == self.find(y) def group_size(self, x): return self.size[self.find(x)] def group_members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parent) if i == x] def group_count(self): return len(self.roots()) N, M = map(int, input().split()) directed = [] uf = UnionFind(N) for _ in range(M): a, b, c = map(int, input().split()) a -= 1 b -= 1 if c == 1: if uf.issame(a, b): # 無向辺のみで閉路アリ print('Yes') exit() uf.unite(a, b) else: # assert c == 2 directed.append((a, b)) new_nodes_dict = {x: i for i, x in enumerate(uf.roots())} L = len(new_nodes_dict) G = [[] for _ in range(L)] deg = [0] * L for a, b in directed: an = new_nodes_dict[uf.find(a)] bn = new_nodes_dict[uf.find(b)] deg[bn] += 1 G[an].append(bn) d = deque([i for i, x in enumerate(deg) if not x]) while d: v = d.popleft() for x in G[v]: deg[x] -= 1 if not deg[x]: d.append(x) print('Yes' if max(deg) else 'No')