[SCOI2010]连续攻击游戏 BZOJ1854 二分图匹配

题目描述

lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示。当他使用某种装备时,他只能使用该装备的某一个属性。并且每种装备最多只能使用一次。游戏进行到最后,lxhgww遇到了终极boss,这个终极boss很奇怪,攻击他的装备所使用的属性值必须从1开始连续递增地攻击,才能对boss产生伤害。也就是说一开始的时候,lxhgww只能使用某个属性值为1的装备攻击boss,然后只能使用某个属性值为2的装备攻击boss,然后只能使用某个属性值为3的装备攻击boss……以此类推。现在lxhgww想知道他最多能连续攻击boss多少次?

输入输出格式

输入格式:

输入的第一行是一个整数N,表示lxhgww拥有N种装备接下来N行,是对这N种装备的描述,每行2个数字,表示第i种装备的2个属性值

输出格式:

输出一行,包括1个数字,表示lxhgww最多能连续攻击的次数。

输入输出样例

输入样例#1: 复制
3
1 2
3 2
4 5
输出样例#1: 复制
2

说明

Limitation

对于30%的数据,保证N < =1000

对于100%的数据,保证N < =1000000

来源:SCOI 2010

二分图匹配;

对于第 i 种的两个属性 a,b;

连边 <a,i>,<b,i>;

#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(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll 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);
}
int sqr(int x) { return x * x; }


/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

struct node {
	int u, v, w;
	int nxt;
}edge[maxn<<4];
bool vis[maxn>>1];
int head[maxn>>1];
int match[maxn << 3];
int tot;
void addedge(int x, int y) {
	edge[++tot].u = x; edge[tot].v = y;
	edge[tot].nxt = head[x]; head[x] = tot;
}

bool dfs(int x) {
	if (vis[x])return false;
	vis[x] = 1;
	for (int i = head[x]; i; i = edge[i].nxt) {
		int v = edge[i].v;
		if (!match[v] || dfs(match[v])) {
			match[v] = x; return true;
		}
	}
	return false;
}

int main() {
	//ios::sync_with_stdio(0);
	int n;
	rdint(n);
	for (int i = 1; i <= n; i++) {
		int x, y; rdint(x); rdint(y);
		addedge(x, i); addedge(y, i);
	}
	int ans = 0;
	for (int i = 1; i <= 10001; i++) {
		ms(vis);
		if (dfs(i))ans++;
		else break;
	}
	cout << ans << endl;
	return 0;
}
EPFL - Fighting
原文地址:https://www.cnblogs.com/zxyqzy/p/10265322.html