結果

問題 No.2263 Perms
ユーザー planesplanes
提出日時 2023-04-08 16:52:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 2,533 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 179,816 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 14:02:27
合計ジャッジ時間 4,099 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

  #include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)

template< typename flow_t >
struct FordFulkerson {
  struct edge {
    int to;
    flow_t cap;
    int rev;
    bool isrev;
    int idx;
  };

  vector< vector< edge > > graph;
  vector< int > used;
  const flow_t INF;
  int timestamp;

  FordFulkerson(int n) : INF(numeric_limits< flow_t >::max()), timestamp(0) {
    graph.resize(n);
    used.assign(n, -1);
  }

  void add_edge(int from, int to, flow_t cap, int idx = -1) {
    graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});
    graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});
  }

  flow_t dfs(int idx, const int t, flow_t flow) {
    if(idx == t) return flow;
    used[idx] = timestamp;
    for(auto &e : graph[idx]) {
      if(e.cap > 0 && used[e.to] != timestamp) {
        flow_t d = dfs(e.to, t, min(flow, e.cap));
        if(d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  flow_t max_flow(int s, int t) {
    flow_t flow = 0;
    for(flow_t f; (f = dfs(s, t, INF)) > 0; timestamp++) {
      flow += f;
    }
    return flow;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e : graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
      }
    }
  }
};


int main() {
  ll N,M;cin>>N>>M;
  vector<vector<ll>> A(N,vector<ll> (N));
  for(ll i=0;i<N;i++) {
    for(ll j=0;j<N;j++) cin>>A[i][j];
  }

bool ok=true;
for(ll i=0;i<N;i++) {
  ll count=0;
  for(ll j=0;j<N;j++)  {
    count+=A[i][j];
  }
  if(count!=M) ok=false;
}



for(ll i=0;i<N;i++) {
  ll count=0;
  for(ll j=0;j<N;j++) {
    count+=A[j][i];
  }
  if(count!=M) ok=false;
}

if(!ok) {
  cout<<-1<<endl;
  return 0;
}



for(ll i=0;i<M;i++) {
  ll V=2*N+2;
  FordFulkerson<ll> g(V);
  for(ll j=0;j<N;j++) {
    g.add_edge(0,j+1,1);
    for(ll h=0;h<N;h++) {
      if(A[j][h]>0) g.add_edge(j+1,N+h+1,1);
    }
    g.add_edge(N+j+1,V-1,1);
  }

ll a=g.max_flow(0,V-1);
vector<ll> P(N);
for(ll j=1;j<=N;j++) {
  for(auto x:g.graph[j]) {
    if(x.to>N&&x.cap==0) {
      P[j-1]=x.to-N;
    break;
    }
  }
}

for(auto x:P) cout<<x<<" ";
cout<<endl;
for(ll j=0;j<N;j++) {
  A[j][P[j]-1]--;
}
}

}







0