結果

問題 No.470 Inverse S+T Problem
ユーザー btkbtk
提出日時 2017-06-20 16:45:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,508 bytes
コンパイル時間 1,984 ms
コンパイル使用メモリ 184,656 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 01:29:06
合計ジャッジ時間 3,706 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

#define CIN_ONLY if(1)
struct cww{cww(){
    CIN_ONLY{
        ios::sync_with_stdio(false);cin.tie(0);
    }
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if(0)
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}

#define ITR(i,v) for(auto & i :(v))
typedef vector<int> V;
typedef vector<V> VV;
typedef VV Graph;

class SCC{
private:
    Graph e,r_e;
    V vs;
public:
    int size;
    V comp;
    VV node;
    Graph edge;
private:
    void dfs(int v){
        comp[v]=0;
        ITR(u,e[v])if(comp[u]<0)dfs(u);
        vs.pb(v);
    }
    void rdfs(int v,int k){
        comp[v]=k;
        ITR(u,r_e[v])if(comp[u]<0)rdfs(u,k);
    }
    void decomposition(){
        comp.resize(size);
        fill(ALL(comp),-1);
        vs.reserve(size);
        REP(v,size)if(comp[v]<0)dfs(v);
        reverse(ALL(vs));
        fill(ALL(comp),-1);
        int k=0;
        ITR(v,vs)if(comp[v]<0)rdfs(v,k++);
        node.resize(k);
        edge.resize(k);
        REP(v,size){
            node[comp[v]].pb(v);
            for(auto& u : e[v]){
                if(!isSame(v,u)){
                    edge[comp[v]].pb(comp[u]);
                }
            }
        }
        size=k;
    }
public:
    bool isSame(int a,int b){
	return comp[a]==comp[b];
    }
    SCC(Graph& in){
        size=in.size();
        e=in;
        r_e.resize(size);
        REP(v,size)ITR(u,e[v])r_e[u].pb(v);
        decomposition();
    }
};




void add_edge(Graph &g,int u,int v){
    g[u].pb(v);
}
void add_or(Graph& g,V &rev,int u,int v){
    add_edge(g,rev[u],v);
    add_edge(g,rev[v],u);
}
void add_xor(Graph& g,V &rev,int u,int v){
    add_or(g,rev,u,v);
    add_or(g,rev,rev[u],rev[v]);
}

int main(){
    int N;
    cin>>N;
    if(N>26*26){
        cout<<"Impossible"<<endl;
        return 0;
    }
    vector<string> U(N);
    cin>>U;
    VV g;
    V t(N),f(N),rev(2*N);
    REP(i,N){
        t[i]=g.size();g.pb({});
        f[i]=g.size();g.pb({});
        rev[t[i]]=f[i];
        rev[f[i]]=t[i];
    }
    REP(i,N){
        REP(j,i){
            bool tt=false;
            bool tf=false;
            bool ft=false;
            bool ff=false;
            tt|=(U[i][0]==U[j][0]);
            ff|=(U[i][2]==U[j][2]);
            tf|=(U[i][0]==U[j][2]);
            ft|=(U[i][2]==U[j][0]);
            tt|=(U[i][1]==U[j][1]&&U[i][2]==U[j][2]);
            tf|=(U[i][1]==U[j][0]&&U[i][2]==U[j][1]);
            ft|=(U[i][0]==U[j][1]&&U[i][1]==U[j][2]);
            ff|=(U[i][0]==U[j][0]&&U[i][1]==U[j][1]);
            if(tt)add_or(g,rev,f[i],f[j]);
            if(ft)add_or(g,rev,t[i],f[j]);
            if(tf)add_or(g,rev,f[i],t[j]);
            if(ff)add_or(g,rev,t[i],t[j]);
        }
    }
    SCC scc(g);
    REP(i,N){
        if(scc.isSame(t[i],f[i])){
            cout<<"Impossible"<<endl;
            return 0;
        }
    }
    REP(i,N){
        cout<<U[i][0];
        if(scc.comp[t[i]]<scc.comp[f[i]])cout<<U[i][1]<<" ";
        else cout<<" "<<U[i][1];        
        cout<<U[i][2]<<endl;
    }

    return 0;
}
0