結果

問題 No.274 The Wall
ユーザー NOSSNOSS
提出日時 2019-09-23 11:04:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 3,689 bytes
コンパイル時間 2,402 ms
コンパイル使用メモリ 186,888 KB
実行使用メモリ 132,388 KB
最終ジャッジ日時 2023-09-04 02:43:15
合計ジャッジ時間 4,446 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 165 ms
68,956 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 459 ms
132,388 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 81 ms
28,500 KB
testcase_17 AC 79 ms
28,444 KB
testcase_18 AC 82 ms
29,396 KB
testcase_19 AC 14 ms
4,380 KB
testcase_20 AC 16 ms
4,376 KB
testcase_21 AC 16 ms
4,384 KB
testcase_22 AC 18 ms
4,376 KB
testcase_23 AC 18 ms
4,380 KB
testcase_24 AC 18 ms
4,376 KB
testcase_25 AC 17 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct SCC {
    int N;
    vector<vector<int> > g, gr, g2i, t;
    vector<bool> visited;
    vector<int> i2g;
    stack<int> order;

    SCC(){}
    SCC(int n){init(n);}
    void init(int n){
        N = n;
        g.clear();
        g.resize(n);
        gr.clear();
        gr.resize(n);
        visited.resize(n);
        i2g.resize(n);
    }
    void add_edge(int u, int v) {
        g[u].emplace_back(v);
        gr[v].emplace_back(u);
    }

    void dfs(int x) {
        if (visited[x]) return;
        visited[x] = true;
        for (int i : g[x]) dfs(i);
        order.push(x);
    }

    void rdfs(int x, int k) {
        if (i2g[x] != -1) return;
        i2g[x] = k;
        for (int i : gr[x]) rdfs(i, k);
    }

    void build() {
        fill(visited.begin(), visited.end(), false);
        fill(i2g.begin(), i2g.end(), -1);
        for (int i = 0; i < N; i++) dfs(i);
        int p = 0;
        while (!order.empty()) {
            rdfs(order.top(), p++);
            order.pop();
        }
        g2i.clear();
        g2i.resize(p);
        for(int i=0;i<N;i++){
            g2i[i2g[i]].push_back(i);
        }
        t.resize(p);
        for(int i=0;i<N;i++){
            for (auto &to : g[i]) {
                int x = i2g[i], y = i2g[to];
                if (x == y) continue;
                t[x].push_back(y);
            }
        }
        for(int i=0;i<p;i++){
            sort(t[i].begin(), t[i].end());
            t[i].erase(unique(t[i].begin(), t[i].end()),t[i].end());
        }
    }
};

struct TwoSAT {
    int N;
    SCC scc;
    vector<int> ls;
    TwoSAT(){}
    TwoSAT(int n):N(n),scc(n*2){}
    int neg(int a){return (a+N)%(N*2);}
    void add_edge(int a, int b){
        scc.add_edge(a,b);
    }
    void add_if(int a, int b){
        // a -> b <=> !b -> !a
        add_edge(a,b);
        add_edge(neg(b),neg(a));
    }
    void add_iff(int a, int b){
        // (a <=> b) <=> a -> b and b -> a
        add_edge(a,b);
        add_edge(b,a);
    }
    void add_or(int a, int b){
        // a or b <=> !a -> b and !b -> a
        add_if(neg(a),b);
    }
    void add_nand(int a, int b){
        // a nand b <=> a -> !b and b -> !a
        add_if(a, neg(b));
    }
    void set_true(int a){
        // a <=> !a -> a
        add_edge(neg(a), a);
    }
    void set_false(int a){
        // !a <=> a -> !a
        add_edge(a, neg(a));
    }
    bool build(){
        scc.build();
        bool ok = true;
        for(int i=1;i<N;i++){
            ok &= scc.i2g[i] != scc.i2g[neg(i)];
        }
        if(ok){
            for(int i=1;i<N;i++){
                if(scc.i2g[i] > scc.i2g[i+N]){
                    ls.push_back(i);
                }
                else{
                    ls.push_back(neg(i));
                }
            }
        }
        return ok;
    }
};

int main(){
    int n, m;
    cin >> n >> m;
    TwoSAT ts(n);
    vector<int> l(n), r(n);
    for(int i=0;i<n;i++){
        cin >> l[i] >> r[i];
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(max(l[i],l[j]) <= min(r[i],r[j])){
                ts.add_nand(i, j);
            }
            if(max(m-r[i]-1,l[j]) <= min(m-l[i]-1,r[j])){
                ts.add_nand(ts.neg(i), j);
            }
            if(max(l[i],m-r[j]-1) <= min(r[i],m-l[j]-1)){
                ts.add_nand(i, ts.neg(j));
            }
            if(max(m-r[i]-1,m-r[j]-1) <= min(m-l[i]-1,m-l[j]-1)){
                ts.add_nand(ts.neg(i), ts.neg(j));
            }
        }
    }
    cout << (ts.build() ? "YES" : "NO") << endl;
    return 0;
}
0