题意是:给出n个点已经m组相邻点,给每个点上黑色或者白色,黑色点之间不能相邻,问最多可以给几个点上黑色。
最大独立集就是求一个图中最多的顶点集,使得两两之间都不相邻。最大独立集中点的个数 = 补图中最大团中点的个数 (补图就是原来相邻的变成不相邻,不相邻的变成相邻,但是点和本身之间还是看成不相邻的)
上黑色看成是放到一个集合中去,该集合中的点是互不相邻的,问该集合最多有几个点?所以就是相当于求最大独立集。
#include<cstring>
#include<string>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#define N 150
using namespace std;
int n, m, x, y, tsn, tnn, maxs, T;
int some[N][N], all[N][N], none[N][N], re[N][N], ans[N];
void dfs(int d, int an, int sn, int nn)
{
int u, v;
if (sn == 0 && nn == 0)
{
if (an > maxs)
{
maxs = an;
for (int i = 1; i <= maxs; i++)
ans[i] = all[d][i];
}
}
u = some[d][1];
for (int i = 1; i <= sn; i++)
{
v = some[d][i];
if (re[u][v]) continue;
for (int j = 1; j <= an; j++) all[d+1][j] = all[d][j];
all[d+1][an+1] = v;
tsn = 0;
tnn = 0;
for (int j = 1; j <= sn; j++)
if (re[v][some[d][j]]) some[d+1][++tsn] = some[d][j];
for (int j = 1; j <= nn; j++)
if (re[v][none[d][j]]) none[d+1][++tnn] = none[d][j];
dfs(d+1, an+1, tsn, tnn);
some[d][i] = 0;
none[d][++nn] = v;
}
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
memset(re, 0, sizeof(re));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
{
re[i][j] = 1;
re[j][i] = 1;
}
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
re[x][y] = 0;
re[y][x] = 0;
}
maxs = 0;
for (int i = 1; i <= n; i++) some[1][i] = i;
dfs(1, 0, n, 0);
printf("%d\n", maxs);
for (int i = 1; i < maxs; i++)
printf("%d ", ans[i]);
printf("%d\n", ans[maxs]);
}
return 0;
}