結果

問題 No.470 Inverse S+T Problem
ユーザー tancahn2380tancahn2380
提出日時 2019-06-11 23:41:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,351 bytes
コンパイル時間 2,718 ms
コンパイル使用メモリ 220,392 KB
実行使用メモリ 6,744 KB
最終ジャッジ日時 2023-08-24 01:35:31
合計ジャッジ時間 4,582 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,556 KB
testcase_01 AC 4 ms
6,644 KB
testcase_02 AC 4 ms
6,688 KB
testcase_03 AC 4 ms
6,496 KB
testcase_04 AC 4 ms
6,532 KB
testcase_05 AC 4 ms
6,644 KB
testcase_06 AC 18 ms
6,532 KB
testcase_07 AC 18 ms
6,480 KB
testcase_08 AC 18 ms
6,664 KB
testcase_09 AC 4 ms
6,540 KB
testcase_10 AC 4 ms
6,544 KB
testcase_11 WA -
testcase_12 AC 4 ms
6,548 KB
testcase_13 AC 4 ms
6,492 KB
testcase_14 WA -
testcase_15 AC 4 ms
6,660 KB
testcase_16 AC 4 ms
6,540 KB
testcase_17 AC 4 ms
6,608 KB
testcase_18 AC 4 ms
6,536 KB
testcase_19 AC 4 ms
6,540 KB
testcase_20 AC 4 ms
6,564 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 4 ms
6,548 KB
testcase_25 AC 4 ms
6,492 KB
testcase_26 AC 4 ms
6,576 KB
testcase_27 AC 4 ms
6,544 KB
testcase_28 AC 6 ms
6,496 KB
testcase_29 AC 4 ms
6,476 KB
testcase_30 AC 5 ms
6,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

struct SCC{
private:
    int v;
    vector<vector<int>> G;  //グラフの隣接リスト表現
    vector<vector<int>> rG; //辺の向きを逆にしたグラフ
    vector<int> vs;         //帰りがけ順の並び
    vector<bool> used;      //すでに調べたか
public:
    vector<int> cmp;        //属する競連結成分のトポロジカル順序
    SCC(){

    }
    SCC(int V){
        v = V;
        G.resize(v);
        rG.resize(v);
        used.resize(v, false);
        cmp.resize(v, 0);
    }

    void init(int V){
        v = V;
        G.resize(v);
        rG.resize(v);
        used.resize(v, false);
        cmp.resize(v, 0);
    }

    void add_edge(int f, int t){
        G[f].emplace_back(t);
        rG[t].emplace_back(f);
    }

    void dfs(int cur){
        used[cur] = true;
	    for (int i = 0; i < (int)G[cur].size(); i++) {
		    if (!used[G[cur][i]])dfs(G[cur][i]);
	    }
	    vs.emplace_back(cur);
    }

    void rdfs(int cur, int k) {
	    used[cur] = true;
	    cmp[cur] = k;
	    for (int i = 0; i < (int)rG[cur].size(); i++) {
		    if (!used[rG[cur][i]])rdfs(rG[cur][i], k);
	    }
    }

    int scc() {
	    vs.clear();
	    for (int i = 0; i < v; i++) {
		    if (!used[i])dfs(i);
	    }
        used.assign(v, 0);
	    int k = 0;
	    for (int i = vs.size() - 1; i >= 0; i--) {
		    if (!used[vs[i]])rdfs(vs[i], k++);
	    }
	    return k;
    }
};

struct two_sat{
private:
    int v;
    SCC sc;
public:
    vector<int> val;
    two_sat(int V){
        v = V*2;
        val.resize(v/2);
        sc.init(v);
    }
    void add_edge(int f, int t){
        sc.add_edge((f + v/2)%v, t);
        sc.add_edge((t + v/2)%v, f);
    }
    bool sat(){
        sc.scc();
        for(int i = 0;i < v/2;i++){
            if(sc.cmp[i] == sc.cmp[i + v/2])return false;
        }
        for(int i = 0;i < v/2;i++){
            val[i] = sc.cmp[i] > sc.cmp[i + v/2];
        }
        return true;
    }
};


int n;
string s[101010];

int main(){
    cin >> n;
    REP(i, n){
        cin >> s[i];
    }
    if(n >= 52){
        cout << "Impossible" << endl;
        return 0;
    }

    two_sat ts(n);
    REP(y, n)REP(x, y){
        if(s[y].substr(0, 1)==s[x].substr(0, 1) || s[y].substr(1, 2)==s[x].substr(1, 2)) ts.add_edge(y + n, x + n);
		if(s[y].substr(0, 1)==s[x].substr(2, 1) || s[y].substr(1, 2)==s[x].substr(0, 2)) ts.add_edge(y + n, x);
		if(s[y].substr(2, 1)==s[x].substr(0, 1) || s[y].substr(0, 2)==s[x].substr(1, 2)) ts.add_edge(y, x + n);
		if(s[y].substr(2, 1)==s[x].substr(2, 1) || s[y].substr(0, 2)==s[x].substr(0, 2)) ts.add_edge(y, x);
    }
    if(!ts.sat()){
        cout << "Impossible" << endl;
        return 0;
    }
    REP(i, n){
        if(ts.val[i] == 1)cout << s[i].substr(0, 1) << " " << s[i].substr(1, 2) << endl;
        else cout << s[i].substr(0, 2) << " " << s[i].substr(2, 1) << endl;
    }
}
0