結果

問題 No.1640 簡単な色塗り
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 22:42:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,277 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,500 KB
実行使用メモリ 173,644 KB
最終ジャッジ日時 2024-06-29 15:47:42
合計ジャッジ時間 43,003 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
62,440 KB
testcase_01 AC 43 ms
54,644 KB
testcase_02 AC 36 ms
54,876 KB
testcase_03 AC 35 ms
55,136 KB
testcase_04 AC 343 ms
151,228 KB
testcase_05 RE -
testcase_06 AC 37 ms
54,452 KB
testcase_07 AC 35 ms
55,384 KB
testcase_08 AC 36 ms
54,868 KB
testcase_09 AC 35 ms
55,516 KB
testcase_10 AC 941 ms
142,204 KB
testcase_11 AC 808 ms
130,208 KB
testcase_12 AC 784 ms
122,240 KB
testcase_13 AC 1,307 ms
173,416 KB
testcase_14 AC 1,217 ms
172,504 KB
testcase_15 AC 666 ms
108,328 KB
testcase_16 AC 720 ms
115,372 KB
testcase_17 AC 1,149 ms
161,136 KB
testcase_18 AC 277 ms
85,288 KB
testcase_19 AC 759 ms
121,392 KB
testcase_20 AC 961 ms
140,052 KB
testcase_21 AC 915 ms
130,076 KB
testcase_22 AC 222 ms
83,276 KB
testcase_23 AC 1,133 ms
143,776 KB
testcase_24 AC 482 ms
95,696 KB
testcase_25 AC 882 ms
122,628 KB
testcase_26 AC 1,070 ms
153,116 KB
testcase_27 AC 604 ms
107,528 KB
testcase_28 AC 1,228 ms
173,644 KB
testcase_29 AC 1,213 ms
152,864 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 40 ms
55,680 KB
testcase_51 AC 39 ms
55,980 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