题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5277
题意:给出一个平面图,n个点,m条边,直线边与直线边之间不相交,求最大团的数目。
解题思路:由于平面图,直线边与直线边之间不相交,所以最大团的大小最大为4(即一个三角形内存在一个顶点)
对于最大团为4:先枚举两条不相连的边
对于最大团为3:先枚举一条边,一个点。
以上两种情况会重复3次,故要除以3
对于最大团为2:等于边数
对于最大团为1:等于顶点数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 1005;
struct Edge
{
int x,y;
Edge(){}
Edge(int _x,int _y)
{
x = _x;
y = _y;
}
};
int n,m,ans[5];
bool seg[maxn][maxn];
vector<Edge> vec;
int main()
{
int tmp,u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(seg,0,sizeof(seg));
vec.clear();
for(int i = 1; i <= n; i++)
scanf("%d%d",&tmp,&tmp);
for(int i = 1; i <= m; i++)
{
scanf("%d%d",&u,&v);
seg[u][v] = true;
seg[v][u] = true;
vec.push_back(Edge(u,v));
}
int len = vec.size();
ans[1] = n;
ans[2] = m;
ans[3] = ans[4] = 0;
for(int i = 0; i < len; i++)
for(int j = i + 1; j < len; j++)
{
Edge a = vec[i];
Edge b = vec[j];
if(a.x != b.x && a.x != b.y && a.y != b.x && a.y != b.y)
if(seg[a.x][b.x] && seg[a.x][b.y] && seg[a.y][b.x] && seg[a.y][b.y])
ans[4]++;
}
if(ans[4] > 0)
{
printf("4 %d\n",ans[4]/3);
continue;
}
for(int i = 0; i < len; i++)
for(int j = 1; j <= n; j++)
{
Edge a = vec[i];
if(a.x != j && a.y != j && seg[a.x][j] && seg[a.y][j])
ans[3]++;
}
if(ans[3] > 0)
{
printf("3 %d\n",ans[3]/3);
continue;
}
if(ans[2] > 0)
{
printf("2 %d\n",ans[2]);
continue;
}
if(ans[1] > 0)
{
printf("1 %d\n",ans[1]);
continue;
}
}
return 0;
}