結果
問題 | No.1023 Cyclic Tour |
ユーザー | rlangevin |
提出日時 | 2024-02-03 13:01:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 567 ms / 2,000 ms |
コード長 | 4,016 bytes |
コンパイル時間 | 561 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 132,048 KB |
最終ジャッジ日時 | 2024-09-28 10:51:12 |
合計ジャッジ時間 | 23,613 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
53,120 KB |
testcase_01 | AC | 43 ms
52,992 KB |
testcase_02 | AC | 43 ms
52,608 KB |
testcase_03 | AC | 43 ms
52,736 KB |
testcase_04 | AC | 284 ms
91,648 KB |
testcase_05 | AC | 295 ms
91,696 KB |
testcase_06 | AC | 288 ms
92,732 KB |
testcase_07 | AC | 299 ms
91,904 KB |
testcase_08 | AC | 254 ms
92,416 KB |
testcase_09 | AC | 457 ms
116,680 KB |
testcase_10 | AC | 439 ms
117,520 KB |
testcase_11 | AC | 480 ms
125,440 KB |
testcase_12 | AC | 406 ms
115,760 KB |
testcase_13 | AC | 420 ms
122,024 KB |
testcase_14 | AC | 426 ms
122,108 KB |
testcase_15 | AC | 427 ms
120,780 KB |
testcase_16 | AC | 542 ms
118,620 KB |
testcase_17 | AC | 553 ms
118,716 KB |
testcase_18 | AC | 547 ms
116,736 KB |
testcase_19 | AC | 518 ms
114,432 KB |
testcase_20 | AC | 469 ms
113,256 KB |
testcase_21 | AC | 484 ms
110,000 KB |
testcase_22 | AC | 520 ms
114,652 KB |
testcase_23 | AC | 530 ms
117,920 KB |
testcase_24 | AC | 548 ms
119,648 KB |
testcase_25 | AC | 500 ms
113,188 KB |
testcase_26 | AC | 501 ms
111,180 KB |
testcase_27 | AC | 436 ms
107,192 KB |
testcase_28 | AC | 567 ms
125,232 KB |
testcase_29 | AC | 562 ms
131,584 KB |
testcase_30 | AC | 533 ms
116,912 KB |
testcase_31 | AC | 533 ms
117,248 KB |
testcase_32 | AC | 518 ms
113,304 KB |
testcase_33 | AC | 545 ms
114,548 KB |
testcase_34 | AC | 152 ms
92,160 KB |
testcase_35 | AC | 266 ms
93,312 KB |
testcase_36 | AC | 489 ms
113,544 KB |
testcase_37 | AC | 557 ms
118,100 KB |
testcase_38 | AC | 556 ms
119,808 KB |
testcase_39 | AC | 562 ms
109,896 KB |
testcase_40 | AC | 564 ms
113,348 KB |
testcase_41 | AC | 561 ms
115,156 KB |
testcase_42 | AC | 351 ms
94,592 KB |
testcase_43 | AC | 237 ms
93,056 KB |
testcase_44 | AC | 353 ms
98,296 KB |
testcase_45 | AC | 405 ms
132,048 KB |
testcase_46 | AC | 231 ms
114,688 KB |
testcase_47 | AC | 204 ms
95,412 KB |
testcase_48 | AC | 284 ms
121,344 KB |
testcase_49 | AC | 284 ms
106,496 KB |
testcase_50 | AC | 260 ms
111,432 KB |
testcase_51 | AC | 175 ms
93,056 KB |
testcase_52 | AC | 197 ms
106,368 KB |
ソースコード
import sys input = sys.stdin.readline 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 self.seen = [0] * N 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: if self.used1[u]: continue self.used1[u] = 1 for v in self.G[u]: if self.used1[v]: continue stack.append(~v) stack.append(v) else: u = ~u if self.seen[u]: continue self.seen[u]= 1 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")