結果
問題 |
No.1023 Cyclic Tour
|
ユーザー |
![]() |
提出日時 | 2020-04-07 18:29:55 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,317 ms / 2,000 ms |
コード長 | 949 bytes |
コンパイル時間 | 150 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 51,200 KB |
最終ジャッジ日時 | 2024-07-07 20:38:26 |
合計ジャッジ時間 | 53,491 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 49 |
ソースコード
from collections import deque n, m=map(int, input().split()) par=[i for i in range(n)] sz=[1]*n def find(x): if x==par[x]: return x par[x]=find(par[x]) return par[x] def unite(x, y): x=find(x) y=find(y) if x==y: return False if sz[x]>sz[y]: x, y=y, x sz[y]+=sz[x] par[x]=y return True e=[] for _ in range(m): a, b, c=map(int, input().split()) a-=1 b-=1 if c==1: if not unite(a, b): print("Yes") exit() else: e.append((a, b)) g=[[] for i in range(n)] deg=[0]*n for p in e: x=find(p[0]) y=find(p[1]) g[x].append(y) deg[y]+=1 que=deque() for x in range(n): if deg[x]==0: que.appendleft(x) while len(que)!=0: x=que.popleft() for y in g[x]: deg[y]-=1 if deg[y]==0: que.appendleft(y) for x in range(n): if deg[x]>0: print("Yes") exit() print("No")