QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#143371 | #6502. Disjoint Set Union | 275307894a | WA | 1ms | 3832kb | C++14 | 2.0kb | 2023-08-21 09:30:47 | 2023-08-21 09:30:49 |
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){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();
}
详细
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)