二分图匹配(匈牙利算法):
http://blog.csdn.net/dark_scope/article/details/8880547
http://www.cnblogs.com/pony1993/archive/2012/07/25/2607738.html
题意:n牛,m个房子,每个牛都只住在自己想住的房子里面,一个房子只能住一个牛,问最多可以安排多少头牛入住;
分析:匈牙利算法模板
邻接表和邻接矩阵都可以
【邻接表】
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <functional>
#include <algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double e=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e3+5;
const ll maxn=5e4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,m;
bool vis[N];
int match[N];
vector<int>g[N];
void init()
{
rep(i,0,n+1) g[i].clear();
}
void addedge(int u,int v)
{
g[u].push_back(v);
//g[v].push_back(u);
}
int dfs(int x)
{
for(int i=0; i<g[x].size(); i++)
{
int y=g[x][i];
if(!vis[y])
{
vis[y]=1;
if(match[y]<0 || dfs(match[y]))
{
match[y]=x;
return true;
}
}
}
return false;
}
int solve()
{
int ans=0;
mem(match,-1);
for(int i=1; i<=n; i++)
{
mem(vis,0);
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
IO;
while(cin>>n>>m)
{
init();
for(int i=1; i<=n; i++)
{
int cnt,y;
cin>>cnt;
while(cnt--)
{
cin>>y;
addedge(i,y);
}
}
cout<<solve()<<endl;
}
return 0;
}
【邻接矩阵】
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <functional>
#include <algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double e=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e3+5;
const ll maxn=5e4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,m;
bool vis[N];
int match[N];
bool a[N][N];
int dfs(int x)
{
for(int y=1;y<=m;y++)
{
if(!vis[y] && a[x][y])
{
vis[y]=1;
if(match[y]<0 || dfs(match[y]))
{
match[y]=x;
return true;
}
}
}
return false;
}
int solve()
{
int ans=0;
mem(match,-1);
for(int i=1; i<=n; i++)
{
mem(vis,0);
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
IO;
while(cin>>n>>m)
{
mem(a,0);
for(int i=1; i<=n; i++)
{
int cnt,y;
cin>>cnt;
while(cnt--)
{
cin>>y;
a[i][y]=1;
}
}
cout<<solve()<<endl;
}
return 0;
}