1854: [Scoi2010]游戏[并查集]

1854: [Scoi2010]游戏

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 4938  Solved: 1948
[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

Sample Output

2

HINT

【数据范围】
对于30%的数据,保证N < =1000
对于100%的数据,保证N < =1000000

Source

Day1

/*
    将每个装备看做一条边,将装备的属性看做点。将每个装备的两个属性连接成一个集合,依次连成一条链。
    最后用并查集处理找出根最大的链,输出。。。
    注意每次并查集合并时要把属性大的作为根。
*/
#include<cstdio>
#include<algorithm>
#define set(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=1e4+5;
int n,fa[N];bool vis[N];
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
    set(game);
    for(int i=1;i<=10000;i++) fa[i]=i;
    n=read();
    for(int i=1,x,y;i<=n;i++){
        x=read();y=read();
        x=find(x);y=find(y);
        if(x==y) vis[x]=1;
        if(x<y) swap(x,y);
        fa[y]=x;
    }
    for(int i=1;i<=10000;i++){
        if(find(i)==i&&!vis[i]){
            printf("%d",i-1);return 0;
        }
    }
    puts("10000");
    return 0;
}
/*
orz zjk 
乱搞100分:
b[]为第一优先
a[],num[b[]]为第二优先 
#include<cstdio>
#include<iostream>
#define set(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=1e6+5;
int n,f,a[N],b[N],num[N];bool vis[N];
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main(){
    set(game);
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();b[i]=read();
        if(a[i]>b[i]) swap(a[i],b[i]);
        num[a[i]]++;num[b[i]]++;
    }
    for(int i=1,pos;i<=10000;i++){
        pos=0;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&b[j]==i){
                pos=j;break;
            }
        }
        if(pos){
            vis[pos]=1;
            num[a[pos]]--;num[b[pos]]--;
            continue;
        }
        pos=0;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&a[j]==i&&num[b[j]]>num[b[pos]]){
                pos=j;
            }
        }
        if(pos){
            vis[pos]=1;
            num[a[pos]]--;num[b[pos]]--;
        }
        else{
            f=1;
            printf("%d
",i-1);
            break;
        }
    }
    if(!f) puts("10000");
    return 0;
}
/*
in:
6
1 3
2 4
3 4
4 5
4 6
5 6
out:
6
*/
原文地址:https://www.cnblogs.com/shenben/p/6623649.html