[BZOJ 4423][AMPPZ2013]Bytehattan

Description

题库链接

给出一个 $N\times N$ 的网格图。有 $k$ 次操作,每次会删掉图中的一条边 $(u,v)$ ,你需要回答在删除这条边之后 $u$ 和 $v$ 是否仍然连通。

保证每条边只会删除一次。

$1\leq N\leq 1500$

Solution

对于平面图上支持删边以及维护联通性的问题,在原图上就不太好处理了;考虑构建对偶图。

对偶图的构法是将平面图划分的区域间连边;容易发现对于平面图上的每一条边,在对偶图上都能找到对应的边。

一开始对偶图的边集为空,当删除原图中的一条边时,等价于将对偶图上对应的一条边连起来。若连上这条边后对偶图中构成环,则在原图上这条边连接的两个点已经不连通。

这样在原图上删边就相当于在对偶图上加边,容易用并查集维护。

Code

//It is made by Awson on 2018.2.22
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 1500;
void read(int &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }

int n, m, ans, cnt, a, b, a1, b1, x, y;
int id[N+5][N+5], fa[N*N+5];
char c, c1;

int find(int o) {return fa[o] ? fa[o] = find(fa[o]) : o; }
void work() {
    read(n), read(m); ans = cnt = 1;
    for (int i = 1; i < n; i++) for (int j = 1; j < n; j++) id[i][j] = ++cnt;
    while (m--) {
    read(a), read(b), scanf("%c", &c);
    if (ans) read(a1), read(b1), scanf("%c", &c1); else read(a), read(b), scanf("%c", &c);
    if (c == 'N') {
        if (a == 1) x = find(1), y = find(id[a][b]);
        else if (a == n) x = find(id[a-1][b]), y = find(1);
        else x = find(id[a][b]), y = find(id[a-1][b]);
    }else {
        if (b == 1) x = find(1), y = find(id[a][b]);
        else if (b == n) x = find(id[a][b-1]), y = find(1);
        else x = find(id[a][b]), y = find(id[a][b-1]);
    }
    if (x != y) fa[x] = y, ans = 1; else ans = 0;
    puts(ans ? "TAK" : "NIE");
    }
}
int main() {
    work(); return 0;
}
posted @ 2018-02-22 21:50  NaVi_Awson  阅读(162)  评论(0编辑  收藏  举报