题意:有一个帝国在打仗,敌方会搞一些破坏,总共用N个阵地,每个阵地都有一个武力值,当第一地方收到攻击的时候他可以进行求助,当然求助的对象只能是武力值比他高的,如果求助失败就输出 ‘-1’, 求助成功就输出 帮助对象的的下标,如果有多个相同武力值的阵地输出下标最小的那个。

输入的第一行是N,表示又N个阵地(0到n-1),下面一行输入每个阵地的武力值,接着输入一个M,下面有M行,表示两点可以想通,接着有Q次查询,query 是查询这个点能不能有帮助他的,destroy表示摧毁两点之间的联系,值得注意的是,他所摧毁的一定是存在的路,而输入的时候也没有重复数据(为了是题目简单一些吧)。
分析:由于是中间有摧毁的步骤,而并查集只能两点链接,不能消除,所以只能倒着处理数据,先把所有能摧毁的全部都不链接,把没有摧毁的链接起来,(可以用扩大第二个数的办法来快速查找路),倒着读的时候遇到摧毁就进行建立,这样可以了,不过还要注意值相等的时候按照下标,就是忘了这点才错了好几次,而且数据之间输出一个空行
////////////////////////////////////////////////////////////////

#include<iostream>

#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
using namespace std; const int maxn = ;
const int BigNum = ; struct node
{
    int op, u, v;//op等于1代表查询,等于0代表摧毁
}data[maxn*];//保存查询数据 int f[maxn], val[maxn];
//为了方便查询,把x值扩大10w倍+y值保存下来,use记录这个路是否被摧毁
int h[maxn], use[maxn]; int Find(int x)
{
    if(f[x] != x)
        f[x] = Find(f[x]);
    return f[x];
}
void Union(int u, int v)
{
    u = Find(u), v = Find(v);     if(u != v)
    {
        if(val[u] < val[v])
            f[u] = v;
        else if(val[u] > val[v])//写成了两个一样的。。。。
            f[v] = u;
        else if(u < v)
            f[v] = u;
        else
            f[u] = v;
    }
} int main()
{
    int N, t=;     while(scanf("%d", &N) != EOF)
    {
        int i, M, u, v, Q;
        char s[];         for(i=; i<N; i++)
        {
            f[i] = i;
            scanf("%d", &val[i]);
        }         scanf("%d", &M);         for(i=; i<M; i++)
        {
            scanf("%d%d", &u, &v);
            if(u > v)swap(u, v);
            h[i] = u + v*BigNum;
            use[i] = ;
        }         sort(h, h+M);         scanf("%d", &Q);         for(i=; i<Q; i++)
        {
            scanf("%s", s);             if(s[] == 'd')
            {
                scanf("%d%d", &u, &v);                 if(u > v)swap(u, v);
                data[i].u = u;data[i].v = v;
                data[i].op = ;
                int k = lower_bound(h, h+M, u+v*BigNum) - h;
                use[k] = ;
            }
            else
            {
                scanf("%d", &u);
                data[i].op = , data[i].u = u;
            }
        }         for(i=; i<M; i++)
        {
            u = h[i] % BigNum, v = h[i] / BigNum;
            if(use[i] == )
                Union(u, v);
        }         stack<int>sta;         for(i=Q-; i>=; i--)
        {
            if(data[i].op == )
                Union(data[i].u, data[i].v);
            else
            {
                u = Find(data[i].u);                 if(val[u] <= val[data[i].u])
                    sta.push(-);
                else
                    sta.push(u);
            }
        }         if(t++)printf("\n");
        while(sta.size())
        {
            printf("%d\n", sta.top());
            sta.pop();
        }
    }     return ;
}
/*
5
1 2 3 4 5
4
0 1
1 2
2 3
3 4
5
query 0
query 1
query 2
query 3
query 4
*/

L - Connections in Galaxy War - zoj 3261的更多相关文章

  1. (并查集)Connections in Galaxy War -- zoj --3261 还没写

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 http://acm.hust.edu.cn/vjudge/ ...

  2. Connections in Galaxy War ZOJ - 3261 (并查集)

    点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...

  3. Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边

    #include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...

  4. 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

    这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...

  5. ZOJ3261:Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...

  6. Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...

  7. Connections in Galaxy War (逆向并查集)题解

    Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...

  8. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...

  9. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

随机推荐

  1. Python之路,Day20 - 分布式监控系统开发

    Python之路,Day20 - 分布式监控系统开发   本节内容 为什么要做监控? 常用监控系统设计讨论 监控系统架构设计 监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个 ...

  2. 没有懂的leetcode

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 将从网上下载下来的javaweb项目继续配置

    1.将下载下来的项目,看有没有报错,这里推荐的是不变成web项目的方法,直接通过编译到服务器目录 2.报错的问题,一般是包,服务器的包,(tomcat-home)指向自己的bin目录 3.然后是添加s ...

  4. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  5. linux下系统定时任务配置----crontab(mysql定时备份)

    crontab命令用于设置周期性被执行的指令,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任 ...

  6. 【USACO 2.1.3】三值的排序

    [题目描述] 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排 ...

  7. case 后面可以接汉语

    switch($_POST['rtype']){        case "图片":         $type="image";break;        c ...

  8. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

  9. Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置

    一 :准备阶段 1:php php-5.5.13下载链接:http://windows.php.net/downloads/releases/php-5.5.13-Win32-VC11-x64.zip ...

  10. Delphi-Copy 函数

    函数名称 Copy 所在单元 System 函数原型 1  function Copy ( Source : string; StartChar, Count : Integer ) : string ...