1854: [Scoi2010]游戏
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3695 Solved: 1405
[ Submit][ Status][ Discuss]
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
1 2
3 2
4 5
Sample Output
2
HINT
【数据范围】
对于30%的数据,保证N < =1000
对于100%的数据,保证N < =1000000
Source
因为所有属性的权值只用选择一次,而所有武器只能选择其中的一个属性,所有从一个武器的两个属性分别向这个武器连边,跑匈牙利算法即可.(貌似网上有写并查集的).
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 3200000
#define M 1200000
#define inf 1000000000
using namespace std;
int next[N*2],v[N*2],remain[N*2];
int point[M];
int belong[M],cur[M];
int n,tot;
void add(int x,int y)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
}
int dfs(int x)
{
for (int i=point[x];i;i=next[i])
{
int t=v[i];
if (cur[t]==i) continue;
cur[t]=i;
if (!belong[t]||dfs(belong[t]))
{
belong[t]=x;
return true;
}
}
return false;
}
int main()
{
scanf("%d",&n);
int maxn=0;
for (int i=1;i<=n;i++)
{
int x,y; scanf("%d%d",&x,&y);
add(x,i);
add(y,i);
maxn=max(maxn,x); maxn=max(maxn,y);
}
memset(belong,0,sizeof(belong));
for (int i=1;i<=maxn;i++)
{
if (!dfs(i))
{
printf("%d\n",i-1);
return 0;
}
}
printf("%d\n",maxn);
}