結果

問題 No.2263 Perms
ユーザー jiangly
提出日時 2023-04-07 21:40:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,519 bytes
コンパイル時間 2,040 ms
コンパイル使用メモリ 207,516 KB
最終ジャッジ日時 2025-02-12 00:51:33
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N, M;
    std::cin >> N >> M;
    
    std::vector A(N, std::vector<int>(N));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            std::cin >> A[i][j];
        }
        if (std::accumulate(A[i].begin(), A[i].end(), 0) != M) {
            std::cout << -1 << "\n";
            return 0;
        }
    }
    
    for (int i = 0; i < N; i++) {
        int s = 0;
        for (int j = 0; j < N; j++) {
            s += A[j][i];
        }
        if (s != M) {
            std::cout << -1 << "\n";
            return 0;
        }
    }
    
    for (int i = 0; i < M; i++) {
        std::vector<bool> vis(N);
        std::vector<int> xy(N, -1), yx(N, -1);
        std::function<bool(int)> dfs = [&](int x) {
            vis[x] = true;
            for (int y = 0; y < N; y++) {
                if (A[x][y] > 0) {
                    if (yx[y] == -1 || (!vis[yx[y]] && dfs(yx[y]))) {
                        xy[x] = y;
                        yx[y] = x;
                        return true;
                    }
                }
            }
            return false;
        };
        
        for (int i = 0; i < N; i++) {
            vis.assign(N, false);
            dfs(i);
        }
        for (int j = 0; j < N; j++) {
            std::cout << xy[j] + 1 << " \n"[j == N - 1];
            A[j][xy[j]] -= 1;
        }
    }
    
    return 0;
}
0