您的位置:首页 > 其它

HDU 1083 Courses (二分最大匹配)

2013-05-05 15:16 357 查看
这道题很简单,一看知道是二分最大匹配的题目。只要是用来联系模板的。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int LN = 110;
const int RN = 310;
int ln, rn;
int g[LN][RN], cx[LN], cy[RN];//存储相应的匹配
bool used[RN];

bool dfs( int u ) {
for ( int v = 1; v <= rn; ++v ) if ( g[u][v] && !used[v] ) {
used[v] = true;
if ( cy[v] == -1 || dfs( cy[v] ) ) {
cy[v] = u;
cx[u] = v;
return 1;
}
}
return 0;
}
int match()
{
int res = 0;
memset(cx, -1, sizeof(cx));
memset(cy, -1, sizeof(cy));
for ( int u = 1; u <= ln; ++u ) {
memset( used, 0, sizeof(used));
if ( dfs(u) ) res++;
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while ( T-- ) {
memset(g, 0, sizeof(g));
scanf("%d%d", &ln, &rn);
for ( int i = 1; i <= ln; ++i ) {
int num;
scanf("%d", &num);
while ( num-- ) {
int v;
scanf("%d", &v);
g[i][v] = 1;
}
}
if ( match() == ln ) printf("YES\n");
else printf("NO\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: