链接: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. HDU 4405 Aeroplane chess:期望dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 你在下简化版飞行棋... 棋盘为一个线段,长度为n. 上面有m对传送门,可以直接将你从a ...

  2. python练习1(简单爬虫)

    做一个简单的练习 目标:爬取中文小说 目标网站:http://www.biqule.com/book_58/26986.html 只爬取正文部分. 使用requests库来获取网页信息,使用re库正则 ...

  3. PS 滤镜— — 万花筒效果

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  4. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  5. 【Opencv】Mat基础

    1.Mat::imread() C++: Mat imread(const string& filename, int flags=1 ) filename – Name of file to ...

  6. 【LeetCode】070. Climbing Stairs

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  7. 洛谷 P5061 秘密任务 —— 二分图

    题目:https://www.luogu.org/problemnew/show/P5061 首先,“配合默契”就是连边的意思: 但发现答案不好统计,因为有连边的两个点可以分在一组,也可以不分在一组: ...

  8. [RTOS]--uCOS、FreeRTOS、RTThread、RTX等RTOS的对比之特点

    本篇博客就来细数这几个RTOS的特点.   以下内容均来自官方网站或者官方手册Feature的Google翻译的加了我的一些调整,没有任何主观成分. 1. FreeRTOS   FreeRTOS是专为 ...

  9. POJ2828(插队问题)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 17077   Accepted: 8466 Desc ...

  10. cocos2dx unzip、createDir

    转自:http://www.cnblogs.com/xioapingguo/p/4037323.html static unsigned long _maxUnzipBufSize = 0x50000 ...