Polycarp and snakes CodeForces - 1185E (并查集优化)

大意: 有至多26种点心, 第一种为$a$, 第二种为$b$, ..., 每种为$1*x$或$x*1$的矩形, 给定$n*m$矩阵, 求是否构成一组合法的点心.

逆序处理, 用并查集优化一下连边, 要注意多组数据并查集必须全部清空, 不然会出错, 比赛时就因为这个, 改了10发, 少了700多分.........本来一眼就看出来是个并查集沙茶题了, 真是血亏.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#include <unordered_map>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
template <class T> void rd(T &x){x=0;bool f=0;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();}if(f)x=-x;}
//head


const int N = 2300;
char s[N][N],t[N][N];
int n,m,clk;
vector<pii> a[300];
int f1[N][N], f2[N][N];
int tim1[N][N], tim2[N][N];
int Find(int tim[], int fa[], int x) {
	if (tim[x]!=clk) {
		fa[x] = x, tim[x] = clk;
	}
	return fa[x]==x?x:fa[x]=Find(tim,fa,fa[x]);
}

void work() {
	++clk;
	scanf("%d%d", &n, &m);
	REP(i,1,n) cin>>s[i]+1;
	REP(i,1,n) REP(j,1,m) t[i][j]='.';
	REP(i,'a','z') a[i].clear();
	int px=-1,py;
	REP(i,1,n) REP(j,1,m) if (isalpha(s[i][j])) { 
		a[s[i][j]].pb(pii(i,j));
		if (px==-1||s[i][j]>s[px][py]) px=i,py=j;
	}
	REP(i,'a','z') if (a[i].size()>=2) {
		sort(a[i].begin(),a[i].end());
		pii L = a[i][0], R = a[i].back();
		a[i].clear();
		a[i].pb(L),a[i].pb(R);
	}
	PER(i,'a','z') if (a[i].size()>=1) {
		int x=a[i][0].x,y=a[i][0].y;
		if (a[i].size()==1) {
			if (t[x][y]=='.') t[x][y]=i;
			continue;
		}
		int xx=a[i][1].x,yy=a[i][1].y;
		if (x==xx) {
			if (yy<y) swap(yy,y);
			for (int d=Find(tim1[x],f1[x],y); d<=yy; d=Find(tim1[x],f1[x],d)) {
				if (t[x][d]=='.') t[x][d]=i;
				f1[x][d] = d+1;
			}
		}
		if (y==yy) {
			if (xx<x) swap(x,xx);
			for (int d=Find(tim2[y],f2[y],x); d<=xx; d=Find(tim2[y],f2[y],d)) {
				if (t[d][y]=='.') t[d][y]=i;
				f2[y][d] = d+1;
			}
		}
	}
	REP(i,1,n) REP(j,1,m) {
		if (s[i][j]!=t[i][j]) {
			puts("NO");
			return;
		}
	}
	puts("YES");
	int ans = 0;
	REP(i,'a','z') if (a[i].size()>=1) {
		ans = max(ans, i);
	}
	if (!ans) puts("0");
	else printf("%d\n",ans-'a'+1);
	REP(i,'a',ans) {
		if (a[i].empty()) {
			printf("%d %d %d %d\n",px,py,px,py);
		}
		if (a[i].size()==1) {
			int x=a[i][0].x,y=a[i][0].y;
			printf("%d %d %d %d\n",x,y,x,y);
		}
		if (a[i].size()==2) {
			int x=a[i][0].x,y=a[i][0].y;
			int xx=a[i][1].x,yy=a[i][1].y;
			printf("%d %d %d %d\n",x,y,xx,yy);
		}
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) work();
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/11056026.html