結果

問題 No.2263 Perms
ユーザー k1suxuk1suxu
提出日時 2023-04-08 20:49:36
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,803 bytes
コンパイル時間 4,209 ms
コンパイル使用メモリ 301,952 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-04-14 17:17:50
合計ジャッジ時間 8,420 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

void solve() {
    int n, m;
    cin >> n >> m;
    vvi a(n, vi(n));
    rep(i, n) rep(j, n) cin >> a[i][j];

    vi th(n, 0), tw(n, 0);

    rep(i, n) {
        rep(j, n) {
            th[i] += a[i][j];
            tw[j] += a[i][j];
        }
    }

    vi repre;
    repre.push_back(*min_element(all(th)));
    repre.push_back(*max_element(all(th)));
    repre.push_back(*min_element(all(tw)));
    repre.push_back(*max_element(all(tw)));

    if(*max_element(all(repre)) != *min_element(all(repre)) || *max_element(all(repre)) != m) {
        cout << -1 << endl;
        return;
    }

    auto encode = [&](int x, int y) -> int {
        return x * n + y;
    };
    auto decode = [&](int xy) -> pii {
        return make_pair(xy/n, xy%n);
    };

    vi b(n*n);

    rep(i, n) rep(j, n) {
        b[encode(i, j)] = a[i][j];
    }


    vector<unordered_set<int>> g(n*n);

    rep(i, n-1) {
        rep(j, n) {
            rep(k, n) if(j != k && a[i+1][k] != 0) {
                g[encode(i, j)].insert(encode(i+1, k));
                // cout << encode(i, j) << "->" << encode(i+1, k) << endl;
            }
        }
    }

    stack<int> st;
    vector<bool> used(n, false);

    auto dfs = [&](int v, auto self) -> bool {
        st.push(v);
        vi ers;
        bool flag = false;
        for(auto e : g[v]) if(!used[e%n]) {
            if(b[e] == 0) {
                ers.push_back(e);
                continue;
            }
            used[e%n] = true;
            if(self(e, self)) {
                b[e]--;
                if(b[e] == 0) {
                    ers.push_back(e);
                }
                flag = true;
                break;
            }else {
                ers.push_back(e);
            }
            used[e%n] = false;
        }

        for(auto e : ers) g[v].erase(e);

        if(decode(v).first == n-1 || flag) {
            return true;
        }else {
            st.pop();
            return false;
        }
    };

    vvi perm;

    rep(i, n) {
        while(b[i] > 0) {
            rep(j, n) used[j] = (j == i);
            bool f = dfs(i, dfs);

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

            perm.push_back({});
            while(!st.empty()) {
                // cout << decode(st.top()).first << " " << decode(st.top()).second << endl;
                perm.back().push_back(st.top());
                st.pop();
            }
            b[i]--;

            cout << "\n";
        }
    }

    for(auto e : perm) {
        vi p(n);
        for(auto f : e) {
            pii now = decode(f);
            p[now.first] = now.second;
        }

        for(auto f : p) cout << f+1 << " "; cout << "\n";
    }
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0