CF1713E Cross Swapping

https://codeforces.com/contest/1713/problem/E

image

写了个随机合并的并查集,,,没过。。。。。。

反手按秩!!!!!!!!!

#include <bits/stdc++.h>
//#define int long long
#define ID(i,j) ((i-1)*n+j)
#define pb push_back
using namespace std;
const int M=1002,N=(int)(2e6+5);
int n,a[M][M],dep[M],id[2][M],ans[M][M],fa[M*2];

int fd(int x) {
//	cout<<x<<' '<<fa[x]<<'\n';
	return x==fa[x]?x:fd(fa[x]);
}

void solve() {
	srand(time(0));
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) {
			cin>>a[i][j];
			ans[i][j]=0;
		}
	int tot=0;
	for(int i=1;i<=n;i++) id[0][i]=++tot,id[1][i]=++tot;
	for(int i=1;i<=tot;i++) dep[i]=1,fa[i]=i;
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++) {
//			cout<<i<<" "<<j<<'\n';
			if(ans[i][j]||ans[j][i]||ID(i,j)>ID(j,i)) continue ;
			if(i==j) {
				ans[i][i]=a[i][i]; continue ;
			}
			if(a[i][j]==a[j][i]) {
				ans[i][j]=ans[j][i]=a[i][j]; continue ;
			}
			if(a[i][j]>a[j][i]) {
				int px,x,qwq;bool fl=0;
				int py,y,qaq; bool fl2=0;
				x=fd(id[0][i]); px=fa[x];
				qwq=fd(id[1][j]); 
				if(fa[x]!=qwq) {
					if(dep[x]>dep[qwq]) {
						x=qwq; px=fa[x]; qwq=fd(id[0][i]);
					} else if(dep[x]==dep[qwq]) fl=1;
					fa[x]=qwq;
					if(fl) ++dep[qwq];
					if(fd(id[0][i])==fd(id[1][i])||fd(id[0][j])==fd(id[1][j])) {
						fa[x]=px; 
						if(fl) --dep[qwq];
						ans[i][j]=a[i][j]; ans[j][i]=a[j][i]; continue ;
					}
				}
				y=fd(id[0][j]); py=fa[y];
				qaq=fd(id[1][i]);
				if(qaq!=py) {
					if(dep[y]>dep[qaq]) {
						y=qaq; py=fa[y]; qaq=fd(id[0][j]);
					} else if(dep[y]==dep[qaq]) fl2=1;
					fa[y]=qaq;
					if(fl2) ++dep[qaq];
					if(fd(id[0][i])==fd(id[1][i])||fd(id[0][j])==fd(id[1][j])) {
						fa[x]=px; fa[y]=py; 
						if(fl) --dep[qwq];
						if(fl2) --dep[qaq]; 
						ans[i][j]=a[i][j]; ans[j][i]=a[j][i]; continue ;
					}
				}
				ans[i][j]=a[j][i]; ans[j][i]=a[i][j];
			} else {
				int px,x,qwq;bool fl=0;
				int py,y,qaq; bool fl2=0;
				x=fd(id[0][i]); px=fa[x];
				qwq=fd(id[0][j]); 
				if(fa[x]!=qwq) {
					if(dep[x]>dep[qwq]) {
						x=qwq; px=fa[x]; qwq=fd(id[0][i]);
					} else if(dep[x]==dep[qwq]) fl=1;
					fa[x]=qwq;
					if(fl) ++dep[qwq];
					if(fd(id[0][i])==fd(id[1][i])||fd(id[0][j])==fd(id[1][j])) {
						fa[x]=px; 
						if(fl) --dep[qwq];
						ans[i][j]=a[j][i]; ans[j][i]=a[i][j]; continue ;
					}
				}
				y=fd(id[1][j]); py=fa[y];
				qaq=fd(id[1][i]);
				if(qaq!=py) {
					if(dep[y]>dep[qaq]) {
						y=qaq; py=fa[y]; qaq=fd(id[1][j]);
					} else if(dep[y]==dep[qaq]) fl2=1;
					fa[y]=qaq;
					if(fl2) ++dep[qaq];
					if(fd(id[0][i])==fd(id[1][i])||fd(id[0][j])==fd(id[1][j])) {
						fa[x]=px; fa[y]=py; 
						if(fl) --dep[qwq];
						if(fl2) --dep[qaq]; 
						ans[i][j]=a[j][i]; ans[j][i]=a[i][j]; continue ;
					}
				}
				ans[i][j]=a[i][j]; ans[j][i]=a[j][i];
			}
		}
	} 
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++) cout<<ans[i][j]<<' ';
		cout<<'\n';
	}
}

signed main() {
	cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
	int T; cin>>T; while(T--) solve();
	return 0;
}
posted @ 2022-08-08 19:14  FxorG  阅读(57)  评论(0编辑  收藏  举报