結果

問題 No.274 The Wall
ユーザー kyunakyuna
提出日時 2019-06-29 01:28:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 429 ms / 2,000 ms
コード長 2,887 bytes
コンパイル時間 1,080 ms
コンパイル使用メモリ 88,920 KB
実行使用メモリ 131,684 KB
最終ジャッジ日時 2023-09-04 02:39:45
合計ジャッジ時間 2,950 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 155 ms
69,592 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 429 ms
131,684 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 76 ms
26,972 KB
testcase_17 AC 73 ms
26,888 KB
testcase_18 AC 76 ms
27,636 KB
testcase_19 AC 13 ms
4,380 KB
testcase_20 AC 16 ms
4,376 KB
testcase_21 AC 17 ms
4,380 KB
testcase_22 AC 17 ms
4,376 KB
testcase_23 AC 18 ms
4,376 KB
testcase_24 AC 17 ms
4,380 KB
testcase_25 AC 18 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct StronglyConnectedComponents {
    vector<vector<int>> gg, rg;
    vector<int> comp, order, seen;
    StronglyConnectedComponents(int v) : \
    gg(v), rg(v), comp(v, -1), seen(v, 0) { }
    void add_edge(int x, int y) {
        gg[x].emplace_back(y);
        rg[y].emplace_back(x);
    }
    int operator[](int k) { return comp[k]; }
    void dfs(int u) {
        seen[u] = true;
        for (int v: gg[u]) if (!seen[v]) dfs(v);
        order.emplace_back(u);
    }
    void rdfs(int u, int id) {
        comp[u] = id;
        for (int v: rg[u]) if (comp[v] == -1) rdfs(v, id);
    }
    vector<vector<int>> build() {
        for (int u = 0; u < gg.size(); ++u) if (!seen[u]) dfs(u);
        reverse(begin(order), end(order));
        int id = 0;
        for (int u: order) if (comp[u] == -1) rdfs(u, id++);
        vector<vector<int>> g_comp(id);
        set<pair<int, int>> connect;
        for (int u = 0; u < gg.size(); ++u) for (int v: gg[u]) {
            int x = comp[u], y = comp[v];
            if (x == y) continue;
            if (connect.count({x, y})) continue;
            g_comp[x].emplace_back(y);
            connect.emplace(x, y);
        }
        return g_comp;
    }
};

struct TwoSatisfiability : StronglyConnectedComponents {
    int sz;
    TwoSatisfiability(int v) : StronglyConnectedComponents(v + v), sz(v) {}
    void add_if(int u, int v) {     // u -> v <=> ¬v -> ¬u
        add_edge(u, v); add_edge(neg(v), neg(u));
    }
    void add_or(int u, int v) { add_if(neg(u), v); }    //  u or v  <=> ¬u -> v
    void add_nand(int u, int v) { add_if(u, neg(v)); }  // ¬(u ∧ v) <=> u -> ¬v
    void set_true(int v) { add_edge(neg(v), v); }       //  v <=> ¬v -> v
    void set_false(int v) { add_edge(v, neg(v)); }      // ¬v <=>  v -> ¬v
    int neg(int x) { return (x + sz) % (sz * 2); }
    bool solve() {
        build();
        for (int i = 0; i < sz; i++) {
            if ((*this)[i] == (*this)[neg(i)]) return false;
        }
        return true;
    }
};

int main() {
    int n, m; cin >> n >> m;
    vector<int> l(n), r(n);
    for (int i = 0; i < n; i++) cin >> l[i] >> r[i], r[i]++;
    TwoSatisfiability ts(n);
    auto lap = [&](int al, int ar, int bl, int br) {
        return max(al, bl) < min(ar, br);
    };
    #define ___(a, b) a, b
    #define REV(a, b) m - b, m - a
    #define NOT(i) ts.neg(i)
    for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) {
        if (lap(___(l[i], r[i]), ___(l[j], r[j]))) ts.add_nand(i, j);
        if (lap(___(l[i], r[i]), REV(l[j], r[j]))) ts.add_nand(i, NOT(j));
        if (lap(REV(l[i], r[i]), ___(l[j], r[j]))) ts.add_nand(NOT(i), j);
        if (lap(REV(l[i], r[i]), REV(l[j], r[j]))) ts.add_nand(NOT(i), NOT(j));
    }
    cout << (ts.solve() ? "YES" : "NO") << endl;
    return 0;
}
0