結果

問題 No.470 Inverse S+T Problem
ユーザー kimiyuki
提出日時 2016-12-19 16:07:20
言語 C++14
(gcc 8.2.0)
結果
AC  
実行時間 29 ms
コード長 3,499 Byte
コンパイル時間 2,365 ms
使用メモリ 8,912 KB
最終ジャッジ日時 2019-06-21 18:21:21

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00sample1.txt AC 2 ms
6,872 KB
00sample2.txt AC 2 ms
6,872 KB
00sample3.txt AC 2 ms
6,872 KB
00sample4.txt AC 3 ms
6,872 KB
10hand01.txt AC 3 ms
6,872 KB
10hand02.txt AC 3 ms
6,868 KB
20randomA00.txt AC 28 ms
6,868 KB
20randomA01.txt AC 29 ms
6,868 KB
20randomA02.txt AC 28 ms
6,872 KB
30randomB00.txt AC 4 ms
6,872 KB
30randomB01.txt AC 3 ms
6,872 KB
30randomB02.txt AC 3 ms
6,868 KB
40randomC00.txt AC 3 ms
6,872 KB
40randomC01.txt AC 4 ms
6,868 KB
40randomC02.txt AC 4 ms
6,872 KB
50randomD00.txt AC 4 ms
6,872 KB
50randomD01.txt AC 3 ms
6,872 KB
50randomD02.txt AC 3 ms
6,872 KB
50randomD03.txt AC 2 ms
6,868 KB
50randomD04.txt AC 4 ms
6,872 KB
50randomD05.txt AC 4 ms
8,912 KB
90special01.txt AC 4 ms
6,868 KB
90special02.txt AC 2 ms
6,872 KB
90special03.txt AC 2 ms
8,912 KB
90special04.txt AC 3 ms
6,868 KB
95challenge01.txt AC 2 ms
6,868 KB
95challenge02.txt AC 3 ms
6,872 KB
95challenge03.txt AC 4 ms
6,872 KB
99_system_test1.txt AC 7 ms
6,868 KB
99_system_test2.txt AC 21 ms
6,868 KB
99_system_test3.txt AC 6 ms
6,872 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
using namespace std;

struct strongly_connected_components {
    static pair<int,vector<int> > decompose(vector<vector<int> > const & g) { // adjacent list
        strongly_connected_components scc(g);
        return { scc.k, scc.c };
    }
private:
    int n;
    vector<vector<int> > to, from;
    explicit strongly_connected_components(vector<vector<int> > const & g) : n(g.size()), to(g), from(n) {
        repeat (i,n) for (int j : to[i]) from[j].push_back(i);
        decompose();
    }
    vector<bool> used;
    vector<int> vs;
    void dfs(int i) {
        used[i] = true;
        for (int j : to[i]) if (not used[j]) dfs(j);
        vs.push_back(i);
    }
    int k; // number of scc
    vector<int> c; // i-th vertex in g is in c_i-th vertex in scc-decomposed g
    void rdfs(int i) {
        used[i] = true;
        c[i] = k;
        for (int j : from[i]) if (not used[j]) rdfs(j);
    }
    void decompose() {
        used.clear(); used.resize(n, false);
        repeat (i,n) if (not used[i]) dfs(i);
        used.clear(); used.resize(n, false);
        k = 0;
        c.resize(n);
        reverse(vs.begin(), vs.end());
        for (int i : vs) if (not used[i]) {
            rdfs(i);
            k += 1;
        }
    }
};

vector<bool> twosat(int n, vector<pair<int, int> > const & cnf) {
    vector<vector<int> > g(2*n);
    auto i = [&](int x) { assert (x != 0 and abs(x) <= n); return x > 0 ? x-1 : n-x-1; };
    for (auto it : cnf) {
        int x, y; tie(x, y) = it; // x or y
        g[i(- x)].push_back(i(y)); // not x implies y
        g[i(- y)].push_back(i(x)); // not y implies x
    }
    vector<int> component = strongly_connected_components::decompose(g).second;
    vector<bool> valuation(n);
    repeat_from (x,1,n+1) {
        if (component[i(x)] == component[i(- x)]) { // x iff not x
            return vector<bool>(); // unsat
        }
        valuation[x-1] = component[i(x)] > component[i(- x)]; // use components which indices are large
    }
    return valuation;
}

int main() {
    int n; cin >> n;
    vector<string> s(n); repeat (i,n) cin >> s[i];
    assert (1 <= n and n <= 100000);
    repeat (i,n) assert (s[i].length() == 3);
    vector<bool> result;
    if (n <= (26*2)*(26*2+1)) {
        //     x_i : U_i = S + TT
        // not x_i : U_i = SS + T
        vector<pair<int, int> > cnf;
        map<string, vector<int> > used;
        repeat (i,n) {
            int x = i + 1;
            used[s[i].substr(0, 1)].push_back(+ x);
            used[s[i].substr(1, 2)].push_back(+ x);
            used[s[i].substr(0, 2)].push_back(- x);
            used[s[i].substr(2, 1)].push_back(- x);
        }
        for (auto it : used) {
            for (int x : it.second) for (int y : it.second) if (x < y) {
                // cerr << "not " << x << " or " << "not " << y << endl;
                cnf.emplace_back(- x, - y); // not x or not y
            }
        }
        result = twosat(n, cnf);
    }
    if (result.empty()) {
        cout << "Impossible" << endl;
    } else {
        repeat (i,n) {
            if (result[i]) {
                cout << s[i][0] << ' ' << s[i][1] << s[i][2] << endl;
            } else {
                cout << s[i][0] << s[i][1] << ' ' << s[i][2] << endl;
            }
        }
    }
    return 0;
}
0