QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143150#6502. Disjoint Set UnionqzezTL 0ms3696kbC++142.6kb2023-08-20 18:31:262023-08-20 18:31:28

Judging History

你现在查看的是最新测评结果

  • [2023-08-20 18:31:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3696kb
  • [2023-08-20 18:31:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=1e3+10;
int T,n,fa[N],a[N],col[N],p[N];
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
int Find(int x){
	return fa[x]==x?x:Find(fa[x]);
}
vector<tuple<int,int,int> >ans;
void merge(int x,int y){
	ans.push_back({2,x,y});
	x=find(x),y=find(y);
	if(x^y)fa[x]=y;
}
int k,in[N],id[N];
vector<int>to[N];
bool topo(){
	queue<int>q;
	for(int i=1;i<=n;i++){
		if(!in[i])q.push(i);
	}
	k=0;
	for(int u;!q.empty();){
		id[++k]=u=q.front(),q.pop();
		for(int v:to[u]){
			if(!--in[v])q.push(v);
		}
	}
	return k==n;
}
void add(int u,int v){
	if(u==v)return;
	// debug(u,v);
	to[u].push_back(v),in[v]++;
}
bool get(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&fa[i]);
		if(fa[i]^i)add(fa[i],i);
	}
	for(int i=1;i<=n;i++){
		int u=i;
		for(int j=1;j<=n;j++)u=fa[u];
		if(fa[u]!=u)return 0;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(a[i]^i)add(Find(a[i]),Find(i));
	}
	// if(T==1e5-136){
	// 	cout<<n<<endl<<ary(fa,1,n)<<endl<<ary(a,1,n)<<endl;
	// 	exit(0);
	// }
	if(!topo())return 0;
	// debug("OK1");
	for(int i=1;i<=n;i++){
		col[i]=i;
		for(int j=1;j<=n;j++){
			if(a[col[i]]==col[i])break;
			col[i]=a[col[i]];
		}
		if(a[col[i]]^col[i])return 0;
	}
	// for(int i=1;i<=n;i++)debug(ary(anc[i],1,n));
	// debug(ary(id,1,n));
	// debug(ary(col,1,n));
	for(int i=n,u;u=id[i],i>=1;i--){
		if(fa[u]==u)
			for(int j=i+1,v;v=id[j],j<=n;j++){
				if(col[u]==col[v]&&fa[v]==v)merge(v,u);
			}
		for(int v=1;v<=n;v++){
			if(fa[v]!=a[v]&&fa[v]!=v&&fa[v]!=u&&fa[fa[v]]==u){
				merge(v,u);
			}
		}
		// debug(u,ary(fa,1,n));
	}
	// debug("OK2");
	for(int i=1;i<=n;i++)if(fa[i]^a[i])return 0;
	// if(T>10)return 1;
	puts("YES");
	printf("%d\n",(int)ans.size());
	for(auto x:ans){
		if(get<0>(x)==1)printf("%d %d\n",get<0>(x),get<1>(x));
		else printf("%d %d %d\n",get<0>(x),get<1>(x),get<2>(x));
	}
	return 1;
}
void clr(){
	ans.clear();
	for(int i=1;i<=n;i++)to[i].clear(),in[i]=0;
	// for(int i=1;i<=n;i++)fill(anc[i]+1,anc[i]+1+n,0);
}
int main(){
	for(scanf("%d",&T);T--;debug(T),clr())if(!get())puts("NO");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3696kb

input:

5
3
1 2 3
2 2 3
4
1 2 3 3
1 1 1 2
5
1 2 3 4 5
2 3 4 5 5
5
1 1 1 1 1
1 2 3 4 5
6
1 2 2 4 5 6
1 1 5 1 4 2

output:

YES
1
2 1 2
YES
4
2 3 2
2 4 2
2 2 1
2 3 1
YES
4
2 1 2
2 2 3
2 3 4
2 4 5
NO
YES
7
2 6 2
2 2 5
2 3 5
2 5 4
2 2 4
2 4 1
2 2 1

result:

ok good! (YES count = 4, NO count = 1) (5 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

100000
5
1 2 1 1 1
2 2 1 1 2
5
3 2 3 4 1
3 2 3 4 1
5
1 2 3 4 3
1 4 4 1 1
5
1 2 3 5 3
1 2 2 5 2
5
5 2 3 5 5
5 2 3 5 5
5
1 2 3 4 5
5 3 3 4 5
5
1 2 3 4 5
1 4 1 4 4
5
1 2 3 1 5
1 2 3 1 2
5
1 2 3 3 1
1 3 3 3 1
5
1 2 3 4 3
2 2 4 4 4
5
1 2 2 4 5
5 2 2 4 5
5
1 2 1 4 5
5 2 5 5 5
5
1 2 3 4 5
1 2 5 5 1
5
1 4 3...

output:

YES
2
2 1 2
2 5 2
YES
0
YES
7
2 3 2
2 5 2
2 2 4
2 3 4
2 5 4
2 4 1
2 5 1
YES
2
2 3 2
2 5 2
YES
0
YES
2
2 1 5
2 2 3
YES
4
2 5 2
2 2 4
2 5 4
2 3 1
YES
1
2 5 2
YES
1
2 2 3
YES
3
2 3 4
2 5 4
2 1 2
YES
1
2 1 5
YES
4
2 4 1
2 1 5
2 3 5
2 4 5
YES
4
2 4 3
2 3 5
2 4 5
2 5 1
YES
5
2 4 3
2 2 3
2 3 1
2 2 1
2 5 1
...

result: