結果
問題 |
No.1640 簡単な色塗り
|
ユーザー |
![]() |
提出日時 | 2023-05-17 01:43:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 261 ms / 2,000 ms |
コード長 | 978 bytes |
コンパイル時間 | 276 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 98,452 KB |
最終ジャッジ日時 | 2024-12-14 22:51:48 |
合計ジャッジ時間 | 19,605 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
import sys readline=sys.stdin.readline N=int(readline()) used=[False]*N cnt=[0]*N graph=[[] for x in range(N)] for i in range(N): a,b=map(int,readline().split()) a-=1;b-=1 graph[a].append((b,i)) graph[b].append((a,i)) cnt[a]+=1 cnt[b]+=1 ans_lst=[None]*N if 0 in cnt: print("No") exit() stack=[x for x in range(N) if cnt[x]==1] while stack: x=stack.pop() for y,i in graph[x]: if not used[i]: cnt[x]-=1 ans_lst[i]=x+1 used[i]=True cnt[y]-=1 if cnt[y]==1: stack.append(y) break else: print("No") exit() for x in range(N): if cnt[x]==0: continue while cnt[x]: for y,i in graph[x]: if not used[i]: cnt[x]-=1 cnt[y]-=1 ans_lst[i]=x+1 used[i]=True x=y break print("Yes") print(*ans_lst,sep="\n")