結果

問題 No.274 The Wall
ユーザー 👑 emthrmemthrm
提出日時 2019-09-06 04:11:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 805 ms / 2,000 ms
コード長 3,176 bytes
コンパイル時間 1,148 ms
コンパイル使用メモリ 94,536 KB
実行使用メモリ 385,084 KB
最終ジャッジ日時 2023-09-04 02:41:50
合計ジャッジ時間 4,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 298 ms
168,792 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 805 ms
385,084 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 189 ms
94,568 KB
testcase_17 AC 181 ms
90,636 KB
testcase_18 AC 196 ms
97,708 KB
testcase_19 AC 21 ms
4,376 KB
testcase_20 AC 24 ms
4,380 KB
testcase_21 AC 25 ms
4,376 KB
testcase_22 AC 27 ms
4,376 KB
testcase_23 AC 26 ms
4,380 KB
testcase_24 AC 27 ms
4,376 KB
testcase_25 AC 27 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
/*-------------------------------------------------*/
struct Edge {
  int src, dst;
  Edge(int src, int dst) : src(src), dst(dst) {}
};

struct SCC {
  vector<int> id;
  // vector<vector<int> > vertices;
  // vector<vector<Edge> > comp;

  SCC(const vector<vector<Edge> > &graph_) : graph(graph_) {
    n = graph.size();
    rev_graph.resize(n);
    REP(i, n) for (Edge &e : graph[i]) {
      rev_graph[e.dst].emplace_back(e.dst, e.src);
    }
    used.resize(n);
    id.resize(n);
  }

  int build() {
    fill(ALL(used), false);
    order.clear();
    fill(ALL(id), -1);
    // vertices.clear();
    // comp.clear();
    REP(i, n) {
      if (!used[i]) dfs(i);
    }
    int now = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        // vertices.emplace_back();
        rev_dfs(order[i], now++);
      }
    }
    // comp.resize(now);
    // REP(i, n) for (Edge &e : graph[i]) {
    //   if (id[i] != id[e.dst]) comp[id[i]].emplace_back(id[i], id[e.dst], e.cost);
    // }
    // REP(i, now) sort(ALL(vertices[i]));
    return now;
  }

private:
  int n;
  vector<vector<Edge> > graph, rev_graph;
  vector<bool> used;
  vector<int> order;

  void dfs(int ver) {
    used[ver] = true;
    for (Edge &e : graph[ver]) {
      if (!used[e.dst]) dfs(e.dst);
    }
    order.emplace_back(ver);
  }

  void rev_dfs(int ver, int now) {
    id[ver] = now;
    // vertices[now].emplace_back(ver);
    for (Edge &e : rev_graph[ver]) {
      if (id[e.dst] == -1) rev_dfs(e.dst, now);
    }
  }
};

struct TwoSat {
  TwoSat(int n) : n(n) { graph.resize(n * 2); }

  int negate(int x) { return (n + x) % (n * 2); }

  void add_if(int x, int y) { graph[x].emplace_back(x, y); }

  void add_or(int x, int y) {
    add_if(negate(x), y);
    add_if(negate(y), x);
  }

  void add_nand(int x, int y) { add_or(negate(x), negate(y)); }

  void set_true(int x) { add_if(negate(x), x); }

  void set_false(int x) { set_true(negate(x)); }

  vector<bool> build() {
    SCC scc(graph);
    scc.build();
    vector<bool> res(n);
    REP(i, n) {
      if (scc.id[i] == scc.id[negate(i)]) return {};
      res[i] = scc.id[negate(i)] < scc.id[i];
    }
    return res;
  }

private:
  int n;
  vector<vector<Edge> > graph;
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m; cin >> n >> m;
  vector<int> l(n), r(n); REP(i, n) cin >> l[i] >> r[i];
  TwoSat ts(n);
  REP(i, n) REP(j, i) {
    if (l[i] <= r[j] && l[j] <= r[i]) ts.add_nand(i, j);
    if (l[i] <= m - l[j] - 1 && m - r[j] - 1 <= r[i]) ts.add_nand(i, ts.negate(j));
    if (m - r[i] - 1 <= r[j] && l[j] <= m - l[i] - 1) ts.add_nand(ts.negate(i), j);
    if (m - r[i] - 1 <= m - l[j] - 1 && m - r[j] - 1 <= m - l[i] - 1) ts.add_nand(ts.negate(i), ts.negate(j));
  }
  cout << (ts.build().empty() ? "NO\n" : "YES\n");
  return 0;
}
0