結果

問題 No.470 Inverse S+T Problem
ユーザー parukiparuki
提出日時 2016-12-20 16:02:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 2,422 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 183,232 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 01:21:46
合計ジャッジ時間 3,572 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

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];
    SCC scc(N*2);
    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 &c : {s,t,u,v})S.insert(c);
                if(sz(S) != 4) {
                    scc.add_edge(i+x*N, j+(1-y)*N);
                }
            }
        }
    }
    scc.scc();
    auto &c = scc.cmp;
    rep(i, N)if(c[i] == c[i + N])cout << "Impossible" << endl, exit(0);
    rep(i, N) {
        int x = c[i] < c[i + N];
        string s = U[i].substr(0, 1 + x), t = U[i].substr(1 + x, 2 - x);
        cout << s << ' ' << t << endl;
    }
}
0