結果

問題 No.1640 簡単な色塗り
ユーザー titiatitia
提出日時 2021-08-06 23:20:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,682 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 97,528 KB
最終ジャッジ日時 2023-09-12 03:14:34
合計ジャッジ時間 16,692 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,112 KB
testcase_01 AC 67 ms
71,312 KB
testcase_02 AC 68 ms
71,356 KB
testcase_03 AC 68 ms
71,608 KB
testcase_04 AC 150 ms
97,456 KB
testcase_05 AC 146 ms
97,528 KB
testcase_06 AC 68 ms
71,260 KB
testcase_07 AC 67 ms
71,556 KB
testcase_08 AC 67 ms
71,600 KB
testcase_09 AC 67 ms
71,248 KB
testcase_10 AC 167 ms
89,992 KB
testcase_11 AC 151 ms
87,516 KB
testcase_12 AC 151 ms
86,372 KB
testcase_13 AC 207 ms
97,100 KB
testcase_14 AC 214 ms
96,596 KB
testcase_15 AC 135 ms
83,736 KB
testcase_16 AC 137 ms
85,360 KB
testcase_17 AC 203 ms
93,036 KB
testcase_18 AC 111 ms
79,052 KB
testcase_19 AC 155 ms
86,360 KB
testcase_20 AC 178 ms
89,540 KB
testcase_21 AC 161 ms
86,904 KB
testcase_22 AC 110 ms
78,872 KB
testcase_23 AC 180 ms
90,660 KB
testcase_24 AC 122 ms
81,044 KB
testcase_25 AC 160 ms
86,288 KB
testcase_26 AC 186 ms
91,856 KB
testcase_27 AC 133 ms
83,564 KB
testcase_28 AC 217 ms
95,660 KB
testcase_29 AC 199 ms
91,872 KB
testcase_30 AC 104 ms
79,956 KB
testcase_31 AC 217 ms
96,344 KB
testcase_32 AC 203 ms
94,140 KB
testcase_33 AC 154 ms
88,688 KB
testcase_34 AC 177 ms
90,332 KB
testcase_35 AC 152 ms
88,624 KB
testcase_36 AC 105 ms
80,152 KB
testcase_37 AC 107 ms
81,052 KB
testcase_38 AC 207 ms
94,956 KB
testcase_39 AC 130 ms
82,636 KB
testcase_40 AC 126 ms
82,776 KB
testcase_41 AC 172 ms
90,528 KB
testcase_42 AC 140 ms
86,376 KB
testcase_43 AC 142 ms
86,456 KB
testcase_44 AC 136 ms
86,164 KB
testcase_45 AC 124 ms
82,576 KB
testcase_46 AC 107 ms
80,296 KB
testcase_47 AC 102 ms
79,772 KB
testcase_48 AC 192 ms
95,148 KB
testcase_49 AC 91 ms
77,004 KB
testcase_50 AC 67 ms
71,428 KB
testcase_51 AC 67 ms
71,372 KB
testcase_52 AC 220 ms
97,512 KB
testcase_53 AC 227 ms
97,284 KB
07_evil_01.txt AC 437 ms
122,352 KB
07_evil_02.txt AC 637 ms
143,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

DEG=[0]*(N+1)

E=[[] for i in range(N+1)]
for i in range(N):
    a,b=map(int,input().split())
    DEG[a]+=1
    DEG[b]+=1

    E[a].append((b,i))
    E[b].append((a,i))



Q=[]
for i in range(N+1):
    if DEG[i]==1:
        Q.append(i)

ANS=[-1]*N
USE=[0]*(N+1)

USE[0]=1

while Q:
    x=Q.pop()
    USE[x]=1
    for to,ind in E[x]:
        if USE[to]==0:
            ANS[ind]=x
            DEG[to]-=1
            if DEG[to]==1:
                Q.append(to)


for i in range(N+1):
    if USE[i]==0:
        NOW=i
        while True:
            flag=0
            USE[NOW]=1
            
            for to,ind in E[NOW]:
                if USE[to]==0:
                    ANS[ind]=NOW
                    NOW=to
                    flag=1
                    break

            if flag==0:
                for to,ind in E[NOW]:
                    if ANS[ind]==-1:
                        ANS[ind]=NOW
                        break
                break

if -1 in ANS:
    print("No")
else:
    print("Yes")
    for ans in ANS:
        print(ans)
            
    


            
0