QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143111#6502. Disjoint Set UnionqzezWA 1ms3748kbC++142.1kb2023-08-20 16:51:412023-08-20 16:51:43

Judging History

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

  • [2023-08-20 16:51:43]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3748kb
  • [2023-08-20 16:51:41]
  • 提交

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];
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]){
			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;
	for(int i=1;i<=n;i++){
		for(col[i]=i;a[col[i]]^col[i];col[i]=a[col[i]]);
	}
	// debug("OK1");
	// 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)continue;
		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;
	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: 3748kb

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
NO

result:

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