QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143378#6502. Disjoint Set Union275307894aWA 134ms3824kbC++142.0kb2023-08-21 09:54:352023-08-21 09:54:39

Judging History

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

  • [2023-08-21 09:54:39]
  • 评测
  • 测评结果:WA
  • 用时:134ms
  • 内存:3824kb
  • [2023-08-21 09:54:35]
  • 提交

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],Bg[N],Df[N],Dh;vector<int> S[N];
vector<tuple<int,int,int> > ans;
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){ans.emplace_back(2,x,y);x=find(x);y=find(y);if(x^y) a[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++) if(a[i]^b[i]) FIND(i);
	for(i=1;i<=n;i++) if(a[i]^b[i]) S[a[i]].emplace_back(b[i]),In[b[i]]++;
	queue<int> q;
	for(i=1;i<=n;i++) if(!In[i]) q.emplace(i);
	Dh=0;
	while(!q.empty()){
		int x=q.front();q.pop();Bg[x]=++Dh;Df[Dh]=x;
		for(int i:S[x]) !--In[i]&&(q.emplace(i),0);
	} 
	if(Dh^n) {puts("NO");return;}
	for(i=1;i<=Dh;i++){
		int x=Df[i],p=-1;for(int j:S[x]) if(p==-1) p=j;else if(Bg[j]<Bg[p]) p=j;
		if(~p) unite(x,p);
		for(j=1;j<=n;j++) if(a[j]^b[j]) FIND(j);
		// 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",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: 100
Accepted
time: 1ms
memory: 3820kb

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
2
1 1
2 1 2
YES
11
1 2
1 3
1 4
2 3 2
1 2
1 3
1 4
1 2
1 3
2 2 1
1 3
YES
14
1 1
1 2
1 3
1 4
2 1 2
1 2
1 3
1 4
2 2 3
1 3
1 4
2 3 4
1 4
2 4 5
NO
YES
25
1 2
1 3
1 4
1 5
1 6
1 2
1 3
1 4
1 5
1 6
2 6 2
1 2
1 3
1 4
1 5
2 2 5
1 2
1 3
1 4
1 5
2 5 4
1 2
1 4
2 4 1
1 2

result:

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

Test #2:

score: -100
Wrong Answer
time: 134ms
memory: 3824kb

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
4
1 1
1 5
2 1 2
1 5
YES
0
YES
15
1 2
1 3
1 4
1 5
2 2 4
1 3
1 4
1 5
2 3 4
1 4
1 5
1 4
1 5
2 4 1
1 5
YES
6
1 3
1 5
1 3
1 5
2 3 2
1 5
YES
0
YES
5
1 1
1 2
2 1 5
1 2
2 2 3
YES
9
1 2
1 3
1 5
2 2 4
1 3
1 5
2 3 1
1 5
2 5 4
YES
5
1 5
1 5
1 5
1 5
2 5 2
YES
3
1 2
1 2
2 2 3
YES
8
1 1
1 3
1 5
2 1 2
1 3
1 5
2...

result:

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