結果

問題 No.274 The Wall
ユーザー minatominato
提出日時 2020-08-26 18:43:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 413 ms / 2,000 ms
コード長 4,319 bytes
コンパイル時間 4,189 ms
コンパイル使用メモリ 248,500 KB
実行使用メモリ 131,620 KB
最終ジャッジ日時 2023-09-04 02:53:28
合計ジャッジ時間 6,500 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 150 ms
69,724 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 413 ms
131,620 KB
testcase_12 AC 12 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 11 ms
4,376 KB
testcase_16 AC 149 ms
48,676 KB
testcase_17 AC 137 ms
46,568 KB
testcase_18 AC 146 ms
49,200 KB
testcase_19 AC 19 ms
4,380 KB
testcase_20 AC 21 ms
4,504 KB
testcase_21 AC 23 ms
4,376 KB
testcase_22 AC 24 ms
4,380 KB
testcase_23 AC 24 ms
4,376 KB
testcase_24 AC 24 ms
4,376 KB
testcase_25 AC 24 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct StronglyConnectedComponent {
    //T:強連結成分をトポロジカルソートしたグラフ C:強連結成分の内訳
    vector<vector<int>> G,R,T,C;
    vector<int> vs,used,blg;
    StronglyConnectedComponent() {}
    StronglyConnectedComponent(int N) : G(N), R(N), used(N), blg(N) {}

    void add_edge(int u,int v) {
        G[u].emplace_back(v);
        R[v].emplace_back(u);
    }

    void dfs(int v){
        used[v] = 1;
        for(int u:G[v]) {
            if(!used[u]) dfs(u);
        }
        vs.emplace_back(v);
    }

    void rdfs(int v, int k) {
        used[v] = 1;
        blg[v] = k;
        C[k].emplace_back(v);
        for(int u:R[v]) {
            if(!used[u]) rdfs(u,k);
        }
    }

    int build() {
        int N = G.size();
        for(int v = 0; v < N; v++) {
            if(!used[v]) dfs(v);
        }

        fill(used.begin(),used.end(),0);
        int k = 0;
        for(int i = N-1; i >= 0; i--) {
            if(!used[vs[i]]) {
                T.emplace_back();
                C.emplace_back();
                rdfs(vs[i], k++);
            }
        }

        for(int v = 0; v < N; v++) {
            for(int u:G[v]) {
                if(blg[v] != blg[u]) T[blg[v]].emplace_back(blg[u]);
            }
        }

        for(int i = 0; i < k; i++) {
            sort(T[i].begin(),T[i].end());
            T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
        }
        return k;
    }

    int operator[](int k) const{return blg[k];}
};

struct TwoSat {
    int N;
    StronglyConnectedComponent scc;

    TwoSat(int N) : N(N), scc(N*2) {}

    int negate(int v) {return (N+v)%(N*2);}

    void add_if(int u, int v) {
        // u -> v <=> !v -> !u
        scc.add_edge(u,v);
        scc.add_edge(negate(v),negate(u));
    }
    void add_or(int u, int v) {
        // u or v <=> !u -> v
        add_if(negate(u),v);
    }
    void add_nand(int u, int v) {
        // u nand v <=> u -> !v
        add_if(u,negate(v));
    }
    void set_true(int v) {
        //  v <=> !v ->  v
        scc.add_edge(negate(v),v);
    }
    void set_false(int v) {
        // !v <=>  v -> !v
        scc.add_edge(v,negate(v));
    }
    
    vector<int> build() {
        scc.build();
        vector<int> ret(N);
        for (int i = 0; i < N; ++i) {
            if(scc[i]==scc[N+i]) return {};
            ret[i] = scc[i] > scc[N+i];
        }
        return ret;
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,M; cin >> N >> M;
    vector<int> L(N),R(N);
    rep(i,N) cin >> L[i] >> R[i];

    auto check=[](int l1, int r1, int l2, int r2) {
        if (l1 > l2) {
            swap(l1,l2);
            swap(r1,r2);
        }
        if (r1 >= l2) return false;
        return true;
    };
    TwoSat T(N);
    rep(i,N) {
        rep(j,i) {
            if (!check(L[i],R[i],L[j],R[j])) T.add_nand(i,j);
            if (!check(L[i],R[i],M-1-R[j],M-1-L[j])) T.add_nand(i,j+N);
            if (!check(M-1-R[i],M-1-L[i],L[j],R[j])) T.add_nand(i+N,j);
            if (!check(M-1-R[i],M-1-L[i],M-1-R[j],M-1-L[j])) T.add_nand(i+N,j+N);
        }
    }

    auto ans = T.build();
    cout << (ans.empty() ? "NO" : "YES") << ln;
}
0