結果

問題 No.1078 I love Matrix Construction
ユーザー finefine
提出日時 2020-06-13 00:08:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 2,014 bytes
コンパイル時間 1,622 ms
コンパイル使用メモリ 168,928 KB
実行使用メモリ 49,944 KB
最終ジャッジ日時 2023-09-06 11:31:02
合計ジャッジ時間 5,885 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
27,248 KB
testcase_01 AC 9 ms
27,248 KB
testcase_02 AC 23 ms
30,700 KB
testcase_03 AC 53 ms
35,580 KB
testcase_04 AC 77 ms
38,908 KB
testcase_05 AC 69 ms
37,056 KB
testcase_06 AC 25 ms
30,336 KB
testcase_07 AC 16 ms
28,580 KB
testcase_08 AC 69 ms
37,200 KB
testcase_09 AC 13 ms
27,964 KB
testcase_10 AC 202 ms
49,944 KB
testcase_11 AC 77 ms
39,540 KB
testcase_12 AC 142 ms
45,524 KB
testcase_13 AC 179 ms
48,084 KB
testcase_14 AC 98 ms
41,652 KB
testcase_15 AC 148 ms
46,964 KB
testcase_16 AC 14 ms
28,280 KB
testcase_17 AC 10 ms
27,524 KB
testcase_18 AC 22 ms
29,640 KB
testcase_19 AC 44 ms
32,712 KB
testcase_20 AC 35 ms
32,736 KB
testcase_21 AC 11 ms
27,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

constexpr int MAX_V = 500 * 500 * 2;

int V;
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> vs;
bool used[MAX_V];
int cmp[MAX_V];

void add_edge(int from, int to) {
    G[from].push_back(to);
    rG[to].push_back(from);
}

void dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
        if (!used[G[v][i]]) dfs(G[v][i]);
    }
    vs.push_back(v);
}

void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rG[v].size(); i++) {
        if (!used[rG[v][i]]) rdfs(rG[v][i], k);
    }
}

int scc() {
    memset(used, 0, sizeof(used));
    vs.clear();
    for (int v = 0; v < V; v++) {
        if (!used[v]) dfs(v);
    }
    memset(used, 0, sizeof(used));
    int k = 0;
    for (int i = (int)vs.size() - 1; i >= 0; i--) {
        if (!used[vs[i]]) rdfs(vs[i], k++);
    }
    return k;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<int> s(n), t(n), u(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        --s[i];
    }

    for (int i = 0; i < n; i++) {
        cin >> t[i];
        --t[i];
    }

    V = n * n * 2;
    for (int i = 0; i < n; i++) {
        cin >> u[i];
        bool f0 = !(u[i] & 1);
        bool f1 = !(u[i] >> 1 & 1);

        for (int j = 0; j < n; j++) {
            add_edge(s[i] * n + j + n * n * f0, j * n + t[i] + n * n * !f1);
            add_edge(j * n + t[i] + n * n * f1, s[i] * n + j + n * n * !f0);
        }
    }

    scc();

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (cmp[i * n + j] == cmp[i * n + j + n * n]) {
                cout << -1 << newl;
                return 0;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << (cmp[i * n + j] > cmp[i * n + j + n * n]) << " \n"[j + 1 == n];
        }
    }

    return 0;
}
0