結果
問題 | No.1023 Cyclic Tour |
ユーザー | rlangevin |
提出日時 | 2024-02-03 12:21:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 772 ms / 2,000 ms |
コード長 | 3,874 bytes |
コンパイル時間 | 433 ms |
コンパイル使用メモリ | 82,068 KB |
実行使用メモリ | 245,504 KB |
最終ジャッジ日時 | 2024-09-28 10:50:12 |
合計ジャッジ時間 | 22,112 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,736 KB |
testcase_01 | AC | 36 ms
52,992 KB |
testcase_02 | AC | 37 ms
52,608 KB |
testcase_03 | AC | 36 ms
52,992 KB |
testcase_04 | AC | 251 ms
90,880 KB |
testcase_05 | AC | 251 ms
91,264 KB |
testcase_06 | AC | 242 ms
92,416 KB |
testcase_07 | AC | 260 ms
91,776 KB |
testcase_08 | AC | 206 ms
92,368 KB |
testcase_09 | AC | 405 ms
109,952 KB |
testcase_10 | AC | 407 ms
114,188 KB |
testcase_11 | AC | 445 ms
117,632 KB |
testcase_12 | AC | 420 ms
122,588 KB |
testcase_13 | AC | 394 ms
115,584 KB |
testcase_14 | AC | 379 ms
115,912 KB |
testcase_15 | AC | 399 ms
113,060 KB |
testcase_16 | AC | 772 ms
188,032 KB |
testcase_17 | AC | 749 ms
188,684 KB |
testcase_18 | AC | 596 ms
141,440 KB |
testcase_19 | AC | 574 ms
144,256 KB |
testcase_20 | AC | 375 ms
101,996 KB |
testcase_21 | AC | 398 ms
110,208 KB |
testcase_22 | AC | 461 ms
119,040 KB |
testcase_23 | AC | 472 ms
116,480 KB |
testcase_24 | AC | 512 ms
126,976 KB |
testcase_25 | AC | 408 ms
112,568 KB |
testcase_26 | AC | 403 ms
110,584 KB |
testcase_27 | AC | 367 ms
104,576 KB |
testcase_28 | AC | 474 ms
114,784 KB |
testcase_29 | AC | 507 ms
126,848 KB |
testcase_30 | AC | 505 ms
129,664 KB |
testcase_31 | AC | 460 ms
115,812 KB |
testcase_32 | AC | 510 ms
143,872 KB |
testcase_33 | AC | 566 ms
145,920 KB |
testcase_34 | AC | 134 ms
91,520 KB |
testcase_35 | AC | 234 ms
92,800 KB |
testcase_36 | AC | 426 ms
103,936 KB |
testcase_37 | AC | 519 ms
132,352 KB |
testcase_38 | AC | 483 ms
135,680 KB |
testcase_39 | AC | 522 ms
121,600 KB |
testcase_40 | AC | 537 ms
121,600 KB |
testcase_41 | AC | 478 ms
108,160 KB |
testcase_42 | AC | 317 ms
94,848 KB |
testcase_43 | AC | 201 ms
92,632 KB |
testcase_44 | AC | 297 ms
98,464 KB |
testcase_45 | AC | 549 ms
245,504 KB |
testcase_46 | AC | 406 ms
214,524 KB |
testcase_47 | AC | 149 ms
96,512 KB |
testcase_48 | AC | 222 ms
112,320 KB |
testcase_49 | AC | 228 ms
112,384 KB |
testcase_50 | AC | 214 ms
112,412 KB |
testcase_51 | AC | 150 ms
92,416 KB |
testcase_52 | AC | 165 ms
102,876 KB |
ソースコード
import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) class DirectedGraph(): def __init__(self, N): self.N = N self.G = [[] for i in range(N)] self.rG = [[] for i in range(N)] self.order = [] self.used1 = [0] * N self.used2 = [0] * N self.group = [-1] * N self.label = 0 def add_edge(self, u, v): self.G[u].append(v) self.rG[v].append(u) def dfs(self, s): self.used1[s] = 1 for u in self.G[s]: if self.used1[u]: continue self.dfs(u) self.order.append(s) # def rdfs(self, s, num): # self.group[s] = num # self.used2[s] = 1 # for u in self.rG[s]: # if self.used2[u]: # continue # self.rdfs(u, num) # def dfs(self, s): # stack = [~s, s] # while stack: # u = stack.pop() # if u >= 0: # self.used1[u] = 1 # for v in self.G[u]: # if self.used1[v]: # continue # stack.append(~v) # stack.append(v) # else: # u = ~u # self.order.append(u) def rdfs(self, s, num): stack = [s] while stack: u = stack.pop() if u >= 0: self.used2[u] = 1 self.group[u] = num for v in self.rG[u]: if self.used2[v]: continue stack.append(v) def scc(self): for i in range(self.N): if self.used1[i]: continue self.dfs(i) for s in reversed(self.order): if self.used2[s]: continue self.rdfs(s, self.label) self.label += 1 return self.label, self.group def construct(self): nG = [set() for _ in range(self.label)] mem = [[] for i in range(self.label)] for s in range(self.N): now = self.group[s] for u in self.G[s]: if now == self.group[u]: continue nG[now].add(self.group[u]) mem[now].append(s) return nG, mem class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N, M = map(int, input().split()) G = DirectedGraph(N) A, B, C = [-1] * M, [-1] * M, [-1] * M U = UnionFind(N) for i in range(M): A[i], B[i], C[i] = map(int, input().split()) A[i], B[i] = A[i] - 1, B[i] - 1 if C[i] == 1: if U.is_same(A[i], B[i]): print("Yes") exit() U.union(A[i], B[i]) for i in range(M): if C[i] == 1: continue a, b = U.find(A[i]), U.find(B[i]) if a == b: print("Yes") exit() G.add_edge(a, b) G.scc() nG, mem = G.construct() for m in mem: if len(m) >= 2: print("Yes") exit() print("No")