結果

問題 No.1640 簡単な色塗り
ユーザー mkawa2mkawa2
提出日時 2021-08-07 00:18:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 343 ms / 2,000 ms
コード長 2,529 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 82,352 KB
実行使用メモリ 111,728 KB
最終ジャッジ日時 2024-06-29 16:38:57
合計ジャッジ時間 18,693 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,012 KB
testcase_01 AC 36 ms
53,456 KB
testcase_02 AC 33 ms
53,628 KB
testcase_03 AC 35 ms
53,676 KB
testcase_04 AC 170 ms
111,728 KB
testcase_05 AC 163 ms
101,076 KB
testcase_06 AC 33 ms
53,732 KB
testcase_07 AC 35 ms
53,568 KB
testcase_08 AC 33 ms
53,616 KB
testcase_09 AC 33 ms
53,156 KB
testcase_10 AC 247 ms
92,464 KB
testcase_11 AC 235 ms
89,664 KB
testcase_12 AC 220 ms
87,676 KB
testcase_13 AC 329 ms
99,804 KB
testcase_14 AC 325 ms
100,140 KB
testcase_15 AC 199 ms
85,236 KB
testcase_16 AC 211 ms
86,264 KB
testcase_17 AC 283 ms
96,448 KB
testcase_18 AC 122 ms
78,536 KB
testcase_19 AC 216 ms
87,624 KB
testcase_20 AC 251 ms
91,900 KB
testcase_21 AC 228 ms
89,388 KB
testcase_22 AC 113 ms
77,984 KB
testcase_23 AC 262 ms
92,148 KB
testcase_24 AC 168 ms
81,520 KB
testcase_25 AC 235 ms
87,720 KB
testcase_26 AC 299 ms
93,580 KB
testcase_27 AC 199 ms
85,300 KB
testcase_28 AC 318 ms
98,328 KB
testcase_29 AC 277 ms
93,728 KB
testcase_30 AC 130 ms
79,820 KB
testcase_31 AC 226 ms
94,016 KB
testcase_32 AC 203 ms
92,416 KB
testcase_33 AC 166 ms
87,400 KB
testcase_34 AC 197 ms
89,908 KB
testcase_35 AC 174 ms
87,260 KB
testcase_36 AC 114 ms
79,796 KB
testcase_37 AC 124 ms
80,940 KB
testcase_38 AC 211 ms
92,760 KB
testcase_39 AC 149 ms
83,800 KB
testcase_40 AC 150 ms
83,948 KB
testcase_41 AC 182 ms
90,784 KB
testcase_42 AC 167 ms
84,536 KB
testcase_43 AC 152 ms
85,180 KB
testcase_44 AC 150 ms
84,460 KB
testcase_45 AC 139 ms
83,536 KB
testcase_46 AC 124 ms
79,844 KB
testcase_47 AC 121 ms
79,348 KB
testcase_48 AC 227 ms
93,172 KB
testcase_49 AC 91 ms
77,312 KB
testcase_50 AC 33 ms
53,480 KB
testcase_51 AC 33 ms
53,608 KB
testcase_52 AC 326 ms
102,144 KB
testcase_53 AC 343 ms
102,196 KB
07_evil_01.txt AC 663 ms
127,996 KB
07_evil_02.txt AC 1,009 ms
152,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
# md = 998244353
md = 10**9+7

class UnionFind:
    def __init__(self, n):
        self.state = [-1]*n
        self.e = [0]*n
        self.size_table = [1]*n
        # cntはグループ数
        self.cnt = n

    def root(self, u):
        stack = []
        while self.state[u] >= 0:
            stack.append(u)
            u = self.state[u]
        for v in stack: self.state[v] = u
        return u

    def merge(self, u, v):
        u = self.root(u)
        v = self.root(v)
        self.e[u] += 1
        if u == v: return
        du = -self.state[u]
        dv = -self.state[v]
        if du < dv: u, v = v, u
        if du == dv: self.state[u] -= 1
        self.state[v] = u
        self.cnt -= 1
        self.size_table[u] += self.size_table[v]
        self.e[u] += self.e[v]
        return

    # グループの要素数
    def size(self, u):
        return self.size_table[self.root(u)]

    def cnt_egde(self, u):
        u = self.root(u)
        return self.e[u]

n = II()
uf = UnionFind(n)
to = [[] for _ in range(n)]
for i in range(n):
    a, b = LI1()
    to[a].append((b, i))
    to[b].append((a, i))
    uf.merge(a, b)

fin = set()
for u in range(n):
    u = uf.root(u)
    if u in fin: continue
    fin.add(u)
    if uf.size(u) != uf.cnt_egde(u):
        print("No")
        exit()

def dfs(u):
    stack = [(u, -1)]
    while stack:
        u, par = stack.pop()
        for v, i in to[u]:
            if v == par: continue
            if vis[v]:
                ans[i] = v+1
                ee[i] = True
                return v
            vis[v] = True
            stack.append((v, u))

def match(u):
    stack = [u]
    while stack:
        u = stack.pop()
        for v, i in to[u]:
            if ee[i]: continue
            ee[i] = True
            ans[i] = v+1
            stack.append(v)

ans = [-1]*n
ee = [False]*n
vis = [False]*n
for u in fin:
    r = dfs(u)
    match(r)

print("Yes")
print(*ans, sep="\n")
0