QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#143376 | #6502. Disjoint Set Union | 275307894a | WA | 141ms | 3924kb | C++14 | 1.9kb | 2023-08-21 09:35:21 | 2023-08-21 09:35:25 |
Judging History
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){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++) {
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",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: 3808kb
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 9 1 2 1 3 1 4 2 4 2 1 2 1 3 1 4 2 2 1 1 3 YES 13 2 1 2 1 2 1 3 1 4 1 2 1 3 1 4 2 2 3 1 3 1 4 2 3 4 1 4 2 4 5 NO YES 20 1 2 1 3 1 4 1 5 1 6 2 6 2 1 2 1 3 1 4 1 5 2 3 5 1 2 1 3 1 4 1 5 2 5 4 1 2 1 4 2 2 1 1 2
result:
ok good! (YES count = 4, NO count = 1) (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 141ms
memory: 3924kb
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 8 1 1 1 5 1 1 1 5 1 1 1 5 2 5 2 1 5 YES 0 NO YES 8 1 3 1 5 1 3 1 5 1 3 1 5 2 5 2 1 5 YES 0 YES 3 2 1 5 1 2 2 2 3 YES 10 1 2 1 3 1 5 2 2 4 1 3 1 5 2 3 1 1 5 1 5 2 5 4 YES 4 1 5 1 5 1 5 2 5 2 YES 1 2 2 3 YES 9 2 1 2 1 3 1 5 1 3 1 5 1 3 1 5 2 5 4 1 5 YES 1 2 1 5 YES 7 1 1 1 3 1 4 2 3 5 1 3 1 4 2 4 ...
result:
wrong answer you didn't find a solution but jury did (test case 3)