[Scoi2010]游戏

1854: [Scoi2010]游戏

Time Limit: 5 Sec  Memory Limit: 162 MB
http://www.lydsy.com/JudgeOnline/problem.php?id=1854

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

并查集

想着:并查集本质上是一种树结构

将每个装备看做边,装备属性看做点,每次合并两个点

将过程想象成给点搭配边的过程

这样得到的连通块有两类

1、没有环,即边数=点数-1,那么对于树内的任意n-1的点,都可以搭配一条边,

但题目要求从小到大攻击,所以先给小的搭配。也就是说,树内只有最大的点没有搭配边。

如何实现这一过程?在合并时,让小的合并到大的上面,也就是始终让树内最大的点是祖先节点

2、边数>=n,有环或重边,那么对于连通块内所有的n个点,都可以搭配一条边

在搭配过程中用一数组记录点是否有搭配边,输出第一个没有搭配边的点

本题并查集解题关键:控制最大的点成为祖先节点

#include<cstdio>
#include<algorithm>
using namespace std;
int n,fa[10001];
bool v[10001];
int find(int i)
{
    return fa[i]==i ? i:fa[i]=find(fa[i]);
}
int main()
{
    for(int i=1;i<=10000;i++) fa[i]=i;
    scanf("%d",&n);
    int a,b,r1,r2;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a,&b);
        r1=find(a);r2=find(b);
        if(r1==r2) v[r1]=true;
        else
        {
            if(r1>r2) swap(r1,r2);
            fa[r1]=r2;
            v[r1]=true;
        } 
    }
    int i;
    for(i=1;v[i];i++);
    printf("%d",i-1);
}

我的错误做法:

所有属性有i的武器向所有属性有i+1的武器连边,然后求0到n的最长路

错误原因:会有同一武器使用两次的情况

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int n;
bool v[1000001];
int front[1000001],tot;
int front2[10001],tot2;
queue<int>q;
struct node
{
    int to,next;
}e2[2000001];
struct node1
{
    int to,next;
}e[2000001];
int dis[1000001];
void add(int u,int v)
{
    e[++tot].to=v;e[tot].next=front[u];front[u]=tot;
}
void add2(int u,int v)
{
    e2[++tot2].to=v;e[tot2].next=front2[u];front2[u]=tot2;
}
int main()
{
    /*freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);*/
    scanf("%d",&n);
    int a,b;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a,&b);
        add2(a,i);add2(b,i);
    }
    for(int i=front2[1];i;i=e2[i].next) 
     add(0,e2[i].to);
    for(int i=1;i<n;i++)
      for(int k=front2[i];k;k=e2[k].next) 
        for(int j=front2[i+1];j;j=e2[k].next)
         if(e2[k].to!=e2[j].to)
          add(e2[k].to,e2[j].to);
    q.push(0);
    v[0]=true;
    int now,to;
    while(!q.empty())
    {
        now=q.front();q.pop();
        for(int i=front[now];i;i=e[i].next)
        {
            to=e[i].to;
            if(dis[to]<=dis[now]+1) 
            {
                dis[to]=dis[now]+1;
                if(!v[to])
                {
                    v[to]=true;
                    q.push(to);                
                }
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++)
     ans=max(ans,dis[i]);
    printf("%d",ans);
}
wrong answer 10分
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6622727.html