题目:http://poj.org/problem?id=1274
题意:又n头牛m个牛棚,每头牛都有自己喜欢去的多个牛棚,每行输入:第一位表示第i头牛喜欢去的牛棚数si,后面是si个牛棚编号。一个牛棚只能待一头牛。要求最多能使几头牛都能进牛棚。
思路:二分图最大匹配,裸的匈牙利算法
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cstddef>
#include <memory>
#include <vector>
#include <cctype>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <sstream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
//#pragma GCC optimize(3)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mkp(a,b) make_pair(a,b)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define fi first
#define se second
#define lc (d<<1) //d*2
#define rc (d<<1|1) //d*2+1
#define eps 1e-9
#define dbg(x) cerr << #x << " = " << x << "\n";
#define mst(a,val) memset(a,val,sizeof(a))
#define stn(a) setprecision(a)//小数总有效位数
#define stfl setiosflags(ios::fixed)//点后位数:cout<<stfl<<stn(a);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI=3.1415926535897932;
const int MAXN=1e5+10;
const ll mod=1e9+7;
ll inline mpow(ll a,ll b){ll ans=1;a%=mod;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod,b>>=1;}return ans;}
int inline sgn(double x){return (x>-eps)-(x<eps);} //a<b:sgn(a-b)<0
priority_queue<int,vector<int>,greater<int> > qu; //up
priority_queue<int,vector<int>,less<int> > qd; //dn
const int inf = 0x3f3f3f3f; //9
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
int n,m,g[210][210];
int lnk[210],use[210];
int dfs(int u)
{
for(int i=1;i<=m;i++)
if(g[u][i]&&!use[i]) //若 use=1 则表示这次的查找 曾试图改变过该i的归属问题,但是没有成功,所以就不用费工夫了
{
use[i]=1;
if(lnk[i]==-1||dfs(lnk[i]))
{
lnk[i]=u;
return true;
}
}
return false;
}
int hungary()
{
int res=0;
mst(lnk,-1);
for(int i=1;i<=n;i++)
{
mst(use,0);
if(dfs(i)) res++;
}
return res;
}
int main()
{
fio;
while(cin>>n>>m)
{
mst(g,0);
for(int i=1;i<=n;i++)
{
int num,to;
cin>>num;
for(int j=0;j<num;j++)
{
cin>>to;
g[i][to]=1;
}
}
int ans;
ans=hungary();
cout<<ans<<endl;
}
}