結果

問題 No.241 出席番号(1)
ユーザー maspymaspy
提出日時 2020-03-20 02:43:28
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 3,509 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 11,288 KB
実行使用メモリ 9,536 KB
最終ジャッジ日時 2023-08-20 21:22:43
合計ジャッジ時間 3,005 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
8,808 KB
testcase_01 AC 22 ms
8,788 KB
testcase_02 AC 22 ms
9,008 KB
testcase_03 AC 21 ms
8,876 KB
testcase_04 AC 25 ms
9,000 KB
testcase_05 AC 20 ms
8,808 KB
testcase_06 AC 21 ms
8,864 KB
testcase_07 AC 20 ms
8,792 KB
testcase_08 AC 22 ms
8,804 KB
testcase_09 AC 21 ms
8,840 KB
testcase_10 AC 23 ms
8,736 KB
testcase_11 AC 22 ms
8,808 KB
testcase_12 AC 21 ms
8,928 KB
testcase_13 AC 21 ms
8,984 KB
testcase_14 AC 24 ms
9,296 KB
testcase_15 AC 25 ms
9,288 KB
testcase_16 AC 20 ms
8,800 KB
testcase_17 AC 20 ms
8,976 KB
testcase_18 AC 21 ms
8,896 KB
testcase_19 AC 20 ms
8,948 KB
testcase_20 AC 20 ms
8,792 KB
testcase_21 AC 21 ms
8,948 KB
testcase_22 AC 20 ms
8,884 KB
testcase_23 AC 23 ms
9,048 KB
testcase_24 AC 25 ms
9,324 KB
testcase_25 AC 26 ms
9,340 KB
testcase_26 AC 25 ms
9,400 KB
testcase_27 AC 26 ms
9,536 KB
testcase_28 AC 26 ms
9,340 KB
testcase_29 AC 26 ms
9,388 KB
testcase_30 AC 26 ms
9,504 KB
testcase_31 AC 26 ms
9,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque


class Dinic:
    def __init__(self, N, source, sink):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.source = source
        self.sink = sink

    def add_edge(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, 0, n1])  # 逆辺を cap 0 で追加

    def add_edge_undirected(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, cap, n1])

    def bfs(self):
        level = [0] * self.N
        G = self.G
        source = self.source
        sink = self.sink
        q = deque([source])
        level[source] = 1
        pop = q.popleft
        append = q.append
        while q:
            v = pop()
            lv = level[v] + 1
            for to, cap, rev in G[v]:
                if not cap:
                    continue
                if level[to]:
                    continue
                level[to] = lv
                if to == sink:
                    self.level = level
                    return
                append(to)
        self.level = level

    def dfs(self):
        INF = 10**18
        G = self.G
        sink = self.sink
        prog = self.progress
        level = self.level
        ascend = False
        ff = 0
        stack = [(self.source, INF)]
        while stack:
            if ff:
                # このまま更新だけして戻っていく
                v, f = stack.pop()
                p = prog[v]
                to, cap, rev = G[v][p]
                G[v][p][1] -= ff
                G[to][rev][1] += ff
                continue
            v, f = stack[-1]
            if v == sink:
                ff = f
                stack.pop()
                continue
            E = G[v]
            lv = level[v]
            if ascend:
                # 流せずに戻ってきた
                prog[v] += 1
            find_flag = False
            for i in range(prog[v], len(E)):
                to, cap, rev = E[i]
                prog[v] = i
                if not cap:
                    continue
                if level[to] <= lv:
                    continue
                find_flag = True
                break
            if not find_flag:
                ascend = True
                stack.pop()
                continue
            ascend = False
            x = f if f < cap else cap
            stack.append((to, x))
        return ff

    def max_flow(self):
        flow = 0
        while True:
            self.bfs()
            if not self.level[self.sink]:
                return flow
            self.progress = [0] * self.N
            while True:
                f = self.dfs()
                if not f:
                    break
                flow += f
        return flow


N, *A = map(int, read().split())
source = N + N
sink = N + N + 1
dinic = Dinic(N + N + 2, source, sink)
for i, x in enumerate(A):
    for j in range(N):
        if j != x:
            dinic.add_edge(i, N + j, 1)

for i in range(N):
    dinic.add_edge(source, i, 1)
    dinic.add_edge(i + N, sink, 1)

f = dinic.max_flow()
if f < N:
    print(-1)
    exit()

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