結果

問題 No.1640 簡単な色塗り
ユーザー titiatitia
提出日時 2021-08-06 22:09:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 678 ms / 2,000 ms
コード長 1,682 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 50,816 KB
最終ジャッジ日時 2023-09-12 02:10:33
合計ジャッジ時間 26,780 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,112 KB
testcase_01 AC 17 ms
8,052 KB
testcase_02 AC 17 ms
8,172 KB
testcase_03 AC 17 ms
8,164 KB
testcase_04 AC 421 ms
50,816 KB
testcase_05 AC 398 ms
47,676 KB
testcase_06 AC 17 ms
8,056 KB
testcase_07 AC 17 ms
8,052 KB
testcase_08 AC 17 ms
8,032 KB
testcase_09 AC 17 ms
8,176 KB
testcase_10 AC 413 ms
32,300 KB
testcase_11 AC 329 ms
27,856 KB
testcase_12 AC 289 ms
25,504 KB
testcase_13 AC 613 ms
44,616 KB
testcase_14 AC 612 ms
44,396 KB
testcase_15 AC 219 ms
20,956 KB
testcase_16 AC 256 ms
23,508 KB
testcase_17 AC 514 ms
38,420 KB
testcase_18 AC 50 ms
11,336 KB
testcase_19 AC 281 ms
25,612 KB
testcase_20 AC 387 ms
31,376 KB
testcase_21 AC 317 ms
27,228 KB
testcase_22 AC 38 ms
10,348 KB
testcase_23 AC 416 ms
33,352 KB
testcase_24 AC 110 ms
15,344 KB
testcase_25 AC 291 ms
25,752 KB
testcase_26 AC 462 ms
35,808 KB
testcase_27 AC 198 ms
20,284 KB
testcase_28 AC 584 ms
42,912 KB
testcase_29 AC 454 ms
35,564 KB
testcase_30 AC 79 ms
13,972 KB
testcase_31 AC 579 ms
46,540 KB
testcase_32 AC 477 ms
41,620 KB
testcase_33 AC 324 ms
31,108 KB
testcase_34 AC 421 ms
36,608 KB
testcase_35 AC 329 ms
31,020 KB
testcase_36 AC 79 ms
14,024 KB
testcase_37 AC 108 ms
16,204 KB
testcase_38 AC 507 ms
42,972 KB
testcase_39 AC 223 ms
23,164 KB
testcase_40 AC 217 ms
23,676 KB
testcase_41 AC 419 ms
37,396 KB
testcase_42 AC 252 ms
25,840 KB
testcase_43 AC 272 ms
26,396 KB
testcase_44 AC 256 ms
25,972 KB
testcase_45 AC 192 ms
21,764 KB
testcase_46 AC 85 ms
14,528 KB
testcase_47 AC 58 ms
12,176 KB
testcase_48 AC 504 ms
43,828 KB
testcase_49 AC 34 ms
10,188 KB
testcase_50 AC 17 ms
8,052 KB
testcase_51 AC 16 ms
8,020 KB
testcase_52 AC 672 ms
47,592 KB
testcase_53 AC 678 ms
47,708 KB
07_evil_01.txt AC 1,416 ms
89,708 KB
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

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