結果
| 問題 |
No.274 The Wall
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2019-09-06 04:11:11 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 812 ms / 2,000 ms |
| コード長 | 3,176 bytes |
| コンパイル時間 | 1,199 ms |
| コンパイル使用メモリ | 92,208 KB |
| 実行使用メモリ | 385,024 KB |
| 最終ジャッジ日時 | 2025-03-17 19:02:58 |
| 合計ジャッジ時間 | 4,108 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 23 |
ソースコード
#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;
}
emthrm