結果

問題 No.1640 簡単な色塗り
ユーザー roarisroaris
提出日時 2021-08-06 22:29:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,986 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 124,380 KB
最終ジャッジ日時 2023-09-12 02:29:17
合計ジャッジ時間 24,870 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,816 KB
testcase_01 AC 96 ms
71,616 KB
testcase_02 AC 95 ms
71,668 KB
testcase_03 AC 99 ms
71,812 KB
testcase_04 AC 315 ms
124,380 KB
testcase_05 WA -
testcase_06 AC 95 ms
71,620 KB
testcase_07 AC 96 ms
71,908 KB
testcase_08 WA -
testcase_09 AC 94 ms
71,748 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 206 ms
81,884 KB
testcase_31 AC 351 ms
97,976 KB
testcase_32 AC 329 ms
95,364 KB
testcase_33 AC 272 ms
89,368 KB
testcase_34 AC 291 ms
92,288 KB
testcase_35 AC 264 ms
90,508 KB
testcase_36 AC 195 ms
81,436 KB
testcase_37 AC 202 ms
83,292 KB
testcase_38 AC 327 ms
96,560 KB
testcase_39 AC 236 ms
86,180 KB
testcase_40 AC 237 ms
86,680 KB
testcase_41 AC 294 ms
92,988 KB
testcase_42 AC 239 ms
87,048 KB
testcase_43 AC 245 ms
87,652 KB
testcase_44 AC 251 ms
87,624 KB
testcase_45 AC 228 ms
85,480 KB
testcase_46 AC 201 ms
81,904 KB
testcase_47 AC 195 ms
81,332 KB
testcase_48 AC 336 ms
96,280 KB
testcase_49 AC 165 ms
79,528 KB
testcase_50 AC 96 ms
71,588 KB
testcase_51 AC 97 ms
71,792 KB
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:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

def bfs(s):
    dist[s] = 0
    q = deque([s])
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist
    
N = int(input())
AB = []
uf = Unionfind(N)
ex_edges = set()
G = [[] for _ in range(N)]

for _ in range(N):
    A, B = map(int, input().split())
    AB.append((A-1, B-1))
    
    if uf.is_same(A-1, B-1):
        ex_edges.add((A-1, B-1))
    else:
        uf.unite(A-1, B-1)
    
    G[A-1].append(B-1)
    G[B-1].append(A-1)
    
ex_cnt = defaultdict(int)

for A, _ in ex_edges:
    ex_cnt[uf.root(A)] += 1

rs = set(uf.root(i) for i in range(N))

for r in rs:
    if ex_cnt[r]==0:
        exit(print('No'))
        
dist = [-1]*N

for A, _ in ex_edges:
    bfs(A)

print('Yes')

for A, B in AB:
    if (A, B) in ex_edges:
        print(A+1)
    else:
        if dist[A]>dist[B]:
            print(A+1)
        else:
            print(B+1)
0