#ifndef hari64 #include // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #define debug(...) #else #include "util/viewer.hpp" #define debug(...) viewer::_debug(__LINE__, #__VA_ARGS__, __VA_ARGS__) #endif using namespace std; constexpr int INF = 1001001001; constexpr long long INFll = 1001001001001001001; template bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template bool chmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } struct Xor128 { // period 2^128 - 1 uint32_t x, y, z, w; static constexpr uint32_t min() { return 0; } static constexpr uint32_t max() { return UINT32_MAX; } Xor128(uint32_t seed = 0) : x(123456789), y(362436069), z(521288629), w(88675123 + seed) {} uint32_t operator()() { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } uint32_t operator()(uint32_t l, uint32_t r) { return ((*this)() % (r - l)) + l; } uint32_t operator()(uint32_t r) { return (*this)() % r; } }; struct Rand { // https://docs.python.org/ja/3/library/random.html Rand(){}; Rand(int seed) : gen(seed){}; // シードを変更します inline void set_seed(int seed) { Xor128 _gen(seed); gen = _gen; } // ランダムな浮動小数点数(範囲は[0.0, 1.0)) を返します inline double random() { return (double)gen() / (double)gen.max(); } // a <= b であれば a <= N <= b 、b < a であれば b <= N <= a // であるようなランダムな浮動小数点数 N を返します inline double uniform(double a, double b) { if (b < a) swap(a, b); return a + (b - a) * double(gen()) / double(gen.max()); } // range(0, stop) の要素からランダムに選ばれた要素を返します inline uint32_t randrange(uint32_t r) { return gen(r); } // range(start, stop) の要素からランダムに選ばれた要素を返します inline uint32_t randrange(uint32_t l, uint32_t r) { return gen(l, r); } // a <= N <= b であるようなランダムな整数 N を返します randrange(a, b + 1) // のエイリアスです inline uint32_t randint(uint32_t l, uint32_t r) { return gen(l, r + 1); } // シーケンス x をインプレースにシャッフルします template void shuffle(vector& x) { for (int i = x.size(), j; i > 1;) { j = gen(i); swap(x[j], x[--i]); } } // 空でないシーケンス seq からランダムに要素を返します template T choice(const vector& seq) { assert(!seq.empty()); return seq[gen(seq.size())]; } // 相対的な重みに基づいて要素が選ばれます (※複数回呼ぶ場合は処理を変えた方が良い) template T choice(const vector& seq, const vector& weights) { assert(seq.size() == weights.size()); vector acc(weights.size()); acc[0] = weights[0]; for (int i = 1; i < int(weights.size()); i++) acc[i] = acc[i - 1] + weights[i]; return seq[lower_bound(acc.begin(), acc.end(), random() * acc.back()) - acc.begin()]; } // 母集団のシーケンスまたは集合から選ばれた長さ k // の一意な要素からなるリストを返します 重複無しのランダムサンプリングに用いられます template vector sample(const vector& p, int k) { int j, i = 0, n = p.size(); assert(0 <= k && k <= n); vector ret(k); unordered_set s; for (; i < k; i++) { do { j = gen(n); } while (s.find(j) != s.end()); s.insert(j); ret[i] = p[j]; } return ret; } // 正規分布です mu は平均で sigma は標準偏差です double normalvariate(double mu = 0.0, double sigma = 1.0) { double u2, z, NV = 4 * exp(-0.5) / sqrt(2.0); while (true) { u2 = 1.0 - random(); z = NV * (random() - 0.5) / u2; if (z * z / 4.0 <= -log(u2)) break; } return mu + z * sigma; } private: Xor128 gen; } myrand; // clang-format off template struct simple_queue{vectorpayload;int pos=0;void reserve(int n){payload.reserve(n);}int size()const{return int(payload.size())-pos;}bool empty()const{return pos==int(payload.size());}void push(const T&t){payload.push_back(t);}T&front(){return payload[pos];}void clear(){payload.clear();pos=0;}void pop(){pos++;}}; template struct mf_graph{ mf_graph():_n(0){};explicit mf_graph(int n):_n(n),g(n){};struct edge{int from,to;Cap cap,flow;}; // fromからtoへ最大容量cap、流量 0 の辺を追加し、何番目に追加された辺かを返す int add_edge(int from,int to,Cap cap){assert(0<=from&&from<_n);assert(0<=to&&to<_n);assert(0<=cap);int m=int(pos.size());pos.push_back({from,int(g[from].size())});int from_id=int(g[from].size());int to_id=int(g[to].size());if(from==to)to_id++;g[from].push_back(_edge{to,to_id,cap});g[to].push_back(_edge{from,from_id,0});return m;} // 今の内部の辺の状態を返す 辺の順番はadd_edgeで追加された順番と同一 edge get_edge(int i){assert(0<=i&&iedges(){vectorresult;for(int i=0;i::max());} // 頂点 s から t へ流量 flow_limit に達するまで流せる限り流し、 流せた量を返す 複数回呼ぶことも可能 Cap flow(int s,int t,Cap flow_limit){assert(0<=s&&s<_n);assert(0<=t&&t<_n);assert(s!=t);vectorlevel(_n),iter(_n);simple_queueque; auto bfs=[&](){fill(level.begin(),level.end(),-1);level[s]=0;que.clear();que.push(s);while(!que.empty()){int v=que.front();que.pop();for(auto e:g[v]){if(e.cap==0||level[e.to]>=0)continue;level[e.to]=level[v]+1;if(e.to==t)return;que.push(e.to);}}}; auto dfs=[&](auto self,int v,Cap up){if(v==s)return up;Cap res=0;int level_v=level[v];for(int&i=iter[v];imin_cut(int s){vectorvisited(_n);simple_queueque;que.push(s);while(!que.empty()){int p=que.front();que.pop();visited[p]=true;for(auto e:g[p]){if(e.cap&&!visited[e.to]){visited[e.to]=true;que.push(e.to);}}}return visited;} // debug用 string state(){string ret("\n");for(auto&e:edges())ret+="[from,to/cap,flow]:"+to_string(e.from)+","+to_string(e.to)+"/"+to_string(e.cap)+","+to_string(e.flow)+"\n";return ret;} private: int _n;struct _edge{int to,rev;Cap cap;};vector>pos;vector>g; }; // 最小流量制限付き最大流 // ref: https://snuke.hatenablog.com/entry/2016/07/10/043918 // s t S→T,s→T,S→t,s→tと流す // ↘ ↗ 先に最小流量制限分だけ流しておくというのが基本的な考え方 // u -> v u → v は一つを取り出しているだけなので、逆向きの辺も存在 // ↘↗ // S - -> T 蟻本などではS→s,t→Tに∞の容量の辺を張っている template struct mf_graph_lb{ mf_graph_lb(int n):g(n+2),S(n),T(n+1),sum_lb(0){} // fromからtoへ最小流量制限low、 最大容量high、流量 0 の辺を追加する void add_edge(int from,int to,Cap low,Cap high){assert(from!=to&&high>=low);g.add_edge(from,to,high-low);if(low!=0){g.add_edge(S,to,low);g.add_edge(from,T,low);sum_lb+=low;}} // 頂点 s から t へ流せる限り流し、流せた量を返す 制約を満たせない場合 -1 Cap flow(int s,int t){assert(s!=t);Cap a=g.flow(S,T);Cap b=g.flow(s,T);Cap c=g.flow(S,t);Cap d=g.flow(s,t);debug(make_tuple(a,b,c,d));return(a+c==sum_lb&&a+b==sum_lb)?b+d:-1;} private: mf_graphg;int S,T;Cap sum_lb; }; // clang-format on int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; // N = myrand.randint(2, 5), M = myrand.randint(2, 5); cin >> N >> M; vector> As(N, vector(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> As[i][j]; } } // for (int i = 0; i < M; i++) { // vector Ps(N); // iota(Ps.begin(), Ps.end(), 0); // myrand.shuffle(Ps); // for (int i = 0; i < N; i++) { // As[i][Ps[i]]++; // } // } { mf_graph g(2 * N + 2); int s = 2 * N; int t = 2 * N + 1; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { g.add_edge(i, N + j, As[i][j]); } } for (int i = 0; i < N; i++) { g.add_edge(s, i, M); } for (int i = 0; i < N; i++) { g.add_edge(N + i, t, M); } auto res = g.flow(s, t); debug(res); if (res != N * M) { cout << -1 << endl; return 0; } } vector> ans; for (int _ = 0; _ < M; _++) { mf_graph g(2 * N + 2); int s = 2 * N; int t = 2 * N + 1; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { g.add_edge(i, N + j, max(0, As[i][j])); } } for (int i = 0; i < N; i++) { g.add_edge(s, i, 1); } for (int i = 0; i < N; i++) { g.add_edge(N + i, t, 1); } auto res = g.flow(s, t, N); debug(res); if (res != N) { cout << -1 << endl; return 0; } ans.push_back({}); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { auto e = g.get_edge(i * N + j); if (e.flow == 1) { ans.back().push_back(j + 1); As[i][j]--; if (As[i][j] < 0) { cout << -1 << endl; return 0; } break; } } } assert(int(ans.back().size()) == N); set check(ans.back().begin(), ans.back().end()); if (int(check.size()) != N) { cout << -1 << endl; return 0; } } for (auto& elems : As) { for (auto& e : elems) { if (e != 0) { cout << -1 << endl; return 0; } } } for (auto& elems : ans) { for (auto& elem : elems) cout << elem << " "; cout << endl; } return 0; }