QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143371#6502. Disjoint Set Union275307894aWA 1ms3832kbC++142.0kb2023-08-21 09:30:472023-08-21 09:30:49

Judging History

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

  • [2023-08-21 09:30:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3832kb
  • [2023-08-21 09:30:47]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=1e3+5,M=N*4+5,K=31650,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,a[N],b[N],c[N],d[N],In[N];vector<int> S[N];
vector<tuple<int,int,int> > ans;
int jump(int x,int y){if(x==y) return 1;return a[x]^x?jump(a[x],y):0;}
int find(int x){return a[x]^x?a[x]=find(a[x]):x;}
void FIND(int x){find(x);ans.emplace_back(1,x,0);}
void unite(int x,int y){x=find(x);y=find(y);if(x^y) a[x]=y;ans.emplace_back(2,x,y);}
void Make(int x,int La){d[x]=d[La]+1;for(int i:S[x]) if(i^La) Make(i,x);}
void Solve(){
	int i,j;scanf("%d",&n);ans.clear();
	for(i=1;i<=n;i++) scanf("%d",&a[i]);
	for(i=1;i<=n;i++) scanf("%d",&b[i]);
	for(i=1;i<=n;i++) S[i].clear(),In[i]=0;
	for(i=1;i<=n;i++) {
		for(j=1;j<=n;j++) if(jump(j,b[i])&&b[j]^b[i]) S[i].emplace_back(j),In[j]++;
	}
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++) if(i^j&&jump(j,i)) S[j].emplace_back(i),In[i]++;
	}
	queue<int> q;
	for(i=1;i<=n;i++) if(!In[i]) q.emplace(i);
	while(!q.empty()){
		int x=q.front();q.pop();
		if(a[x]^b[x]) unite(x,b[x]);
		for(j=1;j<=n;j++) if(a[j]^b[j]) FIND(j);
		for(int i:S[x]) !--In[i]&&(q.emplace(i),0);
		// cerr<<x<<'\n';
		// for(j=1;j<=n;j++) cerr<<a[j]<<" \n"[j==n];
	} 
	// for(i=1;i<=n;i++) cerr<<a[i]<<" \n"[i==n];
	for(i=1;i<=n;i++) if(a[i]^b[i]){puts("NO");return;}
	puts("YES");
	printf("%d\n",ans.size());
	for(auto i:ans) if(get<0>(i)^1) printf("2 %d %d\n",1,get<1>(i),get<2>(i));else printf("1 %d\n",get<1>(i));
}
int main(){
	int t;scanf("%d",&t);while(t--) Solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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 1
YES
9
1 2
1 3
1 4
2 1 3
1 2
1 3
1 4
2 1 2
1 3
YES
13
2 1 1
1 2
1 3
1 4
1 2
1 3
1 4
2 1 2
1 3
1 4
2 1 3
1 4
2 1 4
NO
YES
20
1 2
1 3
1 4
1 5
1 6
2 1 6
1 2
1 3
1 4
1 5
2 1 2
1 2
1 3
1 4
1 5
2 1 5
1 2
1 4
2 1 4
1 2

result:

wrong answer f[1] = 1, but expected 2 (test case 1)