結果

問題 No.470 Inverse S+T Problem
ユーザー むかでむかで
提出日時 2020-11-05 00:24:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 4,399 bytes
コンパイル時間 3,251 ms
コンパイル使用メモリ 217,440 KB
実行使用メモリ 6,240 KB
最終ジャッジ日時 2023-08-24 01:42:03
合計ジャッジ時間 4,169 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

//-------------------------------------------------
//--Strongly Connected Components
//-------------------------------------------------
class SCC
{
private:
    using SCCGraph = vector<vector<int> >;
    int N,sz;
    SCCGraph G, rG;
    stack<int> st;
    vector<bool> seen;
    vector<int> belong;
    
    void dfs(int v){
        seen[v] = true;
        for(auto u:G[v]){
            if (seen[u]) continue;
            dfs(u);
        }
        st.push(v);
    }
    void rdfs(int v, int k){
        seen[v] = true;
        belong[v] = k;
        for(auto u:rG[v]){
            if (seen[u]) continue;
            rdfs(u,k);
        }
    }
public:
    SCC(int n):N(n),G(n),rG(n),seen(n),belong(n){}
    void add_edge(int u, int v){
        G[u].push_back(v);
        rG[v].push_back(u);
    }
    void build(){
        for(int v=0; v<N; v++) seen[v]=0;
        for(int v=0; v<N; v++)
            if (!seen[v]) dfs(v);
        for(int v=0; v<N; v++) seen[v]=0;
        sz=0;
        while(!st.empty()){
            int v = st.top(); st.pop();
            if (!seen[v]) rdfs(v,sz++);
        }
    }
    vector<vector<int> > get_list(){
        vector<vector<int> > ret(sz);
        for(int v=0; v<N; v++)
            ret[belong[v]].push_back(v);
        return ret;
    }
    int size(){return sz;};
    int operator[](int v){return belong[v];}
};

//-------------------------------------------------
//--Two SAT
//-------------------------------------------------
class TwoSAT
{
private:
    int N;
    SCC scc;
public:
    TwoSAT(int n):scc(n<<1),N(n){}
    void add_if(int x, bool f, int y, bool g){
        scc.add_edge(x<<1|f,y<<1|g);
        scc.add_edge(y<<1|!g,x<<1|!f);
    }
    void add_clause(int x, bool f, int y, bool g){
        scc.add_edge(x<<1|!f,y<<1|g);
        scc.add_edge(y<<1|!g,x<<1|f);
    }
    void add_var(int x, bool f){
        scc.add_edge(x<<1|!f,x<<1|f);
    }
    vector<bool> solve(){
        scc.build();
        vector<bool> ret(N);
        for(int i=0; i<N; i++){
            int u = scc[i<<1|1];
            int v = scc[i<<1];
            if (u > v)
                ret[i] = true;
            else if(u < v)
                ret[i] = false;
            else
                return vector<bool>();
        }
        return ret;
    }
};

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin>>N;
    vector<string> S(N);
    rep(i,N) cin>>S[i];

    if (N>52){
        cout<<"Impossible\n";
        return 0;
    }
    
    using P = pair<string,string>;
    auto cut = [&](const string &s, int f){
        return P(s.substr(0,2-f),s.substr(2-f,1+f));
    };

    TwoSAT ts(N);
    rep(i,N)FOR(j,i+1,N-1){
        rep(f,2)rep(g,2){
            string a,b,c,d;
            tie(a,b) = cut(S[i],f);
            tie(c,d) = cut(S[j],g);
            if (a==c||a==d||b==c||b==d) ts.add_clause(i,1-f,j,1-g);
        }
    }
    auto ans = ts.solve();
    if (ans.empty()){
        cout<<"Impossible\n";
        return 0;
    }
    rep(i,N){
        string a,b;
        tie(a,b) = cut(S[i],ans[i]);
        cout<<a<<" "<<b<<"\n";
    }

    return 0;
}
0