結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,576 KB
testcase_01 AC 92 ms
71,556 KB
testcase_02 AC 93 ms
71,424 KB
testcase_03 AC 91 ms
71,948 KB
testcase_04 AC 288 ms
127,728 KB
testcase_05 AC 245 ms
98,928 KB
testcase_06 AC 96 ms
71,720 KB
testcase_07 AC 96 ms
71,396 KB
testcase_08 AC 92 ms
71,624 KB
testcase_09 AC 92 ms
71,748 KB
testcase_10 AC 325 ms
91,708 KB
testcase_11 AC 299 ms
88,972 KB
testcase_12 AC 275 ms
87,764 KB
testcase_13 AC 406 ms
97,908 KB
testcase_14 AC 388 ms
97,740 KB
testcase_15 AC 264 ms
85,560 KB
testcase_16 AC 271 ms
86,732 KB
testcase_17 AC 364 ms
94,132 KB
testcase_18 AC 195 ms
80,892 KB
testcase_19 AC 275 ms
87,360 KB
testcase_20 AC 322 ms
91,000 KB
testcase_21 AC 289 ms
88,684 KB
testcase_22 AC 181 ms
80,448 KB
testcase_23 AC 330 ms
91,740 KB
testcase_24 AC 228 ms
82,864 KB
testcase_25 AC 287 ms
88,516 KB
testcase_26 AC 352 ms
93,560 KB
testcase_27 AC 252 ms
85,016 KB
testcase_28 AC 385 ms
97,288 KB
testcase_29 AC 338 ms
93,332 KB
testcase_30 AC 197 ms
81,904 KB
testcase_31 AC 333 ms
97,968 KB
testcase_32 AC 319 ms
95,444 KB
testcase_33 AC 265 ms
89,676 KB
testcase_34 AC 289 ms
92,408 KB
testcase_35 AC 260 ms
90,424 KB
testcase_36 AC 194 ms
81,356 KB
testcase_37 AC 200 ms
83,152 KB
testcase_38 AC 327 ms
96,612 KB
testcase_39 AC 227 ms
86,248 KB
testcase_40 AC 227 ms
86,612 KB
testcase_41 AC 292 ms
93,120 KB
testcase_42 AC 236 ms
87,148 KB
testcase_43 AC 241 ms
87,712 KB
testcase_44 AC 235 ms
87,748 KB
testcase_45 AC 218 ms
85,568 KB
testcase_46 AC 196 ms
81,892 KB
testcase_47 AC 189 ms
81,320 KB
testcase_48 AC 321 ms
96,028 KB
testcase_49 AC 160 ms
79,700 KB
testcase_50 AC 93 ms
71,400 KB
testcase_51 AC 95 ms
71,764 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt AC 759 ms
122,496 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