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


    1854: [Scoi2010]游戏

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

    题解

    一个武器可以看成连接对应两个属性值的一条边
    可以证明如果一个联通块中出现一个环
    则整个联通块都可以取
    否则会有一个点不能取
    显然这个不取的点是最大的那个点= =
    然后扫一遍看能取到哪就行了
    找环和维护最大可以用并查集做

    //转载的,太神啦!!!!

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <map>
    #include <vector>
    #include <functional>
    #include <ctime>
    #include <cstdlib>
    #include <sstream>
    #include <set>
    #include <deque>
    using namespace std;
    typedef long long ll;
    typedef long double ld;
     
    const int maxn = 1000005;
     
    int n, fa[maxn], is[maxn];
     
    int getfa(int now)
    {
        return (!fa[now]) ? now : fa[now] = getfa(fa[now]);
    }
     
    int main()
    {
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            u = getfa(u), v = getfa(v);
            if (u == v) is[u] = is[v] = 1;
            else
            {
                if (u < v)swap(u, v);
                if (is[u] || is[v]) is[u] = is[v] = 1;
                fa[v] = u;
                is[v] = 1;
            }
        }
        for (int i = 1; i < maxn; ++i)
            if (!is[i])
            {
                cout << i - 1;
                break;
            }
    }
  • 相关阅读:
    Objectivec中的@property和@synthesize详解
    objectc笔记
    iOS常用开源框架之ASIHTTPRequest
    oschina引用库说明
    UITableView的重用机制
    iOS常用开源框架之AFNetworking
    ObjectiveC中@class和#import的区别
    Python中文文档 目录(转载)
    关于Console 2窗口内容偏移以及中文输入的问题
    Python 中除法运算需要注意的几点
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4348774.html
Copyright © 2020-2023  润新知