计算最大团的结点数量,套用标准模板即可,详细见注释。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 55;
int n; //图中结点数量
int ans; //最大团中结点的数量
int mp[MAXN][MAXN]; //结点之间的连接关系
int select[MAXN]; //当前最大团中的结点
int num; //当前最大团中结点的数量
//深搜,搜索最大团中结点的数量
void DFS(int x)
{
if (x == n)
ans = max(ans, num);
else
{
int flag = 1;
for (int i = 0; i < num; i++)
{ //确保从结点x和当前最大团中的所有结点之间都有连接关系
if (mp[select[i]][x] == 0)
{
flag = 0;
break;
}
}
if (flag) //选择结点x,继续深搜
{
select[num++] = x;
DFS(x + 1);
--num;
}
//不选择结点x,仍有可能得到更大的最大团,继续深搜
if (num + n - 1 - x > ans)
DFS(x + 1);
}
}
int main()
{
while (cin >> n)
{
if (n == 0)
break;
memset(mp, 0, sizeof(mp));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cin >> mp[i][j];
}
memset(select, 0, sizeof(select));
ans = num = 0;
DFS(0);
cout << ans << endl;
}
return 0;
}
继续加油。