萌新RE求调

P1285 队员分组

guzzqq @ 2022-01-24 20:51:14

#include<bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define req(i, a, b) for(int i = a; i >= b; i--)
#define SIZE 105 
using namespace std;
int isedge[SIZE][SIZE];
int n;
struct node{
    int to, next;
}edge[100005];
int cnt, head[SIZE];
void add(int u, int v){
    edge[++cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
}
vector<int>p[SIZE][5];
int vis[SIZE], flag, num[SIZE][5];
int NUM;
void dfs(int x, int color, int id){
    vis[x] = color;
    p[id][color].push_back(x);
    num[id][color]++;
    for(int i = head[x]; i; i = edge[i].next){
        int v = edge[i].to;
        if(!vis[v])
            dfs(v, 3 - color, id);
        else if(vis[v] == color)flag = 1;
    }
}
int dp[SIZE][SIZE], from[SIZE][SIZE], ans;
void solve(){
    dp[0][0] = 1;
    rep(i, 1, NUM){
        rep(j, 0, n / 2){
            if(j >= num[i][1] && dp[i - 1][j - num[i][1]])
                dp[i][j] = 1, from[i][j] = 1;
            if(j >= num[i][2] && dp[i - 1][j - num[i][2]])
                dp[i][j] = 1, from[i][j] = 2;
        }
    }
}
vector<int>out1;
void out(int step, int tot){
    if(step < 1) return;
    int x = from[step][tot];
    rep(i, 0, p[step][x].size() - 1){
        out1.push_back(p[step][x][i]);
    }
    out(step - 1, tot - num[step][x]);
}
bool tong[SIZE];
int main(){
    scanf("%d",&n);
    rep(i, 1, n){
        int xx;
        while(1){
            scanf("%d", &xx);
            if(xx == 0) break;
            ++isedge[i][xx];
            ++isedge[xx][i];
        }
    }
    rep(i, 1, n){
        rep(j, i + 1, n){
            if(isedge[i][j] < 2)add(i, j), add(j, i);
        }
    }

    rep(i, 1, n){
        if(!vis[i])++NUM, dfs(i, 1, NUM);
        if(flag){
            printf("No solution");
            return 0;
        }
    }

    solve();
    rep(i, 0, n / 2){
        if(dp[NUM][i])ans = i;
    }
    out(NUM, ans);

    sort(out1.begin(), out1.end());
    printf("%d ",out1.size());
    rep(i, 0, out1.size() - 1){
        printf("%d ",out1[i]);
        tong[out1[i]] = 1;
    }
    printf("\n%d ",n - out1.size());
    rep(i, 1, n){
        if(!tong[i]) printf("%d ",i);
    }
    return 0;
}

|