結果

問題 No.470 Inverse S+T Problem
ユーザー face4face4
提出日時 2019-11-23 11:08:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,125 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 85,600 KB
実行使用メモリ 6,640 KB
最終ジャッジ日時 2023-08-24 01:37:38
合計ジャッジ時間 2,747 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<set>
using namespace std;

vector<int> g[110], rev[110];
vector<int> vs, par(110);
vector<bool> used(110);

void add_edge(int from, int to){
    g[from].push_back(to);
    rev[to].push_back(from);
}

void dfs(int x){
    used[x] = true;
    for(int child : g[x]){
        if(!used[child])    dfs(child);
    }
    vs.push_back(x);    // postorder
}

void rdfs(int x, int k){
    used[x] = true;
    par[x] = k;
    for(int child : rev[x]){
        if(!used[child])    rdfs(child, k);
    }
}

int main(){
    int n;
    cin >> n;
    string s[n];
    for(int i = 0; i < n; i++)  cin >> s[i];
    if(n > 52){
        cout << "Impossible" << endl;
        return 0;
    }
    bool arc[110][110] = {};
    for(int i = 0; i < n; i++){
        for(int j = i+1; j < n; j++){
            for(int k : {1, 2}){
                for(int l : {1, 2}){
                    set<string> x;
                    x.insert(s[i].substr(0, k));
                    x.insert(s[i].substr(k));
                    x.insert(s[j].substr(0, l));
                    x.insert(s[j].substr(l));
                    if(x.size() != 4){
                        arc[i+(k-1)*n][j+(3-l-1)*n] = true;
                        arc[j+(l-1)*n][i+(3-k-1)*n] = true;
                    }
                }
            }
        }
    }
    for(int i = 0; i < n+n; i++){
        for(int j = 0; j < n+n; j++){
            if(arc[i][j])   add_edge(i, j);
        }
    }
    fill(used.begin(), used.end(), 0);
    for(int i = 0; i < n+n; i++){
        if(!used[i])    dfs(i);
    }
    fill(used.begin(), used.end(), 0);
    int k = 0;
    for(int i = vs.size()-1; i >= 0; i--){
        if(!used[vs[i]])    rdfs(vs[i], k++);
    }
    for(int i = 0; i < n; i++){
        if(par[i] == par[i+n]){
            cout << "Impossible" << endl;
            return 0;
        }
    }
    for(int i = 0; i < n; i++){
        if(par[i] > par[i+n]){
            cout << s[i].substr(0,1) << " " << s[i].substr(1) << endl;
        }else{
            cout << s[i].substr(0,2) << " " << s[i].substr(2) << endl;
        }
    }
    return 0;
}
0