回溯,枚举所有点的黑白情况,然后判断可行性。
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
int map[102][102], node[102], save[101], n, k, cct;
int judge(int cur)
{
for(int i = 1; i <= n; i++)
if(map[cur][i] && node[i])
return 0;
return 1;
}
void DFS(int cur)
{
if(cur == n + 1)
{
int ct = 0;
for(int i = 1; i <= n; i++)
if(node[i])
++ct;
if(ct > cct)
{
cct = ct;
for(int i = 1, j = 0; i <= n; i++)
if(node[i])
{
save[j] = i;
++j;
}
}
return ;
}
node[cur] = 0;
DFS(cur + 1);
node[cur] = 1;
if(judge(cur))
DFS(cur + 1);
node[cur] = 0;
}
int main()
{
#ifdef test
freopen("sample.txt", "r", stdin);
#endif
int m, x, y;
scanf("%d", &m);
while(m--)
{
cct = 0;
scanf("%d%d", &n, &k);
memset(map, 0, sizeof(map));
for(int i = 0; i < k; i++)
{
scanf("%d%d", &x, &y);
map[x][y] = map[y][x] = 1;
}
DFS(1);
printf("%d\n%d", cct, save[0]);
for(int i = 1; i < cct; i++)
printf(" %d", save[i]);
puts("");
}
return 0;
}