求助,对照题解和Udebug检查过了输出格式,为什么这道题还是过不去?
查看原帖
求助,对照题解和Udebug检查过了输出格式,为什么这道题还是过不去?
84987
LJY_ljy楼主2022/7/11 13:44

RT,

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define MAXN 110
using namespace std;

int T, n, color[MAXN], cnt, cha[MAXN];
bool G[MAXN][MAXN], dp[MAXN][MAXN];
int ans[MAXN][MAXN];
vector<int> team[MAXN][2], Ans[2];

inline int read() {
    register int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

inline bool dfs(int x, int co) {
    team[cnt][co].push_back(x);
    color[x] = co;
    for (int i = 1; i <= n; i++) {
        if (x != i && !(G[x][i] && G[i][x])) {
            if (color[i] == 0) dfs(i, 3 - co);
            else if (color[i] == co) return false;
        }
    }
    return true;
}

inline bool illegal() {
    for (int i = 1; i <= n; i++) {
        if (!color[i]) {
            cnt++;
            if (!dfs(i, 1)) return false;
            cha[cnt] = team[cnt][1].size() - team[cnt][2].size();
        }
    }
    return true;
}

inline void out_put(int dif) {
    int t = dif;
    for (int i = cnt; i >= 1; i--) {
        if (ans[i][t + n] == 1) {
            for (int u = 0; u < team[i][1].size(); u++) Ans[0].push_back(team[i][1][u]);
            for (int u = 0; u < team[i][2].size(); u++) Ans[1].push_back(team[i][2][u]);
            t -= cha[i];
        } else if (ans[i][t + n] == 2) {
            for (int u = 0; u < team[i][1].size(); u++) Ans[1].push_back(team[i][1][u]);
            for (int u = 0; u < team[i][2].size(); u++) Ans[0].push_back(team[i][2][u]);
            t += cha[i];
        }
    }
    printf("%d", Ans[0].size());
    for (int u = 0; u < Ans[0].size(); u++) printf(" %d", Ans[0][u]);
    printf("\n%d", Ans[1].size());
    for (int u = 0; u < Ans[1].size(); u++) printf(" %d", Ans[1][u]);
    printf("\n");
}

int main() {
    T = read();
    for (int y = 1; y <= T; y++) {
        memset(G, false, sizeof(G));
        memset(color, 0, sizeof(color));
        memset(cha, 0, sizeof(cha));
        memset(dp, false, sizeof(dp));
        memset(ans, 0, sizeof(ans));
        Ans[0].clear(); Ans[1].clear();
        for (int i = 1; i <= cnt; i++) {
            for (int j = 1; j <= 2; j++)
                team[i][j].clear();
        }
        cnt = 0;
        n = read();
        for (int i = 1; i <= n; i++) {
            while (true) {
                int x = read();
                if (x == 0) break;
                G[i][x] = true;
            }
        }
        if (n == 1 || !illegal()) {
            printf("No solution\n");
            if (y != T) printf("\n");
        } else {
            dp[0][n] = true;
            for (int i = 1; i <= cnt; i++) {
                for (int j = -n; j <= n; j++) {
                    if (dp[i - 1][j + n]) {
                        ans[i][j + cha[i] + n] = 1;
                        ans[i][j - cha[i] + n] = 2;
                        dp[i][j + cha[i] + n] = true;
                        dp[i][j - cha[i] + n] = true;
                    }
                }
            }
            for (int i = 0; i <= n; i++) {
                if (dp[cnt][i + n]) {
                    out_put(i);
                    break;
                }
                if (dp[cnt][-i + n]) {
                    out_put(-i);
                    break;
                }
            }
            if (y != T) printf("\n");
        }
    }
    return 0;
}

/*
2
5
3 4 5 0
1 3 5 0
2 1 4 5 0
2 3 5 0
1 2 3 4 0

5
2 3 5 0
1 4 5 3 0
1 2 5 0
1 2 3 0
4 3 2 1 0

No solution
3 1 3 5
2 2 4

*/


p.s Udebug上第二组数据是错的,题目中说了N >= 2

2022/7/11 13:44
加载中...