結果

問題 No.1640 簡単な色塗り
ユーザー sotanishysotanishy
提出日時 2021-08-06 22:01:26
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 991 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 121,088 KB
最終ジャッジ日時 2024-06-29 15:08:24
合計ジャッジ時間 16,243 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39 RE * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N = int(input())
G = [set() for _ in range(N)]
deg = [0] * N
for i in range(N):
    A, B = map(lambda x: int(x) - 1, input().split())
    G[A].add((B, i))
    G[B].add((A, i))
    deg[A] += 1
    deg[B] += 1
if any(d == 0 for d in deg):
    print("No")
    exit()
st = []
for v in range(N):
    if deg[v] == 1:
        st.append(v)
ans = [0] * N
while st:
    v = st.pop()
    if deg[v] == 0:
        print("No")
        exit()
    u, i = next(iter(G[v]))
    deg[v] -= 1
    deg[u] -= 1
    G[v].remove((u, i))
    G[u].remove((v, i))
    ans[i] = v
    if deg[u] == 1:
        st.append(u)
for s in range(N):
    if deg[s] == 0:
        continue
    st.append(s)
    while st:
        v = st.pop()
        u, i = next(iter(G[v]))
        deg[v] -= 1
        deg[u] -= 1
        G[v].remove((u, i))
        G[u].remove((v, i))
        ans[i] = v
        if deg[u] == 1:
            st.append(u)
print("Yes")
print(*map(lambda x: x + 1, ans), sep='\n')
0