QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116364#6502. Disjoint Set Uniongtm1514Compile Error//C++142.4kb2023-06-28 16:08:402023-06-28 16:08:41

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-28 16:08:41]
  • 评测
  • [2023-06-28 16:08:40]
  • 提交

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 0;
    }
    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);
    while(tim--)solve();
    return 0;
}

Details

answer.code: In function ‘void solve()’:
answer.code:78:27: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
   78 |         puts("NO");return 0;
      |                           ^
answer.code:27:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   27 |     scanf("%d",&n);ans.clear();t=0;dfn[0]=0;
      |     ~~~~~^~~~~~~~~
answer.code:29:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |     for(int i=1;i<=n;i++)scanf("%d",&f[i]);
      |                          ~~~~~^~~~~~~~~~~~
answer.code:30:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |     for(int i=1;i<=n;i++)scanf("%d",&g[i]);
      |                          ~~~~~^~~~~~~~~~~~
answer.code: In function ‘int main()’:
answer.code:90:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   90 |     int tim;scanf("%d",&tim);
      |             ~~~~~^~~~~~~~~~~