NightTide @ 2022-08-16 20:50:04
#include<bits/stdc++.h>
#define MAXN 110
using namespace std;
vector<int> blo[MAXN][2];
int n, tot, ans;
int col[MAXN], num[MAXN][2], path[MAXN][MAXN];
bool mapp[MAXN][MAXN], dp[MAXN][MAXN], used[MAXN];
void dfs(int now, int fa, int c){
col[now] = c; num[tot][c]++;
blo[tot][c].push_back(now);
for(int i = 1; i <= n; i++){
if(mapp[now][i] || i == now || i == fa) continue;
if(col[i] == -1) dfs(i, now, !c);
else if(col[i] == c){
printf("No solution\n");
exit(0);
}
}
}
void track_back(int i, int j){
if(i == 0 && j == 0) return ;
for(int k = 0; k < blo[i][path[i][j]].size(); k++) used[blo[i][path[i][j]][k]] = true;
track_back(i - 1, j - num[i][path[i][j]]);
}
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
int x;
while(scanf("%d",&x) && x != 0) mapp[i][x] = true;
}
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if(mapp[i][j] != mapp[j][i]) mapp[i][j] = false;
}
}
memset(col, -1, sizeof(col));
for(int i = 1; i <= n; i++){
if(col[i] == -1){
tot++;
dfs(i, 0, 1);
}
}
dp[0][0] = true;
for(int i = 1; i <= tot; i++){
for(int j = 1; j <= n; j++){
if(j >= num[i][0] && dp[i - 1][j - num[i][0]]){
dp[i][j] = true;
path[i][j] = 0;
}
if(j >= num[i][1] && dp[i - 1][j - num[i][1]]){
dp[i][j] = true;
path[i][j] = 1;
}
}
}
for(int i = n / 2; i >= 0; i--){
if(dp[tot][i]){
ans = i;
break;
}
}
track_back(tot, ans);
printf("%d ",ans);
for(int i = 1; i <= n; i++) if(used[i]) printf("%d ",i); printf("\n");
printf("%d ",n - ans);
for(int i = 1; i <= n; i++) if(!used[i]) printf("%d ",i); printf("\n");
return 0;
}
有 Wrong Relationship
,也有 wrong output format Expected integer, but "No" found
。求助。