結果

問題 No.2263 Perms
ユーザー SSRSSSRS
提出日時 2023-04-07 21:47:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,962 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 179,312 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 16:54:25
合計ジャッジ時間 3,602 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 5 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 7 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 5 ms
6,940 KB
testcase_13 AC 6 ms
6,940 KB
testcase_14 AC 6 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 5 ms
6,944 KB
testcase_21 AC 5 ms
6,944 KB
testcase_22 AC 4 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 5 ms
6,940 KB
testcase_26 AC 6 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 5 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
template <typename Cap>
struct dinic{
  struct edge{
    int to, rev;
    Cap cap;
    edge(int to, int rev, Cap cap): to(to), rev(rev), cap(cap){
    }
  };
  int N;
  vector<vector<edge>> G;
  dinic(){
  }
  dinic(int N): N(N), G(N){
  }
  void add_edge(int from, int to, Cap cap){
    G[from].push_back(edge(to, G[to].size(), cap));
    G[to].push_back(edge(from, G[from].size() - 1, 0));
  }
  Cap dfs(vector<int> &d, vector<int> &iter, int v, int t, Cap f){
    if (v == t){
      return f; 
    }
    while (iter[v] < G[v].size()){
      int w = G[v][iter[v]].to;
      if (G[v][iter[v]].cap > 0 && d[v] < d[w]){
        Cap f2 = dfs(d, iter, w, t, min(f, G[v][iter[v]].cap));
        if (f2 > 0){
          G[v][iter[v]].cap -= f2;
          G[w][G[v][iter[v]].rev].cap += f2;
          return f2;
        }
      }
      iter[v]++;
    }
    return 0;
  }
  Cap max_flow(int s, int t){
    Cap flow = 0;
    while (true){
      vector<int> d(N, -1);
      d[s] = 0;
      queue<int> Q;
      Q.push(s);
      while (!Q.empty()){
        if (d[t] != -1){
          break;
        }
        int v = Q.front();
        Q.pop();
        for (auto &e : G[v]){
          int w = e.to;
          if (e.cap > 0 && d[w] == -1){
            d[w] = d[v] + 1;
            Q.push(w);
          }
        }
      }
      if (d[t] == -1){
        break;
      }
      vector<int> iter(N, 0);
      while (true){
        Cap f = dfs(d, iter, s, t, INF);
        if (f == 0){
          break;
        }
        flow += f;
      }
    }
    return flow;
  }
};
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(N));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      cin >> A[i][j];
    }
  }
  bool ok = true;
  for (int i = 0; i < N; i++){
    int S = 0, T = 0;
    for (int j = 0; j < N; j++){
      S += A[i][j];
      T += A[j][i];
    }
    if (S != M || T != M){
      ok = false;
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else {
    for (int i = 0; i < M; i++){
      dinic<int> G(N * 2 + 2);
      for (int j = 0; j < N; j++){
        G.add_edge(N * 2, j, 1);
        G.add_edge(N + j, N * 2 + 1, 1);
      }
      for (int j = 0; j < N; j++){
        for (int k = 0; k < N; k++){
          if (A[j][k] > 0){
            G.add_edge(j, N + k, 1);
          }
        }
      }
      G.max_flow(N * 2, N * 2 + 1);
      vector<int> P(N);
      for (int j = 0; j < N; j++){
        int cnt = G.G[j].size();
        for (int k = 0; k < cnt; k++){
          if (G.G[j][k].cap == 0){
            int w = G.G[j][k].to;
            if (w != N * 2){
              P[j] = w - N;
            }
          }
        }
      }
      for (int j = 0; j < N; j++){
        cout << P[j] + 1;
        if (j < N - 1){
          cout << ' ';
        }
      }
      cout << endl;
      for (int j = 0; j < N; j++){
        A[j][P[j]]--;
      }
    }
  }
}
0