結果

問題 No.1640 簡単な色塗り
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 22:42:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,277 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 201,844 KB
最終ジャッジ日時 2023-09-12 02:44:45
合計ジャッジ時間 53,747 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
78,816 KB
testcase_01 AC 94 ms
76,284 KB
testcase_02 AC 101 ms
71,608 KB
testcase_03 AC 92 ms
71,688 KB
testcase_04 AC 463 ms
157,516 KB
testcase_05 RE -
testcase_06 AC 93 ms
71,616 KB
testcase_07 AC 95 ms
71,432 KB
testcase_08 AC 94 ms
71,792 KB
testcase_09 AC 95 ms
71,748 KB
testcase_10 AC 1,263 ms
163,100 KB
testcase_11 AC 1,078 ms
140,512 KB
testcase_12 AC 998 ms
137,212 KB
testcase_13 AC 1,587 ms
190,392 KB
testcase_14 AC 1,585 ms
201,844 KB
testcase_15 AC 853 ms
121,320 KB
testcase_16 AC 985 ms
135,872 KB
testcase_17 AC 1,458 ms
184,964 KB
testcase_18 AC 386 ms
91,560 KB
testcase_19 AC 1,017 ms
132,076 KB
testcase_20 AC 1,269 ms
154,712 KB
testcase_21 AC 1,138 ms
142,072 KB
testcase_22 AC 313 ms
89,264 KB
testcase_23 AC 1,360 ms
159,252 KB
testcase_24 AC 656 ms
103,596 KB
testcase_25 AC 1,078 ms
133,964 KB
testcase_26 AC 1,379 ms
168,148 KB
testcase_27 AC 824 ms
117,092 KB
testcase_28 AC 1,553 ms
198,200 KB
testcase_29 AC 1,401 ms
171,440 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 AC 95 ms
71,936 KB
testcase_51 AC 96 ms
71,612 KB
testcase_52 RE -
testcase_53 RE -
07_evil_01.txt TLE -
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#Union-Find
class UnionFind():
    def __init__(self, n: int) -> None:
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * self.n
        self.count = self.n

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

    def unite(self, x: int, y: int) -> None:
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.count -= 1

    def same(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)

    def size(self, x: int) -> int:
        return self.rank[x]

    def count(self) -> int:
        return self.count

n = int(input())
G = [[] for i in range(n)]
UF = UnionFind(n)
from collections import defaultdict
ed_ind = defaultdict(list)
ed_cnt = defaultdict(int)
for i in range(n):
    a, b = map(int, input().split())
    if a > b: a, b = b, a
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
    UF.unite(a - 1, b - 1)
    ed_ind[(a - 1, b - 1)].append(i)
    ed_cnt[(a - 1, b - 1)] += 1
done = [0] * n
visit = [0] * n
used = defaultdict(int)
def dfs(v):
    visit[v] = 1
    for u in G[v]:
        if not visit[u]: 
            dfs(u)
            used[(min(u, v), max(u, v))] += 1
for i in range(n):
    dfs(i)
visit = [0] * n
def dfs2(v):
    visit[v] = 1
    for u in G[v]:
        if (not visit[u]) and (used[(min(u, v), max(u, v))] > 0):
            for i in ed_ind[(min(u, v), max(u, v))]:
                if ans[i] == 0:
                    ans[i] = u + 1
                    dfs2(u)
                    break
ans = [0] * n
for i in range(n):
    if not done[UF.find(i)]:
        for v in G[i]:
            if used[(min(v, i), max(v, i))] < ed_cnt[(min(v, i), max(v, i))]:
                for x in ed_ind[(min(v, i), max(v, i))]:
                    if ans[x] == 0: 
                        ans[x] = i + 1
                        dfs2(i)
                        break
                done[UF.find(i)] = 1
                break
if any(i == 0 for i in ans) or len(set(ans)) != n: exit(print("No"))
print("Yes")
print(*ans, sep="\n")
0