結果

問題 No.1640 簡単な色塗り
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 22:42:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,320 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 355,036 KB
最終ジャッジ日時 2024-06-29 15:48:53
合計ジャッジ時間 65,502 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
59,776 KB
testcase_01 AC 41 ms
56,236 KB
testcase_02 AC 45 ms
55,772 KB
testcase_03 AC 46 ms
54,528 KB
testcase_04 AC 437 ms
151,068 KB
testcase_05 AC 1,033 ms
262,868 KB
testcase_06 AC 42 ms
54,400 KB
testcase_07 AC 41 ms
54,144 KB
testcase_08 AC 41 ms
54,144 KB
testcase_09 AC 42 ms
54,400 KB
testcase_10 AC 1,181 ms
142,208 KB
testcase_11 AC 1,017 ms
131,072 KB
testcase_12 AC 914 ms
121,848 KB
testcase_13 AC 1,528 ms
173,132 KB
testcase_14 AC 1,478 ms
172,524 KB
testcase_15 AC 737 ms
108,416 KB
testcase_16 AC 864 ms
115,840 KB
testcase_17 AC 1,366 ms
160,608 KB
testcase_18 AC 317 ms
85,504 KB
testcase_19 AC 899 ms
121,584 KB
testcase_20 AC 1,151 ms
139,840 KB
testcase_21 AC 1,003 ms
129,664 KB
testcase_22 AC 248 ms
82,816 KB
testcase_23 AC 1,277 ms
143,944 KB
testcase_24 AC 570 ms
96,140 KB
testcase_25 AC 979 ms
122,852 KB
testcase_26 AC 1,287 ms
153,164 KB
testcase_27 AC 736 ms
107,264 KB
testcase_28 AC 1,479 ms
173,476 KB
testcase_29 AC 1,296 ms
152,936 KB
testcase_30 AC 520 ms
110,720 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 AC 1,546 ms
248,956 KB
testcase_34 AC 1,534 ms
214,320 KB
testcase_35 AC 1,367 ms
219,136 KB
testcase_36 AC 583 ms
119,168 KB
testcase_37 AC 571 ms
112,768 KB
testcase_38 AC 1,989 ms
283,684 KB
testcase_39 AC 1,125 ms
194,816 KB
testcase_40 AC 1,009 ms
141,952 KB
testcase_41 AC 1,751 ms
245,564 KB
testcase_42 AC 1,104 ms
161,792 KB
testcase_43 AC 1,249 ms
215,552 KB
testcase_44 AC 1,152 ms
194,816 KB
testcase_45 AC 825 ms
131,840 KB
testcase_46 AC 531 ms
109,056 KB
testcase_47 AC 361 ms
96,384 KB
testcase_48 AC 1,757 ms
227,320 KB
testcase_49 AC 218 ms
84,608 KB
testcase_50 AC 41 ms
54,400 KB
testcase_51 AC 43 ms
54,256 KB
testcase_52 TLE -
testcase_53 TLE -
07_evil_01.txt TLE -
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 7)

#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