結果

問題 No.1640 簡単な色塗り
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 22:45:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,193 bytes
コンパイル時間 138 ms
コンパイル使用メモリ 82,532 KB
実行使用メモリ 345,672 KB
最終ジャッジ日時 2024-06-29 15:50:44
合計ジャッジ時間 56,471 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
59,904 KB
testcase_01 AC 41 ms
54,272 KB
testcase_02 AC 46 ms
54,144 KB
testcase_03 AC 41 ms
54,272 KB
testcase_04 AC 294 ms
139,056 KB
testcase_05 AC 879 ms
245,376 KB
testcase_06 AC 41 ms
54,272 KB
testcase_07 AC 41 ms
54,528 KB
testcase_08 AC 41 ms
54,528 KB
testcase_09 AC 43 ms
54,144 KB
testcase_10 AC 995 ms
128,896 KB
testcase_11 AC 818 ms
117,376 KB
testcase_12 AC 751 ms
111,232 KB
testcase_13 AC 1,255 ms
150,800 KB
testcase_14 AC 1,239 ms
151,148 KB
testcase_15 AC 620 ms
101,248 KB
testcase_16 AC 733 ms
106,496 KB
testcase_17 AC 1,127 ms
141,916 KB
testcase_18 AC 258 ms
83,200 KB
testcase_19 AC 758 ms
111,616 KB
testcase_20 AC 965 ms
124,928 KB
testcase_21 AC 882 ms
119,036 KB
testcase_22 AC 203 ms
82,024 KB
testcase_23 AC 1,069 ms
130,176 KB
testcase_24 AC 480 ms
91,264 KB
testcase_25 AC 801 ms
111,004 KB
testcase_26 AC 1,029 ms
133,556 KB
testcase_27 AC 606 ms
100,832 KB
testcase_28 AC 1,193 ms
146,376 KB
testcase_29 AC 1,067 ms
135,964 KB
testcase_30 AC 439 ms
105,472 KB
testcase_31 TLE -
testcase_32 AC 1,866 ms
305,516 KB
testcase_33 AC 1,299 ms
225,664 KB
testcase_34 AC 1,267 ms
195,504 KB
testcase_35 AC 1,153 ms
203,776 KB
testcase_36 AC 464 ms
106,752 KB
testcase_37 AC 454 ms
103,168 KB
testcase_38 AC 1,685 ms
257,172 KB
testcase_39 AC 966 ms
180,992 KB
testcase_40 AC 860 ms
139,904 KB
testcase_41 AC 1,451 ms
225,212 KB
testcase_42 AC 953 ms
154,368 KB
testcase_43 AC 1,132 ms
208,768 KB
testcase_44 AC 1,017 ms
186,624 KB
testcase_45 AC 667 ms
123,904 KB
testcase_46 AC 464 ms
109,568 KB
testcase_47 AC 300 ms
92,928 KB
testcase_48 AC 1,467 ms
203,324 KB
testcase_49 AC 179 ms
82,176 KB
testcase_50 AC 43 ms
54,272 KB
testcase_51 AC 40 ms
54,528 KB
testcase_52 TLE -
testcase_53 TLE -
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 = 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)
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))] < 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