結果

問題 No.1640 簡単な色塗り
ユーザー roarisroaris
提出日時 2021-08-06 22:37:28
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,989 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 128,052 KB
最終ジャッジ日時 2023-09-12 02:39:21
合計ジャッジ時間 25,087 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,796 KB
testcase_01 AC 94 ms
71,788 KB
testcase_02 AC 95 ms
71,664 KB
testcase_03 AC 96 ms
71,904 KB
testcase_04 AC 290 ms
128,052 KB
testcase_05 AC 255 ms
99,048 KB
testcase_06 AC 94 ms
71,796 KB
testcase_07 AC 95 ms
71,668 KB
testcase_08 AC 94 ms
71,792 KB
testcase_09 AC 96 ms
71,788 KB
testcase_10 AC 321 ms
91,972 KB
testcase_11 AC 300 ms
88,920 KB
testcase_12 AC 289 ms
87,932 KB
testcase_13 AC 412 ms
97,776 KB
testcase_14 AC 403 ms
97,872 KB
testcase_15 AC 270 ms
85,764 KB
testcase_16 AC 279 ms
86,736 KB
testcase_17 AC 374 ms
94,432 KB
testcase_18 AC 199 ms
80,668 KB
testcase_19 AC 279 ms
87,288 KB
testcase_20 AC 319 ms
90,840 KB
testcase_21 AC 293 ms
88,752 KB
testcase_22 AC 181 ms
80,192 KB
testcase_23 AC 338 ms
91,820 KB
testcase_24 AC 241 ms
82,892 KB
testcase_25 AC 309 ms
88,452 KB
testcase_26 AC 374 ms
93,704 KB
testcase_27 AC 270 ms
85,056 KB
testcase_28 AC 423 ms
97,500 KB
testcase_29 AC 370 ms
93,252 KB
testcase_30 AC 215 ms
81,972 KB
testcase_31 AC 369 ms
98,052 KB
testcase_32 AC 341 ms
95,400 KB
testcase_33 AC 286 ms
89,776 KB
testcase_34 AC 311 ms
92,392 KB
testcase_35 AC 277 ms
90,708 KB
testcase_36 AC 201 ms
81,432 KB
testcase_37 AC 214 ms
82,924 KB
testcase_38 AC 349 ms
96,332 KB
testcase_39 AC 248 ms
86,124 KB
testcase_40 AC 249 ms
86,864 KB
testcase_41 AC 320 ms
93,040 KB
testcase_42 AC 248 ms
87,176 KB
testcase_43 AC 256 ms
87,680 KB
testcase_44 AC 259 ms
87,804 KB
testcase_45 AC 236 ms
85,436 KB
testcase_46 AC 212 ms
81,948 KB
testcase_47 AC 207 ms
81,440 KB
testcase_48 AC 350 ms
96,108 KB
testcase_49 AC 175 ms
79,624 KB
testcase_50 AC 99 ms
71,612 KB
testcase_51 AC 100 ms
71,668 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt AC 830 ms
122,724 KB
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