結果

問題 No.1640 簡単な色塗り
コンテスト
ユーザー tktk_snsn
提出日時 2021-08-06 23:31:10
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 870 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 44,672 KB
最終ジャッジ日時 2024-06-29 16:26:19
合計ジャッジ時間 23,967 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

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