题意:使相连的两点都不能为黑色,求所染的色中黑色的数目最大
思路:先将所有点都染为白色,然后再进行染黑色,如果染黑色,则进行下一次递归时,要记得数目num+1,如果没有的话,就不要+1;
#include<stdio.h>
#include<string.h>
#define N 105
int color[N], black[N];
int map[N][N];
int Max, n, a, b, num;
int dfs(int x, int cnt){
int flag = 1;
if (cnt > Max){
Max = cnt;
for(int i = 1; i <= n; i++)
black[i] = color[i];
}
if (cnt + n - x + 1 <= Max)
return 0;
for(int i = 1; i <= n; i++)
if (map[x][i] == 1 && color[i] == 1){
flag = 0;
break;
}
if (flag){
color[x] = 1;
dfs(x + 1, cnt + 1);
color[x] = 0;
}
dfs(x + 1, cnt);
return 0;
}
int main(){
int cas, m;
scanf("%d", &cas);
while (cas--){
scanf("%d%d", &n, &m);
memset(map, 0, sizeof(map));
memset(color, 0, sizeof(color));
memset(black, 0, sizeof(black));
for(int i = 0; i < m; i++){
scanf("%d%d", &a, &b);
map[a][b] = 1;
map[b][a] = 1;
}
Max = 0, num = 0;
dfs(1, 0);
printf("%d\n", Max);
for(int i = 1; i <= n; i++){
if (black[i]){
++num;
printf("%d",i);
if (num == Max)
printf("\n");
else
printf(" ");
}
}
}
return 0;
}