Illumination

You inherited a haunted house. Its floor plan is an nn-by-nn square grid with ll lamps in fixedlocations and no interior walls. Each lamp can either illuminate its row or its column, but not bothsimultaneously. The illumination of each lamp extends by rr squares in both directions, so a lampunobstructed by an exterior wall of the house can illuminate as many as 2r+12r + 1 squares.

If a square is illuminated by more than one lamp in its row, or by more than one lamp in its column,the resulting bright spot will scare away ghosts forever, diminishing the value of your property. Isit possible for all lamps to illuminate a row or column, without scaring any ghosts? Note that asquare illuminated by two lamps, one in its row and the other in its column, will not scare awaythe ghosts.

Input

The first line of input contains three positive integers, nn, rr and ll(1n,r,l1,000)(1 ≤ n, r, l ≤ 1,000).

Each of the next ll lines contains two positive integers rir_i and cic_i (1ri,cin)(1 ≤ r_i, c_i ≤ n), indicating thatthere is a lamp in row ri and column cic_i.

It is guaranteed that all lamps are in distinct locations.

Output

Print, on a single line, YES if it is possible to illuminate all lamps as stated above; otherwise, print NO.

樣例輸入1
3 2 5
1 1
1 3
3 1
3 3
2 2
樣例輸出1
YES
樣例輸入2
3 2 6
1 1
1 2
1 3
3 1
3 2
3 3
樣例輸出2
NO

對於處於相同行的燈iijj,如果分別位於cic_i列和cjc_j列,且兩個燈在行方向上能夠相互影響到,即cicj2r|c_i-c_j|≤2·r,則若燈ii在行方向上,則燈jj在列方向上;對於處於同列的燈同理。
這就是圖論上的2-SAT問題。

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e3 + 10, M = 1e6 + 10;
vi col[N], row[N];
map<pii, int> id;
int head[N], ver[M << 1], Next[M << 1], tot;
stack<int> s;
bool ins[N];
int c[N], dfn[N], low[N], cnt, num;
int n, k, m;

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

inline void build() {
    repi(i, 1, n) if (row[i].size() >= 2) {
            int l = 0, r = 0;
            repi(j, 0, row[i].size() - 1) {
                while (row[i][j] - row[i][l] > 2 * k)l++;
                while (r + 1 < row[i].size() && row[i][r + 1] - row[i][j] <= 2 * k) r++;
                repi(t, l, r) if (t != j)add(id[{i, row[i][j]}], id[{i, row[i][t]}] + m);
            }
        }
    repi(i, 1, n) if (col[i].size() >= 2) {
            int l = 0, r = 0;
            repi(j, 0, col[i].size() - 1) {
                while (col[i][j] - col[i][l] > 2 * k)l++;
                while (r + 1 < col[i].size() && col[i][r + 1] - col[i][j] <= 2 * k)r++;
                repi(t, l, r) if (t != j)add(id[{col[i][t], i}] + m, id[{col[i][j], i}]);
            }
        }
}

void tarjain(int x) {
    dfn[x] = low[x] = ++num;
    s.push(x);
    ins[x] = true;
    for (int i = head[x]; i; i = Next[i])
        if (!dfn[ver[i]]) {
            tarjain(ver[i]);
            low[x] = min(low[x], low[ver[i]]);
        } else if (ins[ver[i]])
            low[x] = min(low[x], dfn[ver[i]]);
    if (dfn[x] == low[x]) {
        cnt++;
        int y;
        do {
            y = s.top();
            s.pop();
            ins[y] = false;
            c[y] = cnt;
        } while (x != y);
    }
}

inline void solve() {
    bool flag = true;
    for (int i = 1; i <= m; i++)
        if (c[i] == c[i + m]) {
            flag = false;
            break;
        }
    puts(flag ? "YES" : "NO");
}

int main() {
    n = qr(), k = qr(), m = qr();
    repi(i, 1, m) {
        int x = qr(), y = qr();
        id[{x, y}] = i;
        row[x].pb(y), col[y].pb(x);
    }
    repi(i, 1, n) {
        sort(row[i].begin(), row[i].end());
        sort(col[i].begin(), col[i].end());
    }
    build();
    repi(i, 1, 2 * m)if (!dfn[i])tarjain(i);
    solve();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章