[bzoj1854][SCOI2010]游戏

时间:2024-05-06 17:05:01

Description

一个装备有两个属性,一个装备只能被使用一次,一次使用一种属性。攻击boss时需按属性1、属性2、属性3...属性k的顺序使用,问k最大为多少。

Input

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

Output

输出一行,包括1个数字,表示k。

Sample Input

3
1 2
3 2
4 5

Sample Output

2

HINT

1<=属性值<=10000,N < =1000000

Solution

由于一种装备只能使用一次,而且只有装备和属性值两种分类,就会想到二分图匹配。

将装备和属性值分成两个集合,将属性值向所属装备连一条边,判断当前k,如果可匹配,判断k+1,如果不可行,k-1就是答案。

如果使用匈牙利算法的话,used[]不能每次都重新赋值false,会T(实测),而要将当前k作为判断标志(used[]=k为访问过)。

 #include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define K 10001
#define N 1000001
#define M 2000001
using namespace std;
struct graph{
int nxt,to;
}e[M];
int g[K],u[N],fr[N],n,cnt,ans;
inline int read(){
int ret=;char c=getchar();
while(!(c>=''&&c<=''))
c=getchar();
while(c>=''&&c<=''){
ret=ret*+c-'';
c=getchar();
}
return ret;
}
inline void addedge(int x,int y){
e[++cnt].nxt=g[x];g[x]=cnt;e[cnt].to=y;
}
inline bool match(int k){
for(int i=g[k];i;i=e[i].nxt)
if(u[e[i].to]!=ans){
u[e[i].to]=ans;
if(!fr[e[i].to]||match(fr[e[i].to])){
fr[e[i].to]=k;return true;
}
}
return false;
}
inline void init(){
n=read();
for(int i=,k;i<=n;i++){
k=read();addedge(k,i);
k=read();addedge(k,i);
}
for(ans=;ans<K;ans++){
if(!match(ans)) break;
}
printf("%d\n",--ans);
}
int main(){
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}