結果

問題 No.1640 簡単な色塗り
ユーザー rlangevinrlangevin
提出日時 2023-12-25 20:33:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,751 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 180,316 KB
最終ジャッジ日時 2023-12-25 20:33:59
合計ジャッジ時間 24,377 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,608 KB
testcase_01 AC 42 ms
55,608 KB
testcase_02 AC 40 ms
55,608 KB
testcase_03 AC 41 ms
55,608 KB
testcase_04 AC 244 ms
162,784 KB
testcase_05 WA -
testcase_06 AC 42 ms
55,608 KB
testcase_07 AC 41 ms
55,608 KB
testcase_08 WA -
testcase_09 AC 42 ms
55,608 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 152 ms
79,884 KB
testcase_31 AC 451 ms
141,448 KB
testcase_32 AC 379 ms
128,796 KB
testcase_33 AC 318 ms
122,764 KB
testcase_34 AC 360 ms
138,336 KB
testcase_35 AC 318 ms
125,644 KB
testcase_36 AC 150 ms
79,784 KB
testcase_37 AC 183 ms
86,828 KB
testcase_38 AC 408 ms
147,036 KB
testcase_39 AC 235 ms
92,164 KB
testcase_40 AC 232 ms
93,616 KB
testcase_41 AC 388 ms
136,560 KB
testcase_42 AC 273 ms
111,404 KB
testcase_43 AC 264 ms
110,964 KB
testcase_44 AC 292 ms
112,452 KB
testcase_45 AC 230 ms
95,688 KB
testcase_46 AC 172 ms
88,484 KB
testcase_47 AC 133 ms
79,248 KB
testcase_48 AC 434 ms
144,588 KB
testcase_49 AC 106 ms
77,356 KB
testcase_50 AC 41 ms
55,608 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


N = int(input())
A, B = [0] * N, [0] * N
G = [[] for i in range(N)]
U = UnionFind(N)
for i in range(N):
    A[i], B[i] = map(int, input().split())
    A[i], B[i] = A[i] - 1, B[i] - 1
    G[A[i]].append((B[i], i))
    G[B[i]].append((A[i], i))
    U.union(A[i], B[i])
    
Q = [deque() for _ in range(N)]
cnt = [0] * N
ans, seen = [-1] * N, [0] * N
for i in range(N):
    ui = U.find(A[i])
    cnt[ui] += 1
    if cnt[ui] == U.get_size(ui):
        Q[ui].append(A[i])
        ans[i] = A[i] + 1
        seen[A[i]] = 1
    elif cnt[ui] > U.get_size(i):
        print("No")
        exit()
        
for s in range(N):
    while Q[s]:
        u = Q[s].popleft()
        for v, ind in G[u]:
            if seen[v]:
                continue
            ans[ind] = v + 1
            Q[s].append(v)
            seen[v] = 1
            
print("Yes")
for a in ans:
    print(a)
0