BZOJ4423 AMPPZ2013Bytehattan(并查集)

时间:2021-07-07 22:24:37

  判断网格图中某两点是否被割开,可以将割边视为边区域视为点,转化为可切割这两点的区域是否连通。于是每次判断使两个区域连通后是否会形成环(边界视为连通),若是则说明被两点被割开。并查集维护。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
char getc(){char c=getchar();while (c!='N'&&c!='E') c=getchar();return c;}
#define N 1510
int n,m,fa[N*N],last=;
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int trans(int x,int y)
{
if (x==||x==n||y==||y==n) return ;
return (x-)*(n-)+y;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4423.in","r",stdin);
freopen("bzoj4423.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<=n*n;i++) fa[i]=i;
while (m--)
{
int x=read(),y=read();char c=getc();
if (last==) read(),read(),getc();
else x=read(),y=read(),c=getc();
int p=x-(c=='N'),q=y-(c=='E');
int u=find(trans(p,q)),v=find(trans(x,y));
last=find(u)==find(v);
fa[u]=v;
if (last==) printf("TAK\n");
else printf("NIE\n");
}
return ;
}