1 条题解

  • 0
    @ 2026-2-7 19:15:26
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    using  namespace  std;
    struct  node
    {
        int  y,c,next,other;
    }a[210000];int  last[9000],len,n,st,ed,tl,ji[9000];
    int  cur[21000];
    void  ins(int  x,int  y)
    {
    	len++;
    	a[len].y=y;a[len].c=1;a[len].next=last[x];last[x]=len;
    	len++;
    	a[len].y=x;a[len].c=0;a[len].next=last[y];last[y]=len;
    	a[len].other=len-1;a[len-1].other=len;
    	cur[x]=len-1;cur[y]=len; 
    }
    int  list[9000],head,tail,h[9000];
    bool  bt()
    {
        memset(h,0,sizeof(h));h[st]=1;
        head=1;tail=2;list[head]=st;
        while(head!=tail)
        {
            int  x=list[head];
            for(int  k=last[x];k;k=a[k].next)
            {
                int  y=a[k].y;
                if(a[k].c>0  &&  h[y]==0)
                {
                    h[y]=h[x]+1;
                    list[tail++]=y;
                }
            }
            head++;
        }
        return  h[ed]!=0;
    }
    int  mymin(int  x,int  y){return  x<y?x:y;}
    int  find(int  x,int  f)
    {
        if(x==ed)return  f;
        int  ans=0,t,role=cur[x],k;
        for(k=cur[x];k;k=a[k].next)
        {
            int  y=a[k].y;
            if(a[k].c>0  &&  h[y]==h[x]+1  &&  ans<f)
            {
                ans+=t=find(y,mymin(f-ans,a[k].c));
                a[k].c-=t;a[a[k].other].c+=t;
                cur[x]=a[k].next;
                if(k==0)k=last[x];
                if(x%2==1)ji[x/2+1]=y/2;
                if(ans==f)return  ans;
            }
        }
        for(k=last[x];k!=role;k=a[k].next)
        {
            int  y=a[k].y;
            if(a[k].c>0  &&  h[y]==h[x]+1  &&  ans<f)
            {
                ans+=t=find(y,mymin(f-ans,a[k].c));
                a[k].c-=t;a[a[k].other].c+=t;
                cur[x]=a[k].next;
                if(x%2==1)ji[x/2+1]=y/2;
                if(ans==f)return  ans;
            }
        }
        if(ans==0)h[x]=0;
        return  ans;
    }
    bool  vl[9000];
    int  main()
    {
        scanf("%d",&n);ed=n*n*2;tl=n;
        for(int  i=1;i<=tl;i++)
        {
            for(int  j=i+1;j<=tl;j++)
            {
                int  t=sqrt(i+j);
                if(t*t==i+j)ins(i*2-1,j*2);
            }
            ins(st,i*2-1);ins(i*2,ed);
        }
        int  ans=0;
        while(bt())ans+=find(st,999999999);
        while(tl-ans<=n)
        {
            tl++;ins(st,tl*2-1);ins(tl*2,ed);
            for(int  i=1;i<tl;i++)
            {
                int  t=sqrt(i+tl);
                if(t*t==i+tl)
                {
                    ins(i*2-1,tl*2);
                }
            }
            memset(ji,0,sizeof(ji));
            ans=0;
            for(int  i=1;i<=len;i+=2)
            {
                a[i].c=1;a[a[i].other].c=0;
            }
            while(bt())ans+=find(st,999999999);
        }
        memset(last,0,sizeof(last));len=0;
        tl--;
        for(int  i=1;i<=tl;i++)
        {
            for(int  j=i+1;j<=tl;j++)
            {
                int  t=sqrt(i+j);
                if(t*t==i+j)ins(i*2-1,j*2);
            }
            ins(st,i*2-1);ins(i*2,ed);
        }
        memset(ji,0,sizeof(ji));
        ans=0;
        while(bt())ans+=find(st,999999999);
        printf("%d\n",tl);
        for(int  i=1;i<=tl;i++)
        {
            int  opo[9100],qp=0;
            if(vl[i]==false)
            {
                vl[i]=true;
                opo[++qp]=i;
                int  root=i;
                while(ji[root]!=0)
                {
                    root=ji[root];
                    vl[root]=true;
                    opo[++qp]=root;
                }
                printf("%d",opo[1]);
                for(int  k=2;k<=qp;k++)printf(" %d",opo[k]);
                printf("\n");
            }
        }
        return  0;
    }
    
    • 1

    信息

    ID
    969
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    10
    已通过
    5
    上传者