結果

問題 No.1640 簡単な色塗り
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 22:54:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 2,406 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 11,208 KB
実行使用メモリ 156,776 KB
最終ジャッジ日時 2023-09-12 02:58:59
合計ジャッジ時間 52,296 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
13,356 KB
testcase_01 AC 22 ms
156,776 KB
testcase_02 AC 21 ms
13,164 KB
testcase_03 AC 21 ms
8,768 KB
testcase_04 AC 1,208 ms
86,148 KB
testcase_05 AC 1,222 ms
102,440 KB
testcase_06 AC 20 ms
8,804 KB
testcase_07 AC 20 ms
8,924 KB
testcase_08 AC 20 ms
8,868 KB
testcase_09 RE -
testcase_10 AC 992 ms
50,292 KB
testcase_11 AC 801 ms
44,428 KB
testcase_12 AC 675 ms
37,580 KB
testcase_13 AC 1,400 ms
77,096 KB
testcase_14 AC 1,558 ms
76,936 KB
testcase_15 AC 486 ms
31,240 KB
testcase_16 AC 710 ms
34,924 KB
testcase_17 AC 1,140 ms
58,836 KB
testcase_18 AC 104 ms
13,840 KB
testcase_19 AC 684 ms
37,356 KB
testcase_20 AC 914 ms
49,252 KB
testcase_21 AC 705 ms
43,712 KB
testcase_22 AC 79 ms
11,776 KB
testcase_23 AC 1,034 ms
51,552 KB
testcase_24 AC 258 ms
19,908 KB
testcase_25 AC 661 ms
37,924 KB
testcase_26 AC 1,170 ms
55,260 KB
testcase_27 AC 489 ms
30,336 KB
testcase_28 AC 1,459 ms
69,000 KB
testcase_29 AC 1,148 ms
54,676 KB
testcase_30 AC 236 ms
19,792 KB
testcase_31 RE -
testcase_32 AC 1,480 ms
73,768 KB
testcase_33 RE -
testcase_34 AC 1,220 ms
70,756 KB
testcase_35 AC 943 ms
58,488 KB
testcase_36 RE -
testcase_37 AC 302 ms
25,424 KB
testcase_38 RE -
testcase_39 RE -
testcase_40 AC 604 ms
36,672 KB
testcase_41 AC 1,250 ms
64,544 KB
testcase_42 RE -
testcase_43 RE -
testcase_44 AC 702 ms
47,452 KB
testcase_45 AC 538 ms
36,368 KB
testcase_46 AC 240 ms
21,968 KB
testcase_47 AC 149 ms
17,312 KB
testcase_48 AC 1,573 ms
88,684 KB
testcase_49 AC 72 ms
12,096 KB
testcase_50 RE -
testcase_51 AC 21 ms
8,796 KB
testcase_52 AC 1,994 ms
91,552 KB
testcase_53 AC 1,901 ms
91,456 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]

n = int(input())
G = [[] for i in range(n)]
UF = UnionFind(n)
from collections import defaultdict
ed_ind = dict()
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)
    if (a - 1, b - 1) not in ed_ind: ed_ind[(a - 1, b - 1)] = []
    ed_ind[(a - 1, b - 1)].append(i)
done = [0] * n
visit = [0] * n
used = dict()
def dfs(v):
    visit[v] = 1
    for u in G[v]:
        if not visit[u]: 
            dfs(u)
            if (min(u, v), max(u, v)) not in used:
                used[(min(u, v), max(u, v))] = 1
            else:
                used[(min(u, v), max(u, v))] += 1
for i in range(n):
    if not visit[i]: 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 (min(v, i), max(v, i)) not in used: used[(min(v, i), max(v, i))] = 0
            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")
0