結果

問題 No.470 Inverse S+T Problem
ユーザー tubo28tubo28
提出日時 2016-12-20 13:34:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 3,180 bytes
コンパイル時間 1,975 ms
コンパイル使用メモリ 183,608 KB
実行使用メモリ 12,580 KB
最終ジャッジ日時 2023-08-24 01:20:54
合計ジャッジ時間 3,729 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 17 ms
6,236 KB
testcase_07 AC 18 ms
6,176 KB
testcase_08 AC 18 ms
6,232 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 93 ms
12,580 KB
testcase_30 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\t" #x " = " << x << endl

using Weight = int;
using Capacity = int;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

vector<int> kosaraju(const Graph &g) {
    int n = g.size(), sz = 0;
    Graph rg(n);
    vector<int> stk, cmp(n, -1), added(n), visited(n), ord(n);
    for (auto &es : g) {
        for (auto &e : es) rg[e.dst].emplace_back(e.dst, e.src, e.weight);
        sz += es.size();
    }
    stk.resize(n + sz);
    sz = 0;
    for (int i = 0; i < n; i++) {
        if (visited[i]) continue;
        int s = 0;
        stk[s++] = i;
        while (s != 0) {
            int v = stk[s - 1];
            visited[v] = true;
            bool pushed = false;
            for (auto &e : g[v]) {
                int dst = e.dst;
                if (!visited[dst]) {
                    stk[s++] = dst;
                    pushed = true;
                }
            }
            if (pushed) continue;
            int t = stk[s - 1];
            if (!added[t]) {
                added[t] = true;
                ord[n - ++sz] = t;
            }
            --s;
        }
    }
    int k = 0;
    for (int &u : ord) {
        if (cmp[u] != -1) continue;
        int s = 0;
        stk[s++] = u;
        while (s != 0) {
            int v = stk[--s];
            cmp[v] = k;
            for (auto &e : rg[v]) {
                int d = e.dst;
                if (cmp[d] == -1) stk[s++] = d;
            }
        }
        ++k;
    }
    return cmp;
}

int n;
vector<string> ss;

void solve(){
    if(n > 2756){
        cout << "Impossible" << endl;
        return;
    }

    int nn = n+n;
    Graph g(nn);

    auto sep = [&](int u, int v){
        int iu = (u+n) % nn;
        int iv = (v+n) % nn;
        g[u].emplace_back(u, v, 1);
        g[iv].emplace_back(iv, iu, 1);
    };

    vector<string> a(n), ab(n), bc(n), c(n);
    rep(i, n){
        string &s = ss[i];
        a[i] = s.substr(0, 1);
        ab[i] = s.substr(0, 2);
        bc[i] = s.substr(1, 2);
        c[i] = s.substr(2, 1);
    }

    rep(i, n){
        rep(j, i){
            if(a[i] == a[j] || bc[i] == bc[j]) sep(i, j+n);
            if(a[i] == c[j] || bc[i] == ab[j]) sep(i, j);
            if(c[i] == a[j] || ab[i] == bc[j]) sep(i+n, j+n);
            if(c[i] == c[j] || ab[i] == ab[j]) sep(i+n, j);
        }
    }

    vector<int> cmp = kosaraju(g);
    rep(i, n){
        if(cmp[i] == cmp[i+n]){
            cout << "Impossible" << '\n';
            return;
        }
    }

    cerr << "Possible" << '\n';
    rep(i, n){
        auto &s = ss[i];
        if(cmp[i] < cmp[i+n]) cout << s.substr(0, 2) << ' ' << s.substr(2) << '\n';
        else cout << s.substr(0, 1) << ' ' << s.substr(1) << '\n';
    }
}

int main(){
    while(cin >> n){
        ss.resize(n);
        rep(i, n) cin >> ss[i];
        solve();
    }
}
0