結果

問題 No.1640 簡単な色塗り
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-08-07 23:10:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 3,994 bytes
コンパイル時間 2,447 ms
コンパイル使用メモリ 184,784 KB
実行使用メモリ 57,252 KB
最終ジャッジ日時 2023-09-12 04:03:07
合計ジャッジ時間 21,870 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 256 ms
45,452 KB
testcase_05 AC 266 ms
46,892 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 253 ms
31,184 KB
testcase_11 AC 196 ms
27,048 KB
testcase_12 AC 166 ms
23,916 KB
testcase_13 AC 386 ms
43,888 KB
testcase_14 AC 365 ms
43,704 KB
testcase_15 AC 116 ms
16,896 KB
testcase_16 AC 143 ms
20,556 KB
testcase_17 AC 323 ms
37,784 KB
testcase_18 AC 24 ms
5,976 KB
testcase_19 AC 173 ms
23,904 KB
testcase_20 AC 242 ms
29,748 KB
testcase_21 AC 183 ms
26,388 KB
testcase_22 AC 16 ms
5,180 KB
testcase_23 AC 271 ms
33,332 KB
testcase_24 AC 61 ms
11,056 KB
testcase_25 AC 178 ms
24,260 KB
testcase_26 AC 302 ms
36,024 KB
testcase_27 AC 112 ms
16,480 KB
testcase_28 AC 372 ms
43,228 KB
testcase_29 AC 288 ms
34,340 KB
testcase_30 AC 31 ms
9,588 KB
testcase_31 AC 338 ms
51,260 KB
testcase_32 AC 291 ms
42,964 KB
testcase_33 AC 177 ms
30,224 KB
testcase_34 AC 236 ms
40,468 KB
testcase_35 AC 175 ms
28,800 KB
testcase_36 AC 33 ms
9,808 KB
testcase_37 AC 49 ms
12,884 KB
testcase_38 AC 289 ms
41,972 KB
testcase_39 AC 102 ms
21,484 KB
testcase_40 AC 118 ms
19,384 KB
testcase_41 AC 240 ms
34,576 KB
testcase_42 AC 128 ms
22,076 KB
testcase_43 AC 134 ms
27,164 KB
testcase_44 AC 127 ms
24,140 KB
testcase_45 AC 90 ms
19,968 KB
testcase_46 AC 36 ms
10,020 KB
testcase_47 AC 24 ms
8,268 KB
testcase_48 AC 291 ms
48,452 KB
testcase_49 AC 9 ms
5,184 KB
testcase_50 AC 1 ms
4,384 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 487 ms
54,844 KB
testcase_53 AC 489 ms
57,252 KB
07_evil_01.txt AC 1,138 ms
96,316 KB
07_evil_02.txt AC 1,807 ms
143,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.08.07 23:08:20
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;



struct StronglyConnectedComponents {
private:
    vector<vector<int>> g, rg;
    vector<int> order; 
    vector<bool> visited;
    int V;
    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (int u : g[v]) dfs(u);
        order.push_back(v);
    }
    void rdfs(int v, int c) {
        if (comp[v] != -1) return;
        comp[v] = c;
        for (int u : rg[v]) rdfs(u, c);
    }
public:
    vector<vector<int>> Graph; 
    vector<int> comp; 
    StronglyConnectedComponents(int v) : V(v) {
        g.resize(v);
        rg.resize(v);
        visited.resize(v);
        comp.resize(v);
    }
    
    void add_vertex(int n = 1) {
        V += n;
        g.resize(V);
        rg.resize(V);
        visited.resize(V);
        comp.resize(V);
    }
    void add_edge(int from, int to) {
        g[from].push_back(to);
        rg[to].push_back(from);
    }
    void build() {
        Graph = vector<vector<int>>();
        order = vector<int>();
        visited.assign(V, false);
        comp.assign(V, -1);
        for (int i = 0; i < V; i++) dfs(i);
        reverse(order.begin(), order.end());
        int number = 0;
        for (int i = 0; i < V; i++) {
            if (comp[order[i]] == -1) {
                rdfs(order[i], number++);
            }
        }
        Graph.resize(number);
        for (int i = 0; i < V; i++) {
            for (int v : g[i]) {
                if (comp[i] == comp[v]) continue;
                Graph[comp[i]].push_back(comp[v]);
            }
        }
    }
};

struct TwoSAT {
private:
    int _n;
    std::vector<bool> _answer;
    StronglyConnectedComponents scc;
    void at_most_one_naive(const vector<int> &x, const vector<bool> &t) {
        assert(x.size() == t.size());
        int _m = x.size();
        for (int i = 0; i < _m-1; i++) {
            for (int j = i+1; j < _m; j++) {
                add_clause(x[i],!t[i],x[j],!t[j]);
            }
        }
    }
public:
    TwoSAT():_n(0),scc(0){}
    explicit TwoSAT(int n) : _n(n), _answer(n), scc(2*n) {}
    void add_clause(int i, bool f, int j, bool g) {
        assert(0 <= i && i < _n);
        assert(0 <= j && j < _n);
        scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0));
        scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0));
    }
    
    void at_most_one(const vector<int> &x, const vector<bool> &t) {
        assert(x.size() == t.size());
        int _m = x.size();
        if (_m <= 6) {
            at_most_one_naive(x,t);
            return;
        }
        int _nn = _n;
        _n += _m;
        scc.add_vertex(_m*2);
        _answer.resize(_n);
        for (int i = 0; i < _m; i++) {
            add_clause(_nn+i,false,x[i],!t[i]);
            if (i) {
                add_clause(_nn+i,false,_nn+i-1,true);
                add_clause(x[i],!t[i],_nn+i-1,true);
            }
        }
    }
    bool satisfiable() {
        scc.build();
        auto id = scc.comp;
        for (int i = 0; i < _n; i++) {
            if (id[2 * i] == id[2 * i + 1]) return false;
            _answer[i] = id[2 * i] < id[2 * i + 1];
        }
        return true;
    }
    std::vector<bool> answer() { return _answer; }
};
int main() {
    int n;cin >> n;
    TwoSAT ts(n);
    vector<int> a(n),b(n);
    vector<vector<int>> x(n);
    vector<vector<bool>> t(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        x[a[i]-1].push_back(i);
        x[b[i]-1].push_back(i);
        t[a[i]-1].push_back(true);
        t[b[i]-1].push_back(false);
    }
    for (int i = 0; i < n; i++) {
        ts.at_most_one(x[i],t[i]);
    }
    if (!ts.satisfiable()) {
        puts("No");
    }
    else {
        puts("Yes");
        auto ans = ts.answer();
        for (int i = 0; i < n; i++) {
            cout << (ans[i]?a[i]:b[i]) << endl;
        }
    }
    return 0;
}
0