[HNOI2006]超级英雄 超级英雄Hero HYSBZ - 1191 二分图匹配

题目描述
题目描述

现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金。主持人问题准备了若干道题目,只有当选手正确回答一道题后,才能进入下一题,否则就被淘汰。为了增加节目的趣味性并适当降低难度,主持人总提供给选手几个“锦囊妙计”,比如求助现场观众,或者去掉若干个错误答案(选择题)等等。

这里,我们把规则稍微改变一下。假设主持人总共有m道题,选手有n种不同的“锦囊妙计”。主持人规定,每道题都可以从两种“锦囊妙计”中选择一种,而每种“锦囊妙计”只能用一次。我们又假设一道题使用了它允许的锦囊妙计后,就一定能正确回答,顺利进入下一题。现在我来到了节目现场,可是我实在是太笨了,以至于一道题也不会做,每道题只好借助使用“锦囊妙计”来通过。如果我事先就知道了每道题能够使用哪两种“锦囊妙计”,那么你能告诉我怎样选择才能通过最多的题数吗?
输入输出格式
输入格式:

输入的第一行是两个正整数 nnn 和 mmm (0<n<1001,0<m<10010 < n < 1001, 0 < m < 10010<n<1001,0<m<1001)表示总共有 n 种“锦囊妙计”,编号为 0∼n−10 \sim n-10∼n−1,总共有 mmm 个问题。

以下的m行,每行两个数,分别表示第 mmm 个问题可以使用的“锦囊妙计”的编号。

注意,每种编号的“锦囊妙计”只能使用一次,同一个问题的两个“锦囊妙计”可能一样。

输出格式:

输出的第一行为最多能通过的题数 ppp,接下来 ppp 行,每行为一个整数,第 iii 行表示第 iii 题使用的“锦囊妙计的编号”。

如果有多种答案,那么任意输出一种,本题使用 Special Judge 评判答案。

输入输出样例
输入样例#1: 复制

5 6
3 2
2 0
0 3
0 4
3 2
3 2

输出样例#1: 复制

4
3
2
0
4

说明

感谢@zhouyonglong 提供special Judge


不少同学用网络流做的;
当然直接二分图匹配就行了;
注意一点:当有一题无法匹配时,break;因为题目意思很明确了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 1e9 + 7;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

int match[2000];
int vis[maxn];
int G[2000][2000];
int res[maxn];
int n, m;
bool dfs(int x) {
	for (int i = 0; i < n; i++) {
		if (!vis[i] && G[x][i]) {
			vis[i] = 1;
			if (match[i]==-1 || dfs(match[i])) {
				match[i] = x; res[x] = i;
				return 1;
			}
		}
	}
	return 0;
}


int main()
{
	//ios::sync_with_stdio(false);
	rdint(n); rdint(m);
	for (int i = 1; i <= m; i++) {
		int x, y; rdint(x); rdint(y);
	//	G[x][i] = G[y][i] = 1;
		G[i][x] = G[i][y] = 1;
	}
	memset(match, -1, sizeof(match));
	int ans = 0;
	for (int i = 1; i <= m; i++) {
		ms(vis);
		if (dfs(i))ans++;
		else break;
	}
	cout << ans << endl;
	for (int i = 1; i <= ans; i++) {
		cout << res[i] << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值