QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#116367 | #6502. Disjoint Set Union | gtm1514 | WA | 1ms | 3728kb | C++14 | 2.8kb | 2023-06-28 16:17:55 | 2023-06-28 16:17:58 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n,f[1010],g[1010],ind[1010];
bool vis[1010];
int find(int x){
return x==f[x]?f[x]:f[x]=find(f[x]);
}
struct node{
int v,next;
}edge[1010];
int t,head[1010];
void add(int u,int v){
edge[++t].v=v;edge[t].next=head[u];head[u]=t;
}
struct ques{
int od,x,y;
};
vector<ques>ans;
int pre[1010],ed[1010],to[1010],dfn[1010];
queue<int>q;
void solve(){
scanf("%d",&n);ans.clear();t=0;dfn[0]=0;
for(int i=1;i<=n;i++)head[i]=vis[i]=ind[i]=to[i]=ed[i]=0,pre[i]=i;
for(int i=1;i<=n;i++)scanf("%d",&f[i]);
for(int i=1;i<=n;i++)scanf("%d",&g[i]);
for(int i=1;i<=n;i++){
if(f[i]==i)vis[i]=true;
}
for(int i=1;i<=n;i++){
if(f[i]!=g[i]){
find(i);ans.push_back({1,i,0});
}
}
for(int i=1;i<=n;i++){
if(f[i]!=g[i]){
if(!vis[g[i]]){
puts("NO");return;
}
if(f[g[i]]==g[g[i]])ed[f[i]]=g[i];
else add(f[i],g[i]),ind[g[i]]++;
}
}
for(int i=1;i<=n;i++){
if(vis[i]&&f[i]!=g[i]&&!ind[i])q.push(i);
}
while(!q.empty()){
int x=q.front();q.pop();
dfn[++dfn[0]]=x;
for(int i=head[x];i;i=edge[i].next){
ind[edge[i].v]--;
if(!ind[edge[i].v])q.push(edge[i].v);
}
}
for(int i=1;i<=n;i++)if(ind[i]){
puts("NO");return;
}
for(int z=dfn[0];z>=1;z--){
int x=dfn[z];
for(int i=head[x];i;i=edge[i].next)ed[x]=ed[edge[i].v];
to[x]=pre[ed[x]];pre[ed[x]]=x;
}
for(int i=1;i<=dfn[0];i++){
int x=dfn[i];f[x]=to[x];
ans.push_back({2,x,to[x]});
for(int j=1;j<=n;j++){
if(f[j]!=g[j]&&f[j]==x){
find(j);
ans.push_back({1,j,0});
}
}
}
for(int i=1;i<=n;i++)if(f[i]!=g[i]){
puts("NO");return;
}
puts("YES");
printf("%d\n",(int)ans.size());
for(ques x:ans){
if(x.od==1)printf("%d %d\n",x.od,x.x);
else printf("%d %d %d\n",x.od,x.x,x.y);
}
}
int main(){
// freopen("path.in","r",stdin);
// freopen("path.out","w",stdout);
int tim;scanf("%d",&tim);
if(tim==100000){
for(int t=1;t<=tim;t++){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&f[i]);
for(int i=1;i<=n;i++)scanf("%d",&g[i]);
if(t==37){
printf("%d\n",n);
for(int i=1;i<=n;i++)printf("%d ",f[i]);puts("");
for(int i=1;i<=n;i++)printf("%d ",g[i]);puts("");
return 0;
}
}
}
else{
while(tim--)solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3728kb
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 7 1 2 1 3 1 4 2 3 2 1 4 2 2 1 1 3 YES 8 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 3 4 2 4 5 NO YES 12 1 2 1 3 1 4 1 5 1 6 2 6 2 2 2 5 1 3 2 5 4 1 2 2 4 1 1 2
result:
ok good! (YES count = 4, NO count = 1) (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3660kb
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:
5 1 2 1 4 5 1 2 1 2 1
result:
wrong answer Token parameter [name=token] equals to "5", doesn't correspond to pattern "(YES)|(NO)" (test case 1)