链接:https://vjudge.net/problem/HDU-1179

题意:

有n个法师和m个魔棒,每个法师喜欢多种魔棒,但每个法师只能在喜欢的魔棒中选一个。

求最多有几个法师能选到魔棒。

思路:

二分图最大匹配,因为题目是魔棒对应的法师,所以输入处理一下。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std; typedef long long LL;
const int MAXN = 2000+10;
vector<int> G[MAXN];
int Link[MAXN], Vis[MAXN];
int n, m; void Init()
{
for (int i = 1;i <= n;i++)
G[i].clear();
} bool Dfs(int x)
{
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i];
if (Vis[node])
continue;
Vis[node] = 1;
if (Link[node] == -1 || Dfs(Link[node]))
{
Link[node] = x;
return true;
}
}
return false;
} int Solve()
{
memset(Link, -1, sizeof(Link));
int cnt = 0;
for (int i = 1;i <= n;i++)
{
memset(Vis, 0, sizeof(Vis));
if (Dfs(i))
cnt++;
}
return cnt;
} int main()
{
while (cin >> n && n)
{
Init();
cin >> m;
int num, r;
for (int i = 1;i <= m;i++)
{
cin >> num;
while (num--)
{
cin >> r;
G[r].push_back(i);
}
}
int cnt = Solve();
cout << cnt << endl;
} return 0;
}

  

HDU-1179-Ollivanders(二分图最大匹配)的更多相关文章

  1. HDU——1179 Ollivanders: Makers of Fine Wands since 382 BC.

    Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  2. HDU - 2819 Swap(二分图最大匹配)

    Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. C ...

  3. HDU 2063 (二分图最大匹配)

    RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每个女孩 ...

  4. HDU 1281 - 棋盘游戏 - [二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1281 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  5. HDU:过山车(二分图最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意:有m个男,n个女,和 k 条边,求有多少对男女可以搭配. 思路:裸的二分图最大匹配,匈牙利算法. 枚 ...

  6. [HDU] 2063 过山车(二分图最大匹配)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. #include<cstdio> ...

  7. HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)

    HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配) Description The zoo have N cats and M dogs, toda ...

  8. HDU 2255 奔小康赚大钱(带权二分图最大匹配)

    HDU 2255 奔小康赚大钱(带权二分图最大匹配) Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊 ...

  9. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  10. # 匈牙利算法(二分图最大匹配)- hdu 过山车

    匈牙利算法(二分图最大匹配)- hdu 过山车 Hdu 2063 二分图:图中的点可以分成两组U,V,所有边都是连接U,V中的顶点.等价定义是:含奇数条边的图. 匹配:一个匹配是一个边的集合,其中任意 ...

随机推荐

  1. sphinx 全文搜索引擎

    sphinx的安装与配置 --------------------------------------------------------------------------------------- ...

  2. osx快捷键表示图

  3. POJ3107Godfather(求树的重心裸题)

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got real ...

  4. javacpp-FFmpeg系列补充:FFmpeg解决avformat_find_stream_info检索时间过长问题

    javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...

  5. bzoj1072Perm——状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1072 数字串最多只有10位,所以考虑用状压: 压缩的状态是哪些数字被用过,这样可以从一种状态 ...

  6. zk 04之 Zookeeper Api(java)与应用

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  7. Angular 2 ViewChild & ViewChildren

    一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...

  8. 继承HibernateDaoSupport实现DAO(spring整合hibernate)

    转自:https://blog.csdn.net/yz9612/article/details/80234377 spring为hibernate的DAO提供工具类:HibernateDaoSuppo ...

  9. 运用flask、flask-restful开发rest风格的接口,并使用蓝图增加代码的延展性和可扩展性。

    本人做为一个测试人员,之前也有写过,想要测试好接口,那必须要知道如何开发一个接口的重要性. 之前也写过通flask或者flask-retful开发接口,但那些只是一些最简单的demo,不具有很好延展性 ...

  10. gcc -frandom-seed

    -frandom-seed=string This option provides a seed that GCC uses when it would otherwise use random nu ...