1854: [Scoi2010]游戏
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2145 Solved: 766
[ Submit][ Status]
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
暴力!!!!!
//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <bitset>
using namespace std;
//
int n;
struct edge{int v,next;}e[2000001];
int k=0;
int head[1000001];
bitset<1000001> vis;
int pre[1000001];
//
void adde(int u,int v){e[k].v=v;e[k].next=head[u];head[u]=k++;}
bool search(int u)
{
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(vis[v]) continue;
vis[v]=1;
if(pre[v]==0||search(pre[v]))
{
pre[v]=u;
return 1;
}
}
return 0;
}
//
void input()
{
memset(head,-1,sizeof(head));
scanf("%d",&n);
int x,y;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
adde(x,i);
adde(y,i);
}
}
void solve()
{
//init
int ans=10000;//赋初值!!!!!!!
int l=0,r=n,mid;
//calculate
for(int i=1;i<=10000;i++)
{
vis.reset();
if(!search(i))
{
ans=i-1;
break;
}
}
//output
printf("%d",ans);
}
//
int main()
{
#ifndef _TEST
freopen("2415.in","r",stdin); freopen("2415.out","w",stdout);
#endif
input();
solve();
#ifdef _TEST
for(;;);
#endif
return 0;
}
并查集:
发现这题的并查集做法真是惊呆了
不过似乎匹配跑得更快?
对于一个联通块,假如不含环(就是一棵树),那么必定可以满足其中任意的p-1个点。
对于一个联通块,假如含环,那么必定全部的p个点都能满足。
那么合并并查集的时候可以利用一个vis来维护这个性质
把权值看成点,把武器看成边
如果每次加入的边是合并两个联通块
就把权值小的联通块并到权值大的联通块,然后给权值小的vis=true
如果不是
就把改联通块的顶点的vis=true
这样就可以保证,如果一个大小为N联通块
=N-1条边构成,最大点的vis=false,其他为true
≥N条边构成,所有点的vis=true
然后最后只要一次扫描vis就可以得出答案了 ————hzwer
代码:
var i,n,m,x,y,tmp:longint;
fa:array[0..15000] of longint;
vis:array[0..15000] of boolean;
function find(x:longint):longint;
begin
if fa[x]<>x then fa[x]:=find(fa[x]);
exit(fa[x]);
end;
procedure main;
begin
readln(n);
fillchar(vis,sizeof(vis),false);
for i:=1 to 10100 do fa[i]:=i;
for i:=1 to n do
begin
readln(x,y);
x:=find(x);y:=find(y);
if x=y then vis[x]:=true
else
begin
if x>y then begin tmp:=x;x:=y;y:=tmp;end;
fa[x]:=y;
vis[x]:=true;
end;
end;
for i:=1 to 10010 do if (not(vis[i])) and (find(i)=i) then break;
writeln(i-1);
end;
begin
main;
end.