結果

問題 No.1640 簡単な色塗り
ユーザー tktk_snsntktk_snsn
提出日時 2021-08-06 23:31:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 804 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 553 ms
コンパイル使用メモリ 10,972 KB
実行使用メモリ 42,224 KB
最終ジャッジ日時 2023-09-12 03:23:59
合計ジャッジ時間 22,071 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,144 KB
testcase_01 AC 18 ms
8,312 KB
testcase_02 AC 17 ms
8,284 KB
testcase_03 AC 17 ms
8,224 KB
testcase_04 AC 303 ms
30,488 KB
testcase_05 AC 305 ms
30,376 KB
testcase_06 AC 17 ms
8,312 KB
testcase_07 AC 17 ms
8,292 KB
testcase_08 AC 17 ms
8,364 KB
testcase_09 AC 17 ms
8,272 KB
testcase_10 AC 291 ms
21,312 KB
testcase_11 AC 237 ms
18,940 KB
testcase_12 AC 212 ms
17,672 KB
testcase_13 AC 463 ms
28,088 KB
testcase_14 AC 468 ms
27,944 KB
testcase_15 AC 155 ms
15,140 KB
testcase_16 AC 189 ms
16,824 KB
testcase_17 AC 379 ms
24,756 KB
testcase_18 AC 46 ms
9,776 KB
testcase_19 AC 210 ms
17,704 KB
testcase_20 AC 287 ms
20,976 KB
testcase_21 AC 230 ms
18,496 KB
testcase_22 AC 34 ms
9,240 KB
testcase_23 AC 304 ms
21,696 KB
testcase_24 AC 85 ms
12,000 KB
testcase_25 AC 214 ms
17,780 KB
testcase_26 AC 338 ms
23,232 KB
testcase_27 AC 144 ms
14,872 KB
testcase_28 AC 436 ms
27,048 KB
testcase_29 AC 339 ms
23,016 KB
testcase_30 AC 58 ms
12,204 KB
testcase_31 AC 383 ms
42,224 KB
testcase_32 AC 332 ms
35,240 KB
testcase_33 AC 270 ms
27,268 KB
testcase_34 AC 343 ms
31,876 KB
testcase_35 AC 185 ms
22,832 KB
testcase_36 AC 59 ms
12,992 KB
testcase_37 AC 102 ms
15,760 KB
testcase_38 AC 310 ms
33,180 KB
testcase_39 AC 145 ms
21,048 KB
testcase_40 AC 134 ms
18,088 KB
testcase_41 AC 250 ms
30,088 KB
testcase_42 AC 142 ms
20,472 KB
testcase_43 AC 218 ms
23,628 KB
testcase_44 AC 212 ms
23,076 KB
testcase_45 AC 140 ms
20,304 KB
testcase_46 AC 56 ms
12,008 KB
testcase_47 AC 48 ms
12,148 KB
testcase_48 AC 475 ms
39,272 KB
testcase_49 AC 31 ms
9,652 KB
testcase_50 AC 17 ms
8,368 KB
testcase_51 AC 17 ms
8,332 KB
testcase_52 AC 519 ms
39,276 KB
testcase_53 AC 804 ms
33,864 KB
07_evil_01.txt AC 928 ms
52,288 KB
07_evil_02.txt AC 1,384 ms
74,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class BipartiteMatching:
    def __init__(self, N, M):
        self.N = N
        self.M = M
        self.pairA = [-1] * N
        self.pairB = [-1] * M
        self.edge = [[] for _ in range(N)]
        self.was = [0] * N
        self.iter = 0

    def add(self, s, t):
        self.edge[s].append(t)

    def _DFS(self, s):
        self.was[s] = self.iter
        for t in self.edge[s]:
            if self.pairB[t] == -1:
                self.pairA[s] = t
                self.pairB[t] = s
                return True
        for t in self.edge[s]:
            if self.was[self.pairB[t]] != self.iter and self._DFS(self.pairB[t]):
                self.pairA[s] = t
                self.pairB[t] = s
                return True
        return False

    def solve(self):
        res = 0
        while True:
            self.iter += 1
            found = 0
            for i in range(self.N):
                if self.pairA[i] == -1 and self._DFS(i):
                    found += 1
            if not found:
                break
            res += found
        return res


N = int(input())
match = BipartiteMatching(N, N)
for i in range(N):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    match.add(i, a)
    match.add(i, b)

pair = match.solve()
if pair < N:
    print("No")
    exit()

print("Yes")
for x in match.pairA:
    print(x+1)
0