萌新求调(悬赏1关注)

P1285 队员分组

_Catluo_ @ 2023-05-20 17:35:16

18pts


#include<bits/stdc++.h>
using namespace std;
int n;
int to[105][105],asd;
int vis[105];
int c[105];
int num[105];
int f[105][205];
int fi[105];
int last[105][205];
vector<int>qu[2];
void dfs(int x,int color){
    if(vis[x]==1&&c[x]!=color){
        cout<<"No solution";
        exit(0);
    }
    if(vis[x])return;
    vis[x]=1;
    c[x]=color;
    num[asd]+=color*2-1;
    for(int i=1;i<=n;i++){
        if(to[x][i]){
            dfs(i,1-color);
        }
    }
    return;
}
void p(int x,int color){
    if(vis[x]==2)return;
    vis[x]=2;
    qu[color].push_back(x);
    for(int i=1;i<=n;i++){
        if(to[x][i]){
            p(i,1-color);
        }
    }
    return;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        while(1){
            scanf("%d",&x);
            if(x==0)break;
            to[i][x]=1;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<i;j++){
            to[i][j]=(to[i][j]&to[j][i]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<i;j++){
            to[i][j]=to[j][i]=!to[i][j];
        }
    }
    f[0][100]=1;
    for(int i=1;i<=n;i++){
        if(vis[i]==0){
            ++asd;
            fi[asd]=i;
            dfs(i,1);
            for(int j=0;j<=200;j++){
                if(f[asd-1][j]){
                    f[asd][j+num[asd]]=1;
                    last[asd][j+num[asd]]=1;
                    f[asd][j-num[asd]]=1;
                    last[asd][j-num[asd]]=0;
                }
            }
        }
    }
    for(int i=0;i<=n;i++){
        if(f[asd][100-i]==1){
            int now=100-i;
            for(int j=asd;j>=1;j--){
                p(fi[j],last[j][now]);
                if(last[j][now]==1){
                    now=now-num[j];
                }else{
                    now=now+num[j];
                }
            }
            cout<<qu[0].size()<<" ";
            for(int j=0;j<qu[0].size();j++){
                cout<<qu[0][j]<<" ";
            }cout<<endl;
            cout<<qu[1].size()<<" ";
            for(int j=0;j<qu[1].size();j++){
                cout<<qu[1][j]<<" ";
            }cout<<endl;
            return 0;
        }else if(f[asd][100+i]==1){
            int now=100+i;
            for(int j=asd;j>=1;j--){
                p(fi[j],last[j][now]);
                if(last[j][now]==1){
                    now=now-num[j];
                }else{
                    now=now+num[j];
                }
            }
            cout<<qu[0].size()<<" ";
            for(int j=0;j<qu[0].size();j++){
                cout<<qu[0][j]<<" ";
            }cout<<endl;
            cout<<qu[1].size()<<" ";
            for(int j=0;j<qu[1].size();j++){
                cout<<qu[1][j]<<" ";
            }cout<<endl;
            return 0;
        }
    }
    return 0;
} 

by _Catluo_ @ 2023-05-20 19:47:36

发现了,输出没排序


|