【USACO4.2.2】完美的牛栏 二分图的匹配

裸二分图匹配




/*
TASK:stall4
LANG:C++
*/
#include<iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;


const int max_ver = 200 + 10;
struct edge
{
	edge *next;
	int ver;
	edge(int _Ver, edge *_Next):ver(_Ver), next(_Next){}
	edge(){}
}*net[max_ver]={NULL};
int n, m;

inline void insert(int a, int b)
{
	net[a] = new edge(b, net[a]);
}

void init()
{
	scanf("%d%d", &n, &m);
	int i = 0;
	while (m--)
	{
		++ i;
		int tmp;
		scanf("%d", &tmp);
		while (tmp--)
		{
			int ver;
			scanf("%d", &ver);
			insert(i, ver);
		}
	}
}

int vis[max_ver], belong[max_ver];

int dfs(int k)
{
	for (edge *e = net[k]; e; e = e -> next)
	{
		if (!vis[e -> ver])
		{
			vis[e -> ver] = 1;
			if (belong[e -> ver] == -1 || dfs(belong[e -> ver]))	
			{
				belong[e -> ver] = k;
				return 1;
			}
		}
	}
	return 0;
}

void doit()
{
	int ans = 0;
	memset(belong, -1, sizeof(belong));
	for (int i = 1; i <= n; ++ i)
	{
		memset(vis, 0, sizeof(vis));
		ans += dfs(i);
	}
	printf("%d\n", ans);
}

int main()
{
	freopen("stall4.in","r",stdin);
	freopen("stall4.out","w",stdout);
	init();
	doit();
	return 0;
}


你可能感兴趣的:(【USACO4.2.2】完美的牛栏 二分图的匹配)