poj 1422

最小覆盖等于n-最大匹配.

View Code
#include<iostream>
#include<map>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn=180;
vector<int>v[maxn];
int vis[maxn],match[maxn];
int n,m;
void init()
{
for(int i=0;i<=n;i++)
v[i].clear();
memset(match,-1,sizeof(match));
}
bool dfs(int u)
{
for(int i=0;i<v[u].size();i++)
{
int t=v[u][i];
if(vis[t]) continue;
vis[t]=1;
if(match[t]==-1||dfs(match[t]))
{
match[t]=u;
return 1;
}
}
return 0;
}
int main()
{
int cas;int x,y;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
}
int ans=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",n-ans);
}
return 0;
}



posted @ 2012-03-07 19:18  静静的等待_93  阅读(91)  评论(0编辑  收藏  举报