Links: http://www.lightoj.com/volume_showproblem.php?problem=1201
Maximum independent set = number of vertices-maximum match
Code:
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int n, m;
int p[1010][1010];
int book[1010];
int match[1010];
int dfs(int u)
{
int i;
for (i = 1;i <= n;i++)
{
if (book[i] == 0 && p[u][i] == 1)
{
book[i] = 1;
if (match[i] == 0 || dfs(match[i]))
{
match[i] = u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
int cases = 1;
scanf("%d", &t);
while (t--)
{
int ans = 0;
memset(match, 0, sizeof(match));
memset(p, 0, sizeof(p));
scanf("%d%d", &n, &m);
int a, b;
while (m--)
{
scanf("%d%d", &a, &b);
p[a][b] = p[b][a] = 1;
}
for (int i = 1;i <= n;i++)
{
memset(book, 0, sizeof(book));
if (dfs(i))
ans++;
}
printf("Case %d: %d\n", cases++,n - ans/2);
}
return 0;
}
Copyright NOTICE: Reprint please indicate the source.
Lightoj 1201-a Perfect Murder "the largest independent set of two graphs"