結果

問題 No.1640 簡単な色塗り
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-08-07 05:43:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,122 ms / 2,000 ms
コード長 3,976 bytes
コンパイル時間 2,046 ms
コンパイル使用メモリ 185,340 KB
実行使用メモリ 183,308 KB
最終ジャッジ日時 2023-09-12 03:44:47
合計ジャッジ時間 36,264 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 586 ms
155,132 KB
testcase_05 AC 635 ms
152,176 KB
testcase_06 AC 1 ms
4,376 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 578 ms
108,940 KB
testcase_11 AC 468 ms
88,860 KB
testcase_12 AC 392 ms
78,492 KB
testcase_13 AC 1,021 ms
162,456 KB
testcase_14 AC 965 ms
161,248 KB
testcase_15 AC 280 ms
58,496 KB
testcase_16 AC 347 ms
70,776 KB
testcase_17 AC 788 ms
135,276 KB
testcase_18 AC 49 ms
15,696 KB
testcase_19 AC 400 ms
79,216 KB
testcase_20 AC 530 ms
104,476 KB
testcase_21 AC 469 ms
86,752 KB
testcase_22 AC 34 ms
11,212 KB
testcase_23 AC 574 ms
114,616 KB
testcase_24 AC 132 ms
34,564 KB
testcase_25 AC 383 ms
80,588 KB
testcase_26 AC 723 ms
123,632 KB
testcase_27 AC 269 ms
57,264 KB
testcase_28 AC 926 ms
154,596 KB
testcase_29 AC 685 ms
122,848 KB
testcase_30 AC 82 ms
26,816 KB
testcase_31 AC 915 ms
168,316 KB
testcase_32 AC 689 ms
135,256 KB
testcase_33 AC 416 ms
92,896 KB
testcase_34 AC 643 ms
126,656 KB
testcase_35 AC 413 ms
91,920 KB
testcase_36 AC 79 ms
26,292 KB
testcase_37 AC 114 ms
35,540 KB
testcase_38 AC 749 ms
141,588 KB
testcase_39 AC 300 ms
67,044 KB
testcase_40 AC 257 ms
61,164 KB
testcase_41 AC 569 ms
113,688 KB
testcase_42 AC 307 ms
72,492 KB
testcase_43 AC 376 ms
82,640 KB
testcase_44 AC 345 ms
73,508 KB
testcase_45 AC 245 ms
56,976 KB
testcase_46 AC 96 ms
28,944 KB
testcase_47 AC 57 ms
20,980 KB
testcase_48 AC 765 ms
146,936 KB
testcase_49 AC 23 ms
10,068 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 1,107 ms
178,656 KB
testcase_53 AC 1,122 ms
183,308 KB
07_evil_01.txt TLE -
07_evil_02.txt MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.08.07 05:36:45
**/

#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;
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 only_one(const vector<int> &x, const vector<bool> &t) {
        assert(x.size() == t.size());
        int _m = x.size();
        if (_m <= 12) {
            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]);
                }
            }
        }
        int _nn = _n;
        _n += _m*2;
        scc.add_vertex(_m*4);
        _answer.resize(_n);
        for (int i = 0; i < _m; i++) {
            add_clause(_nn+i,false,x[i],!t[i]);
            add_clause(_nn+_m+i,false,x[i],!t[i]);
        }
        for (int i = 1; i < _m; i++) {
            add_clause(_nn+i,false,_nn+i-1,true);
            add_clause(x[i],!t[i],_nn+i-1,true);
            add_clause(_nn+_m+i-1,false,_nn+_m+i,true);
            add_clause(x[i-1],!t[i-1],_nn+_m+i,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.only_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