結果

問題 No.1640 簡単な色塗り
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 22:55:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,823 ms / 2,000 ms
コード長 2,377 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 11,120 KB
実行使用メモリ 106,308 KB
最終ジャッジ日時 2023-09-12 02:57:21
合計ジャッジ時間 48,600 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
13,328 KB
testcase_01 AC 22 ms
15,832 KB
testcase_02 AC 21 ms
8,864 KB
testcase_03 AC 20 ms
8,824 KB
testcase_04 AC 847 ms
85,764 KB
testcase_05 AC 1,059 ms
106,308 KB
testcase_06 AC 20 ms
8,784 KB
testcase_07 AC 20 ms
8,916 KB
testcase_08 AC 20 ms
8,872 KB
testcase_09 AC 21 ms
8,940 KB
testcase_10 AC 849 ms
50,392 KB
testcase_11 AC 674 ms
44,524 KB
testcase_12 AC 579 ms
37,660 KB
testcase_13 AC 1,244 ms
77,088 KB
testcase_14 AC 1,320 ms
76,712 KB
testcase_15 AC 422 ms
31,116 KB
testcase_16 AC 588 ms
34,816 KB
testcase_17 AC 1,017 ms
58,880 KB
testcase_18 AC 90 ms
13,820 KB
testcase_19 AC 588 ms
37,328 KB
testcase_20 AC 794 ms
49,128 KB
testcase_21 AC 613 ms
43,556 KB
testcase_22 AC 68 ms
11,960 KB
testcase_23 AC 879 ms
51,524 KB
testcase_24 AC 220 ms
19,976 KB
testcase_25 AC 574 ms
37,908 KB
testcase_26 AC 998 ms
55,092 KB
testcase_27 AC 417 ms
30,236 KB
testcase_28 AC 1,264 ms
68,936 KB
testcase_29 AC 976 ms
54,744 KB
testcase_30 AC 193 ms
19,956 KB
testcase_31 AC 1,725 ms
96,808 KB
testcase_32 AC 1,332 ms
75,600 KB
testcase_33 AC 905 ms
57,808 KB
testcase_34 AC 1,106 ms
73,056 KB
testcase_35 AC 856 ms
60,024 KB
testcase_36 AC 211 ms
20,332 KB
testcase_37 AC 264 ms
26,048 KB
testcase_38 AC 1,482 ms
82,380 KB
testcase_39 AC 534 ms
39,152 KB
testcase_40 AC 547 ms
37,208 KB
testcase_41 AC 1,133 ms
65,936 KB
testcase_42 AC 617 ms
44,508 KB
testcase_43 AC 664 ms
49,296 KB
testcase_44 AC 620 ms
48,580 KB
testcase_45 AC 467 ms
37,300 KB
testcase_46 AC 209 ms
22,388 KB
testcase_47 AC 130 ms
17,528 KB
testcase_48 AC 1,426 ms
91,172 KB
testcase_49 AC 62 ms
12,240 KB
testcase_50 AC 20 ms
8,764 KB
testcase_51 AC 20 ms
8,836 KB
testcase_52 AC 1,823 ms
93,268 KB
testcase_53 AC 1,733 ms
93,440 KB
07_evil_01.txt TLE -
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.buffer.readline
#Union-Find
class UnionFind():
    def __init__(self, n: int) -> None:
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * 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

    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 main():
    n = int(input())
    G = [[] for i in range(n)]
    UF = UnionFind(n)
    from collections import defaultdict
    ed_ind = defaultdict(list)
    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)
    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)
    def dfs2(v):
        for u in G[v]:
            if 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))] < len(ed_ind[(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")
if __name__ == '__main__':
    main()
    
0