[SCOI2010]连续攻击游戏

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

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

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

Sample Input
3
1 2
3 2
4 5

Sample Output
2

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


按题意建出二分图,然后我们直接枚举属性值,由于匈牙利过程保证在枚举(i)的时候,(1sim i-1)都已经匹配完成,所以我们一旦碰到一个无法扩展的点就退出即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
	int x=0,f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e4,M=1e6;
int pre[(M<<1)+10],now[N+10],child[(M<<1)+10];
int path[M+10],vis[M+10];
int tot,Time;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
bool Extra(int x){
	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
		if (vis[son]==Time)	continue;
		vis[son]=Time;
		if (!~path[son]||Extra(path[son])){
			path[son]=x;
			return 1;
		}
	}
	return 0;
}
int main(){
	memset(path,255,sizeof(path));
	int n=read(),Ans=0,Max=0;
	for (int i=1;i<=n;i++){
		int x=read(),y=read();
		join(x,i),join(y,i);
		Max=max(Max,x);
		Max=max(Max,y);
	}
	for (int i=1;i<=Max;i++){
		++Time;
		if (Extra(i))	Ans++;
		else	break;
	}
	printf("%d
",Ans);
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/10251893.html