結果

問題 No.274 The Wall
ユーザー sekiya9311sekiya9311
提出日時 2017-02-25 16:47:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,048 ms / 2,000 ms
コード長 5,413 bytes
コンパイル時間 1,338 ms
コンパイル使用メモリ 122,088 KB
実行使用メモリ 260,712 KB
最終ジャッジ日時 2023-09-04 02:15:59
合計ジャッジ時間 5,910 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 337 ms
127,464 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1,048 ms
260,712 KB
testcase_12 AC 67 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 40 ms
4,376 KB
testcase_16 AC 417 ms
112,672 KB
testcase_17 AC 389 ms
107,532 KB
testcase_18 AC 418 ms
115,992 KB
testcase_19 AC 76 ms
4,376 KB
testcase_20 AC 86 ms
4,380 KB
testcase_21 AC 90 ms
4,376 KB
testcase_22 AC 97 ms
4,376 KB
testcase_23 AC 96 ms
4,376 KB
testcase_24 AC 96 ms
4,380 KB
testcase_25 AC 95 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
#include <chrono>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

class StronglyConnectedComponentDecomposition {
private:
    std::vector<bool> used;
    std::vector<std::vector<int>> graph, revgraph;
    std::vector<int> comp;
    std::vector<int> sortNode;

    void dfs(int now) {
        this->used[now] = true;
        for (const int e : this->graph[now]) {
            if (!this->used[e]) dfs(e);
        }
        this->sortNode.emplace_back(now);
        return;
    }
    void dfs2(int now, int cnt) {
        this->used[now] = true;
        for (const int e : this->revgraph[now]) {
            if (!this->used[e]) dfs2(e, cnt);
        }
        this->comp[now] = cnt;
        return;
    }
public:
    StronglyConnectedComponentDecomposition() {}
    StronglyConnectedComponentDecomposition(const int n) : used(n, false), graph(n), revgraph(n), comp(n, 0) {}
    void resize(const int n) {
         this->used.resize(n, false);
         this->graph.resize(n);
         this->revgraph.resize(n);
         this->comp.resize(n, 0);
    }
    void addEdge(const int from, const int to) {
        this->graph[from].emplace_back(to);
        this->revgraph[to].emplace_back(from);
        return;
    }
    int solve() {
        std::fill(std::begin(this->used), std::end(this->used), false);
        for (int i = 0; i < (int)this->used.size(); i++) {
            if (!used[i]) dfs(i);
        }
        std::fill(std::begin(this->used), std::end(this->used), false);
        std::reverse(std::begin(this->sortNode), std::end(this->sortNode));
        int res = 0;
        for (const int n : this->sortNode) {
            if (!this->used[n]) dfs2(n, res++);
        }
        return res;
    }
    std::vector<int> getComp() {
        return this->comp;
    }
    bool same(const int a, const int b) {
        return this->comp[a] == this->comp[b];
    }
};
// need SCC Library!!!!
class Two_Sat {
private:
    StronglyConnectedComponentDecomposition scc;
    int sz;
public:
    Two_Sat(const int n) : scc(n * 2), sz(n) {}
    Two_Sat() {}
    // add (a OR b)
    void addLogic(const int a, const bool ab, const int b, const bool bb) {
        this->scc.addEdge(a + (ab ? 0 : this->sz), b + (bb ? this->sz : 0));
        this->scc.addEdge(b + (bb ? 0 : this->sz), a + (ab ? this->sz : 0));
    }
    // (logic 1) AND (logic 2) AND ... (logic N) <- satisfy
    bool calcSatisfy() {
        this->scc.solve();
        for (int i = 0; i < this->sz; i++) {
            if (this->scc.same(i, i + this->sz)) return false;
        }
        return true;
    }
};

const int MAX = 2010;
int N, M;
pair<int, int> lr[MAX][2];

bool check(const pair<int, int> &p1, const pair<int, int> &p2) {
    bool res = false;
    res |= p2.first <= p1.first && p1.first <= p2.second;
    res |= p2.first <= p1.second && p1.second <= p2.second;
    res |= p1.first <= p2.first && p2.first <= p1.second;
    res |= p1.first <= p2.second && p2.second <= p1.second;
    return res;
}

int main(void) {
    cin >> N >> M;
    Two_Sat ts(N);
    for (int i = 0; i < N; i++) {
        int l, r;
        cin >> l >> r;
        lr[i][0] = make_pair(l, r);
        lr[i][1] = make_pair(M - r - 1, M - l - 1);
    }
    //int cnt = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i != j) {
                for (int k = 0; k < 2; k++) {
                    for (int l = 0; l < 2; l++) {
                        if (check(lr[i][k], lr[j][l])) {
                            //cerr << i << " " << k << " " << j << " " <<l << endl;
                            ts.addLogic(i, k, j, l);
                        }
                    }
                }
            }
        }
    }
    //DEBUG(cnt)
    puts(ts.calcSatisfy() ? "YES" : "NO");
}
0