結果

問題 No.1640 簡単な色塗り
ユーザー tktk_snsntktk_snsn
提出日時 2021-08-06 23:29:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 82,788 KB
実行使用メモリ 179,712 KB
最終ジャッジ日時 2024-06-29 16:25:14
合計ジャッジ時間 15,171 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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