結果

問題 No.470 Inverse S+T Problem
ユーザー parukiparuki
提出日時 2017-02-21 02:12:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 2,977 bytes
コンパイル時間 2,111 ms
コンパイル使用メモリ 187,516 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 01:27:18
合計ジャッジ時間 3,629 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

struct SCC{
    int V;
    vector<vector<int>> G, rG;
    vector<int> vs, cmp, used;

    SCC(int n)
        : V(n), G(n), rG(n), used(n), cmp(n){}

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

    void dfs(int v) {
        used[v] = 1;
        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] = 1;
        cmp[v] = k;
        for (int i = 0; i < rG[v].size(); ++i)
            if (!used[rG[v][i]]) rdfs(rG[v][i], k);
    }

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

vector<int> twoSAT(int n, vector<pii> clauses) {
    SCC scc(n * 2);
    for(auto clause : clauses) {
        int a, b;
        tie(a, b) = clause;
        int na = (a + n) % (n<<1);
        int nb = (b + n) % (n<<1);
        scc.add_edge(na, b);
        scc.add_edge(nb, a);
    }
    scc.scc();
    for(int i = 0; i < n; ++i) {
        if(scc.cmp[i] == scc.cmp[i + n]) {
            return vector<int>();
        }
    }
    vector<int> res(n);
    for(int i = 0; i < n; ++i) {
        res[i] = scc.cmp[i] > scc.cmp[i + n];
    }
    return res;
}

string U[52];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    if(N > 52)cout << "Impossible" << endl, exit(0);
    rep(i, N)cin >> U[i];

    vector<pii> c;
    rep(i, N)rep(j, N) if(i!=j){
        string &a = U[i], &b = U[j], s, t, u, v;
        rep(x, 2) {
            s = a.substr(0, 1 + x), t = a.substr(1 + x, 2 - x);
            rep(y, 2) {
                u = b.substr(0, 1 + y), v = b.substr(1 + y, 2 - y);
                set<string> S;
                for(auto &ch : {s,t,u,v})S.insert(ch);
                if(sz(S) != 4) {
                    c.emplace_back(i + (1 - x)*N, j + (1 - y)*N);
                }
            }
        }
    }
    vi tf = twoSAT(N, c);
    if(!sz(tf)) {
        cout << "Impossible" << endl;
        return 0;
    }
    rep(i, N) {
        int x = tf[i];
        string s = U[i].substr(0, 2 - x), t = U[i].substr(2 - x, 1 + x);
        cout << s << ' ' << t << endl;
    }
}
0