QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143098#6502. Disjoint Set UnionqzezWA 1ms3740kbC++142.0kb2023-08-20 16:37:282023-08-20 16:37:30

Judging History

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

  • [2023-08-20 16:37:30]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3740kb
  • [2023-08-20 16:37:28]
  • 提交

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];
int find(int x){
	return fa[x]==x?x:fa[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]){
			// anc[u][v]=1;
			// for(int i=1;i<=n;i++){
			// 	anc[i][v]|=anc[i][u];
			// }
			if(!--in[v])q.push(v);
		}
	}
	return k==n;
}
bool get(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&fa[i]);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(a[i]^i)to[a[i]].push_back(i),in[i]++;
	}
	if(!topo())return 0;
	// debug("OK1");
	// for(int i=1;i<=n;i++)debug(ary(anc[i],1,n));
	// debug(ary(id,1,n));
	for(int i=n,u;u=id[i],i>=1;i--){
		if(fa[u]^u)continue;
		for(int v:to[u]){
			merge(v,u);
			for(int w=1;w<=n;w++){
				if(fa[w]==v&&a[w]!=v)merge(w,u);
			}
		}
	}
	// debug("OK2");
	for(int i=1;i<=n;i++)if(fa[i]^a[i])return 0;
	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--;clr())if(!get())puts("NO");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3740kb

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
NO
YES
4
2 1 2
2 2 3
2 3 4
2 4 5
NO
NO

result:

wrong answer you didn't find a solution but jury did (test case 2)