結果

問題 No.470 Inverse S+T Problem
ユーザー Hoi_koroHoi_koro
提出日時 2016-12-21 15:23:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,304 bytes
コンパイル時間 1,871 ms
コンパイル使用メモリ 139,444 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 01:22:14
合計ジャッジ時間 3,601 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <tuple>
#include <queue>
#include <stack>
#include <functional>
#include <utility>
#include <complex>
#include <bitset>
#include <numeric>
#include <random>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n) ;++(i))
#define REPN(i,a,n) FOR((i),(a),(a)+(n))
#define FOR(i,a,b) for(int (i)=(a); (i)<(b) ;++(i))
#define PB push_back
#define MP make_pair
#define SE second
#define FI first
#define DBG(a) cerr<<(a)<<endl;
#define ALL(v) (v).begin(),(v).end()
typedef long long LL;  typedef pair<LL, LL> PLL; typedef vector<LL> VLL;
typedef vector<int> VI;
const LL LINF=334ll<<53; const int INF=15<<26; const LL MOD=1E9+7;
//StronglyC ConeectC Components

struct Edge{
    int from,to;
    Edge(int from,int to)
        : from(from),to(to){};
    Edge(){Edge(0,0);}
};
typedef vector<vector<Edge>> Graph;

struct stronglyConnectedComponents{
    vector<int> scrank,vis,po;
    stronglyConnectedComponents(int n){
        scrank=vector<int>(n); vis=vector<int>(n);
    }
    private:
    void dfs(int a,Graph &g){
        vis[a]=1;
        for(int i=0; i<g[a].size(); ++i){
            if(!vis[g[a][i].to]) dfs(g[a][i].to,g);
        }
        po.push_back(a);
    }
    void rdfs(int a, int k, Graph &rg){
        scrank[a]=k;
        vis[a]=1;
        for(int i=0; i<rg[a].size(); ++i){
            if(!vis[rg[a][i].to]) rdfs(rg[a][i].to,k,rg);
        }
    }
    public:
    void calc(Graph &g, Graph& rg){
        for(int i=0; i<g.size(); ++i){
            if(!vis[i]) dfs(i,g);
        }
        fill(ALL(vis),0);
        int c=0;
        for(int i=rg.size()-1; i>=0; --i){
            if(!vis[po[i]]) rdfs(po[i],c++,rg);
        }
    }
};



int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    if(n>52){cout << "Impossible" << endl; return 0;}
    string s[52]={};
    vector<tuple<string,int,int>> v;
    REP(i,n){
        cin >> s[i];
        v.push_back(make_tuple(s[i].substr(0,1),2*i,2*i+1));
        v.push_back(make_tuple(s[i].substr(1,2),2*i,2*i+1));
        v.push_back(make_tuple(s[i].substr(0,2),2*i+1,2*i));
        v.push_back(make_tuple(s[i].substr(2,1),2*i+1,2*i));
    }
    sort(ALL(v));
    Graph g(n*2),rev(n*2);
    stronglyConnectedComponents scc(n*2);
    REP(i,4*n){
        FOR(j,i+1,4*n){
            if(get<0>(v[i])==get<0>(v[j])){
                g[get<1>(v[i])].push_back(Edge(get<1>(v[i]),get<2>(v[j])));
                rev[get<2>(v[j])].push_back(Edge(get<2>(v[j]),get<1>(v[i])));
                g[get<1>(v[j])].push_back(Edge(get<1>(v[j]),get<2>(v[i])));
                rev[get<2>(v[i])].push_back(Edge(get<2>(v[i]),get<1>(v[j])));
            }
        }
    }
    scc.calc(g,rev);
    REP(i,n) {
        if(scc.scrank[2*i]==scc.scrank[2*i+1]){
            cout << "Impossible"<<endl;
            return 0;
        }
    }
    REP(i,n){
        if(scc.scrank[2*i]>scc.scrank[2*i+1]) 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