結果

問題 No.274 The Wall
ユーザー motimoti
提出日時 2015-09-12 02:14:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 2,839 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 98,488 KB
実行使用メモリ 132,184 KB
最終ジャッジ日時 2023-09-04 02:06:40
合計ジャッジ時間 3,180 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 135 ms
68,904 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 356 ms
132,184 KB
testcase_12 AC 27 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 19 ms
4,380 KB
testcase_16 AC 165 ms
62,064 KB
testcase_17 AC 153 ms
59,112 KB
testcase_18 AC 162 ms
63,908 KB
testcase_19 AC 34 ms
4,380 KB
testcase_20 AC 38 ms
4,376 KB
testcase_21 AC 40 ms
4,376 KB
testcase_22 AC 43 ms
4,376 KB
testcase_23 AC 43 ms
4,376 KB
testcase_24 AC 42 ms
4,376 KB
testcase_25 AC 42 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <cstring>
#include <assert.h>
#include <array>
#include <cstdio>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

const int MAX_V = 6000;

vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> vs;
bool used[MAX_V];
int cmp[MAX_V];

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for(int i=0; i<G[v].size(); i++) {
    if(!used[G[v][i]]) dfs(G[v][i]);
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] =  true;
  cmp[v] = k;
  for(int i=0; i<rG[v].size(); i++) {
    if(!used[rG[v][i]]) rdfs(rG[v][i], k);
  }
}

int V;

int scc() {

  memset(used, 0, sizeof(used));
  vs.clear();
  for(int v=0; v<V; v++) {
    if(!used[v]) dfs(v);
  }

  memset(used, 0, sizeof(used));

  // 逆辺にしたらDAGなら進めないところが、巡回路であれば進めるので、それらを同じ数字でマークできる。
  int k = 0;
  for(int i=(int)vs.size()-1; i>=0; i--) {
    if(!used[vs[i]]) rdfs(vs[i], k++);
  }
  return k;
}

const int MAX_N = 2001;

int N, M;
using tpl = tuple<int, int>;
tpl lr[MAX_N * 2];

bool uncover(tpl a, tpl b) {
  auto&& al = get<0>(a), &&ar = get<1>(a);
  auto&& bl = get<0>(b), &&br = get<1>(b);
  return !(
    (bl <= al && al <= br) || (bl <= ar && ar <= br)
    ||
    (al <= bl && bl <= ar) || (al <= br && br <= ar)
  );
}

int main() {

  cin >> N >> M;
  rep(i, N) {
    int l, r; cin >> l >> r; lr[i] = make_tuple(l, r);
    lr[N+i] = make_tuple(M-1-get<1>(lr[i]), M-1-get<0>(lr[i]));
    //printf("(%d, %d) -> (%d, %d)\n", get<0>(lr[i]), get<1>(lr[i]), get<0>(lr[N+i]), get<1>(lr[N+i]));
  }

  /*
    基本形: p -> q <=> ~q -> ~p <=> ~p v q <=> ~(p ∧ ~q)
    p, q の符号の組み合わせをすべて試す
   */

  rep(i, N) REP(j, i+1, N) {

    // p -> ~q <=> q -> ~p <=> ~p v ~q <=> ~(p ∧ q)
    if(!uncover(lr[i], lr[j])) {
      add_edge(j, N+i);
      add_edge(i, N+j);
    }

    // ~p -> ~q <=> q -> p <=> p v ~q <=> ~(~p ∧ q)
    if(!uncover(lr[N+i], lr[j])) {
      add_edge(N+i, N+j);
      add_edge(j, i);
    }

    // p -> q <=> ~q -> ~p <=> ~p v q <=> ~(p ∧ ~q)
    if(!uncover(lr[i], lr[N+j])) {
      add_edge(i, j);
      add_edge(N+j, N+i);
    }

    // ~p -> q <=> ~q -> p <=> p v q <=> ~(~p ∧ ~q)
    if(!uncover(lr[N+i], lr[N+j])) {
      add_edge(N+i, j);
      add_edge(N+j, i);
    }
  }

  V = 2 * N;

  scc();

  rep(i, N) {
    if(cmp[i] == cmp[N+i]) {
      cout << "NO" << endl;
      return 0;
    }
  }

  cout << "YES" << endl;

  return 0;
}
0