結果

問題 No.470 Inverse S+T Problem
ユーザー tubo28tubo28
提出日時 2016-12-20 14:23:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,064 bytes
コンパイル時間 1,691 ms
コンパイル使用メモリ 175,632 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-24 01:21:38
合計ジャッジ時間 3,903 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 RE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 3 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 WA -
testcase_22 RE -
testcase_23 WA -
testcase_24 RE -
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,376 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

struct union_find {
    std::vector<int> parent;
    int __size;
    union_find(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

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

    int n;
    cin >> n;
    if(n > 52){
        cout << "Impossible" << endl;
        exit(0);
    }
    vector<string> ss(n);
    rep(i, n) cin >> ss[i];

    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);
        c[i] = s.substr(2);
    }

    int nn = n+n;
    char g[55][55] = {};
    auto sep = [&](int u, int v){
        g[u][v] = g[(v+n) % nn][(u+n) % nn] = true;
    };
    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);
    }

    union_find uf(nn);
    rep(k, nn) rep(i, nn) rep(j, nn) g[i][j] |= g[i][k] & g[k][j];
    rep(i, nn) rep(j, nn) if(g[i][j] && g[j][i]) uf.unite(i, j);

    rep(i, n){
        if(uf.is_same(i, i+n)){
            cout << "Impossible" << '\n';
            exit(0);
        }
    }

    rep(i, n){
        if(uf.find(i) < uf.find(i+n)) cout << ab[i] << ' ' << c[i] << '\n';
        else cout << a[i] << ' ' << bc[i] << '\n';
    }
}
0