結果
問題 | No.1023 Cyclic Tour |
ユーザー | chineristAC |
提出日時 | 2020-10-08 17:01:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 986 ms / 2,000 ms |
コード長 | 2,273 bytes |
コンパイル時間 | 209 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 113,568 KB |
最終ジャッジ日時 | 2024-07-20 05:58:51 |
合計ジャッジ時間 | 27,205 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
54,528 KB |
testcase_01 | AC | 45 ms
54,144 KB |
testcase_02 | AC | 45 ms
54,144 KB |
testcase_03 | AC | 48 ms
54,144 KB |
testcase_04 | AC | 502 ms
94,900 KB |
testcase_05 | AC | 456 ms
94,488 KB |
testcase_06 | AC | 464 ms
95,860 KB |
testcase_07 | AC | 534 ms
96,880 KB |
testcase_08 | AC | 434 ms
103,720 KB |
testcase_09 | AC | 403 ms
102,784 KB |
testcase_10 | AC | 426 ms
101,872 KB |
testcase_11 | AC | 417 ms
104,008 KB |
testcase_12 | AC | 316 ms
105,344 KB |
testcase_13 | AC | 307 ms
104,576 KB |
testcase_14 | AC | 316 ms
103,168 KB |
testcase_15 | AC | 315 ms
104,340 KB |
testcase_16 | AC | 384 ms
103,936 KB |
testcase_17 | AC | 407 ms
104,448 KB |
testcase_18 | AC | 416 ms
102,400 KB |
testcase_19 | AC | 377 ms
100,352 KB |
testcase_20 | AC | 826 ms
111,584 KB |
testcase_21 | AC | 856 ms
112,680 KB |
testcase_22 | AC | 579 ms
106,880 KB |
testcase_23 | AC | 584 ms
108,032 KB |
testcase_24 | AC | 488 ms
106,864 KB |
testcase_25 | AC | 986 ms
111,700 KB |
testcase_26 | AC | 811 ms
111,140 KB |
testcase_27 | AC | 793 ms
110,680 KB |
testcase_28 | AC | 529 ms
107,740 KB |
testcase_29 | AC | 454 ms
107,904 KB |
testcase_30 | AC | 488 ms
107,844 KB |
testcase_31 | AC | 519 ms
107,776 KB |
testcase_32 | AC | 490 ms
105,088 KB |
testcase_33 | AC | 526 ms
107,008 KB |
testcase_34 | AC | 292 ms
92,160 KB |
testcase_35 | AC | 713 ms
100,904 KB |
testcase_36 | AC | 740 ms
110,164 KB |
testcase_37 | AC | 617 ms
107,140 KB |
testcase_38 | AC | 449 ms
105,976 KB |
testcase_39 | AC | 637 ms
108,264 KB |
testcase_40 | AC | 659 ms
108,652 KB |
testcase_41 | AC | 621 ms
107,760 KB |
testcase_42 | AC | 883 ms
113,568 KB |
testcase_43 | AC | 486 ms
107,316 KB |
testcase_44 | AC | 508 ms
103,900 KB |
testcase_45 | AC | 295 ms
109,312 KB |
testcase_46 | AC | 238 ms
103,168 KB |
testcase_47 | AC | 224 ms
99,472 KB |
testcase_48 | AC | 339 ms
106,256 KB |
testcase_49 | AC | 346 ms
105,132 KB |
testcase_50 | AC | 326 ms
107,828 KB |
testcase_51 | AC | 341 ms
106,744 KB |
testcase_52 | AC | 333 ms
110,076 KB |
ソースコード
class UnionFindVerSize(): def __init__(self, N): self._parent = [n for n in range(0, N)] self._size = [1] * N self.edge = [[] for i in range(N)] self.deg = [0 for i in range(N)] def find_root(self, x): if self._parent[x] == x: return x self._parent[x] = self.find_root(self._parent[x]) return self._parent[x] def unite(self, x, y): gx = self.find_root(x) gy = self.find_root(y) if gx == gy: return if self._size[gx] < self._size[gy]: self._parent[gx] = gy self._size[gy] += self._size[gx] self.edge[gy] += self.edge[gx] self.edge[gx] = [] else: self._parent[gy] = gx self._size[gx] += self._size[gy] self.edge[gx] += self.edge[gy] self.edge[gy] = [] def add_edge(self,x,y): gx = self.find_root(x) gy = self.find_root(y) self.edge[gx].append(gy) def get_size(self, x): return self._size[self.find_root(x)] def is_same_group(self, x, y): return self.find_root(x) == self.find_root(y) def calc_group_num(self): N = len(self._parent) ans = 0 for i in range(N): if self.find_root(i) == i: ans += 1 return ans def rebuild(self): for i in range(N): if self.edge[i]: self.edge[i] = [self.find_root(v) for v in self.edge[i]] for v in self.edge[i]: self.deg[v] += 1 import sys from collections import deque input = sys.stdin.readline N,M = map(int,input().split()) uf = UnionFindVerSize(N) uf_un = UnionFindVerSize(N) for i in range(M): a,b,c = map(int,input().split()) if c==1: uf.unite(a-1,b-1) if uf_un.is_same_group(a-1,b-1): print("Yes") exit() uf_un.unite(a-1,b-1) else: uf.add_edge(a-1,b-1) uf.rebuild() ans = [v for v in range(N) if uf.deg[v]==0] deq = deque(ans) used = [0]*N while deq: v = deq.popleft() for nv in uf.edge[v]: uf.deg[nv] -= 1 if uf.deg[nv] == 0: deq.append(nv) ans.append(nv) if len(ans)==N: print("No") else: print("Yes")