結果

問題 No.241 出席番号(1)
ユーザー Mao-betaMao-beta
提出日時 2020-09-25 23:26:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,480 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 11,224 KB
実行使用メモリ 9,480 KB
最終ジャッジ日時 2023-09-10 17:04:36
合計ジャッジ時間 3,670 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,984 KB
testcase_01 AC 19 ms
8,856 KB
testcase_02 AC 19 ms
8,952 KB
testcase_03 AC 19 ms
8,836 KB
testcase_04 AC 21 ms
9,056 KB
testcase_05 AC 19 ms
8,804 KB
testcase_06 AC 18 ms
8,896 KB
testcase_07 AC 19 ms
8,896 KB
testcase_08 AC 18 ms
8,800 KB
testcase_09 AC 19 ms
8,960 KB
testcase_10 AC 20 ms
8,928 KB
testcase_11 AC 19 ms
8,920 KB
testcase_12 AC 19 ms
8,840 KB
testcase_13 AC 19 ms
8,900 KB
testcase_14 AC 22 ms
9,376 KB
testcase_15 AC 22 ms
9,264 KB
testcase_16 AC 19 ms
9,012 KB
testcase_17 AC 18 ms
8,884 KB
testcase_18 AC 18 ms
8,860 KB
testcase_19 AC 20 ms
8,796 KB
testcase_20 AC 19 ms
8,840 KB
testcase_21 AC 19 ms
8,816 KB
testcase_22 AC 18 ms
8,832 KB
testcase_23 AC 21 ms
9,068 KB
testcase_24 AC 22 ms
9,348 KB
testcase_25 AC 22 ms
9,372 KB
testcase_26 AC 22 ms
9,480 KB
testcase_27 AC 22 ms
9,356 KB
testcase_28 AC 22 ms
9,364 KB
testcase_29 AC 24 ms
9,436 KB
testcase_30 AC 24 ms
9,360 KB
testcase_31 AC 25 ms
9,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


INF = 10 ** 20
class Dinic:
    """ 最大流(Dinic) """


    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None

    def add_link(self, _from, to, cap):
        self.links[_from].append([cap, to, len(self.links[to])])
        self.links[to].append([0, _from, len(self.links[_from]) - 1])

    def bfs(self, s):
        from collections import deque

        depth = [-1] * self.n
        depth[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        self.depth = depth

    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0

    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, INF)
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, INF)


def main():
    N = NI()
    A = [NI() for _ in range(N)]
    s, t = 2*N, 2*N+1
    din = Dinic(2*N+2)
    for i in range(N):
        din.add_link(s, i, 1)
        din.add_link(i+N, t, 1)
        hate = A[i]
        for j in range(N):
            if j == hate: continue
            din.add_link(i, j+N, 1)
    if din.max_flow(s, t) != N:
        print(-1)
        exit()

    for i in range(N):
        for cap, to, _ in din.links[i]:
            if cap == 0:
                print(to - N)


if __name__ == "__main__":
    main()
0