幼儿园中有n个男孩m个女孩,男孩之间都认识,女孩之间都认识,求一个极大团。
建出补图后就转化成了求该图独立集的问题,因为补图是二分图,于是匹配。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define N 5005
#define M 500005
using namespace std;
int n , m , s , t;
struct arc
{
int x , f , next;
}e[M];
int pre[N] , mcnt;
void addarc(int x ,int y ,int z)
{
e[mcnt] = (arc) {y , z , pre[x]} , pre[x] = mcnt ++;
e[mcnt] = (arc) {x , 0 , pre[y]} , pre[y] = mcnt ++;
}
int d[N] , cur[N];
bool f[N];
bool BFS()
{
memset(f, 0 ,sizeof(f));
queue<int> q;
q.push(s) , d[s] = 0 , f[s] = 1;
while (!q.empty())
{
int x = q.front() ; q.pop();
for (int i = pre[x] ; ~i ;i = e[i].next)
{
arc& a = e[i];
if (!f[a.x] && a.f)
f[a.x] = 1 , d[a.x] = d[x] + 1 , q.push(a.x);
}
}
return f[t];
}
int DFS(int x , int flow)
{
if (x == t || !flow) return flow;
int sum = 0 , u;
for (int& i = cur[x] ; ~i ;i = e[i].next)
{
arc& a = e[i];
if (d[a.x] == d[x] + 1 && (u = DFS(a.x , min(flow , a.f))) > 0 )
{
a.f -= u , e[i ^ 1].f += u;
sum += u , flow -= u;
if (!flow) break;
}
}
return sum;
}
int dinic()
{
int ans = 0;
while (BFS())
{
memcpy(cur , pre , (t + 1) * sizeof(int));
ans += DFS(s , 1 << 30);
}
return ans;
}
int k , ca;
bool g[305][305];
void work()
{
int i , j , x , y;
printf("Case %d: " , ++ ca);
scanf("%d%d",&m,&k);
memset(pre , -1 , sizeof(pre));
mcnt = 0 , s = n + m + 1 , t = s + 1;
memset(g , 1 , sizeof(g));
while (k --)
scanf("%d%d",&x,&y) , g[x][y] = 0;
for (i = 1 ; i <= n ; ++ i) addarc(s , i , 1);
for (i = 1 ; i <= m ; ++ i) addarc(i + n , t , 1);
for (i = 1 ; i <= n ; ++ i)
for (j = 1 ; j <= m ; ++ j)
if (g[i][j])
addarc(i , j + n , 1);
cout << n + m - dinic() << endl;
}
int main()
{
while(scanf("%d", &n) , n)
//int _;cin>>_;while(_--)
work();
return 0;
}