結果

問題 No.2263 Perms
ユーザー 👑 rin204rin204
提出日時 2024-04-27 15:40:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 4,353 bytes
コンパイル時間 1,047 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 85,404 KB
最終ジャッジ日時 2024-11-15 19:07:38
合計ジャッジ時間 9,047 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
79,316 KB
testcase_01 AC 98 ms
79,616 KB
testcase_02 AC 99 ms
79,616 KB
testcase_03 AC 96 ms
79,556 KB
testcase_04 AC 95 ms
79,616 KB
testcase_05 AC 110 ms
80,364 KB
testcase_06 AC 249 ms
84,536 KB
testcase_07 AC 233 ms
83,228 KB
testcase_08 AC 160 ms
81,624 KB
testcase_09 AC 255 ms
85,404 KB
testcase_10 AC 97 ms
79,872 KB
testcase_11 AC 233 ms
83,356 KB
testcase_12 AC 242 ms
83,748 KB
testcase_13 AC 250 ms
84,268 KB
testcase_14 AC 261 ms
84,972 KB
testcase_15 AC 97 ms
80,128 KB
testcase_16 AC 142 ms
81,280 KB
testcase_17 AC 161 ms
81,580 KB
testcase_18 AC 145 ms
81,272 KB
testcase_19 AC 167 ms
81,212 KB
testcase_20 AC 242 ms
84,560 KB
testcase_21 AC 252 ms
84,216 KB
testcase_22 AC 240 ms
83,704 KB
testcase_23 AC 191 ms
82,024 KB
testcase_24 AC 147 ms
81,280 KB
testcase_25 AC 247 ms
84,032 KB
testcase_26 AC 249 ms
84,000 KB
testcase_27 AC 190 ms
82,528 KB
testcase_28 AC 152 ms
81,868 KB
testcase_29 AC 182 ms
82,136 KB
testcase_30 AC 119 ms
80,040 KB
testcase_31 AC 111 ms
80,136 KB
testcase_32 AC 157 ms
81,688 KB
testcase_33 AC 149 ms
81,188 KB
testcase_34 AC 100 ms
79,588 KB
testcase_35 AC 95 ms
79,584 KB
testcase_36 AC 132 ms
81,352 KB
testcase_37 AC 106 ms
80,000 KB
testcase_38 AC 240 ms
84,612 KB
testcase_39 AC 98 ms
79,616 KB
testcase_40 AC 98 ms
79,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

from dataclasses import dataclass


class mf_graph:
    @dataclass
    class edge:
        from_: int
        to: int
        cap: int
        flow: int
        # def __init__(self, from_, to, cap, flow):
        #     self.from_ = from_
        #     self.to = to
        #     self.cap = cap
        #     self.flow = flow

    @dataclass
    class _edge:
        to: int
        rev: int
        cap: int
        # def __init__(self, to, rev, cap):
        #     self.to = to
        #     self.rev = rev
        #     self.cap = cap

    def __init__(self, n):
        self.n = n
        self.G = [[] for _ in range(n)]
        self.pos = []

    def add_edge(self, from_, to, cap):
        m = len(self.pos)
        self.pos.append((from_, len(self.G[from_])))
        from_id = len(self.G[from_])
        to_id = len(self.G[to])
        if from_ == to:
            to_id += 1

        self.G[from_].append(mf_graph._edge(to, to_id, cap))
        self.G[to].append(mf_graph._edge(from_, from_id, 0))
        return m

    def get_edge(self, i):
        _e = self.G[self.pos[i][0]][self.pos[i][1]]
        _re = self.G[_e.to][_e.rev]
        return mf_graph.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap)

    def edges(self):
        m = len(self.pos)
        result = []
        for i in range(m):
            result.append(self.get_edge(i))

        return result

    def change_edge(self, i, new_cap, new_flow):
        _e = self.G[self.pos[i][0]][self.pos[i][1]]
        self.G[_e.to][_e.rev].cap = new_flow
        self.G[self.pos[i][0]][self.pos[i][1]].cap = new_cap - new_flow

    def flow(self, s, t, flow_limit=1 << 60):
        level = []
        iter = []
        que = deque()

        def bfs():
            nonlocal level
            level = [-1] * self.n
            level[s] = 0
            que.clear()
            que.append(s)
            while que:
                v = que.popleft()
                for e in self.G[v]:
                    if e.cap == 0 or level[e.to] >= 0:
                        continue
                    level[e.to] = level[v] + 1
                    if e.to == t:
                        return
                    que.append(e.to)

        def dfs(v, up):
            if v == s:
                return up

            nonlocal level, iter

            res = 0
            level_v = level[v]
            while iter[v] < len(self.G[v]):
                i = iter[v]
                iter[v] += 1
                e = self.G[v][i]
                if level_v <= level[e.to] or self.G[e.to][e.rev].cap == 0:
                    continue

                d = dfs(e.to, min(up - res, self.G[e.to][e.rev].cap))
                if d <= 0:
                    continue

                self.G[v][i].cap += d
                self.G[e.to][e.rev].cap -= d
                res += d
                if res == up:
                    return res

            level[v] = self.n
            return res

        flow = 0
        while flow < flow_limit:
            bfs()
            if level[t] == -1:
                break

            iter = [0] * self.n
            f = dfs(t, flow_limit - flow)
            if f == 0:
                break
            flow += f

        return flow

    def min_cut(self, s):
        visited = [False] * self.n
        que = deque()
        que.append(s)
        while que:
            p = que.popleft()
            visited[p] = True
            for e in self.G[p]:
                if e.cap and not visited[e.to]:
                    visited[e.to] = True
                    que.append(e.to)

        return visited


n, m = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]

for i in range(n):
    tot1 = 0
    tot2 = 0
    for j in range(n):
        tot1 += A[i][j]
        tot2 += A[j][i]

    if tot1 != m or tot2 != m:
        print(-1)
        exit()

for _ in range(m):
    G = mf_graph(2 * n + 2)
    s = 2 * n
    t = 2 * n + 1
    for i in range(n):
        G.add_edge(s, i, 1)
        G.add_edge(n + i, t, 1)
        for j in range(n):
            if A[i][j]:
                G.add_edge(i, n + j, 1)

    G.flow(s, t)
    P = [-1] * n
    for e in G.edges():
        if e.from_ != s and e.to != t and e.flow == 1:
            P[e.from_] = e.to - n + 1
            A[e.from_][e.to - n] -= 1
    print(*P)
0