BZOJ1854: [Scoi2010]游戏

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1854

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

题解:好题啊,竟然可以写并查集,然而我知道写二分图最大匹配,但是脑补+看题解,我还是选择了神奇的并查集写法-------

   详见:http://hzwer.com/2950.html

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #define N 1000005
 7 using namespace std;
 8 int n,a,b;
 9 int fa[N];
10 bool vis[N];
11 int read()
12 {
13     int x=0; char ch; bool bo=0;
14     while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1;
15     while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9');
16     if (bo) return -x; return x;
17 }
18 int find(int x)
19 {
20     if (fa[x]!=x) fa[x]=find(fa[x]);
21     return (fa[x]);
22 }
23 int main()
24 {
25     n=read();
26     for (int i=1; i<=n+1; i++) fa[i]=i;
27     for (int i=1; i<=n; i++)
28     {
29         a=read(),b=read();
30         int q=find(a),p=find(b);
31         if (q==p)
32         {
33             vis[p]=true; 
34         }
35         else 
36         {
37             if (q<p) swap(q,p);
38             vis[p]=true; fa[p]=q;
39         }
40     }
41     int i;
42     for (i=1; i<=n+1; i++) if (!vis[i]) break;
43     printf("%d\n",i-1);
44 }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值