The Perfect Stall
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26274 Accepted: 11672
Description
Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
Input
The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.
Output
For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
Sample Input
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
Sample Output
4
题目大意:一些牛和牛栏,每个牛有喜欢的牛栏,问最多能有多少只牛有牛栏。
解题思路: 二分图匹配,测试一下
Hopcroft−Karp
模板
///Hopcroft-Carp
///复杂度O(sqrt(n)*E)
///邻接表存图,vector实现
///vector先初始化,然后加入边
///uN为左端的顶点数,使用前赋值(点编号0开始)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int MAXN=1e5+5;
vector<int> G[MAXN];
int head[MAXN];
int uN,tot;
namespace fastIO {
#define BUF_SIZE 100000
//fread -> read
bool IOerror = 0;
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if(p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
char ch;
while(blank(ch = nc()));
if(IOerror)
return;
for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
}
#undef BUF_SIZE
};
using namespace fastIO;
struct Edge
{
int to,nxt;
}e[MAXN*2];
void addedge(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
int Mx[MAXN],My[MAXN];
int dx[MAXN],dy[MAXN];
int dis;
bool used[MAXN];
bool searchP()
{
queue<int> Q;
dis=INF;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
for(int i=0;i<uN;i++)
{
if(Mx[i]==-1)
{
Q.push(i);
dx[i]=0;
}
}
while(!Q.empty())
{
int u=Q.front();
Q.pop();
if(dx[u]>dis) break;
//int sz=G[u].size();
//for(int i=0;i<sz;i++)
for(int i=head[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
//int v=G[u][i];
if(dy[v]==-1)
{
dy[v]=dx[u]+1;
if(My[v]==-1) dis=dy[v];
else
{
dx[My[v]]=dy[v]+1;
Q.push(My[v]);
}
}
}
}
return dis!=INF;
}
bool dfs(int u)
{
//int sz=G[u].size();
//for(int i=0;i<sz;i++)
for(int i=head[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
//int v=G[u][i];
if(!used[v]&&dy[v]==dx[u]+1)
{
used[v]=true;
if(My[v]!=-1&&dy[v]==dis) continue;
if(My[v]==-1||dfs(My[v]))
{
My[v]=u;
Mx[u]=v;
return true;
}
}
}
return false;
}
int maxMatch()
{
int res=0;
memset(Mx,-1,sizeof(Mx));
memset(My,-1,sizeof(My));
while(searchP())
{
memset(used,false,sizeof(used));
for(int i=0;i<uN;i++)
{
if(Mx[i]==-1&&dfs(i))
res++;
}
}
return res;
}
//T=min(K/2,S);
//T+(K-2*T)=K-T
int main()
{
int T;
//read(T);
//scanf("%d",&T);
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
tot=0;
//read(n);read(k);
uN=n;
//for(int i=0;i<=n;i++) G[i].clear();
memset(head,-1,sizeof(head));
int tmp;
int u,v,num;
for(int i=0;i<n;i++)
{
scanf("%d",&num);
for(int j=1;j<=num;j++)
{
scanf("%d",&u);
addedge(i,u);;
//addedge(i,u+k);
}
}
/*for(int i=2;i<=n;i++)
{
//read(tmp);
scanf("%d",&tmp);
addedge(i,tmp);
addedge(tmp,i);
//G[tmp].push_back(i);
//G[i].push_back(tmp);
}*/
//int T=min(k/2,maxMatch());
//printf("%d\n",k-T);
printf("%d\n",maxMatch());
}
return 0;
}
/*
2
4 4
1 2 3
4 3
1 1 1
2
2
*/
解决一个农场中牛与特定牛栏匹配的问题,通过二分图匹配算法找到最大数量的有效分配方案。
3505

被折叠的 条评论
为什么被折叠?



