ZOJ3261 Connections in Galaxy War 并查集

分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int,int>pii;
const int N=1e4+5;
pii p[N<<1];
int v[N],fa[N],n,m,q;
bool vis[N<<1];
struct Ask{
  int op,id,ans;
}ask[N*5];
int find(int x){
  return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int x,int y){
  x=find(x),y=find(y);
  if(x==y)return;
  if(v[x]>v[y]||(v[x]==v[y]&&x<y))fa[y]=x;
  else fa[x]=y;
}
int main()
{
    bool flag=1;
    while(~scanf("%d",&n)){
       if(flag)flag=0;
       else printf("\n");
       for(int i=0;i<n;++i){
        scanf("%d",&v[i]),fa[i]=i;
       }
       memset(vis,0,sizeof(vis));
       scanf("%d",&m);
       for(int i=1;i<=m;++i){
          scanf("%d%d",&p[i].first,&p[i].second);
          if(p[i].first>p[i].second)swap(p[i].first,p[i].second);
       }
       sort(p+1,p+1+m);
       scanf("%d",&q);
       for(int i=1;i<=q;++i){
          char s[10];
          int u,v;
          scanf("%s%d",s,&u);
          if(s[0]==‘q‘){
            ask[i].op=1;
            ask[i].id=u;
          }
          else{
            ask[i].op=2;
            scanf("%d",&v);
            if(u>v)swap(u,v);
            ask[i].id=lower_bound(p+1,p+1+m,make_pair(u,v))-p;
            vis[ask[i].id]=1;
          }
       }
       for(int i=1;i<=m;++i){
        if(vis[i])continue;
         Union(p[i].first,p[i].second);
       }
       for(int i=q;i>0;--i){
         if(ask[i].op==1){
            ask[i].ans=find(ask[i].id);
            if(v[ask[i].ans]==v[ask[i].id])ask[i].ans=-1;
         }
         else Union(p[ask[i].id].first,p[ask[i].id].second);
       }
       for(int i=1;i<=q;++i)if(ask[i].op==1)printf("%d\n",ask[i].ans);
    }
    return 0;
}

时间: 2024-12-19 05:46:13

ZOJ3261 Connections in Galaxy War 并查集的相关文章

ZOJ 3261 Connections in Galaxy War (并查集)

Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began

Connections in Galaxy War(逆向并查集)

Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit: 3 Seconds      Memory Limit: 32768 KB In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional

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

In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then m

题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

Description In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimen

zoj 3261 Connections in Galaxy War(并查集+离线逆向操作)

 题目:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) destory a,b 表示删除连接a,b的边 思路并查集,但是要逆向处理,所以先离线读入,从后向前处理,于是对于destroy操作,等价于连接两个点的操作,然后对于每个询问输出即可 #include<cstdio> #include<cstring> #include<cmath> #i

ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)

题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断连接两点的边,query为查询某星球能不能向它人呼叫支援. 还是需要离线逆向并查集求解.思路和HDU 4496很相似,但是此处不一定是把所有边都删去,所以需要删边的情况建立出最终的状态.因为N可以到1e4,所以可以用map嵌套map的方式记录过程中被删去的边,最后再根据删除情况建立状态. 在合并时需

Connections in Galaxy War——逆向并查集

题目链接 题意: 首先给定一个n 然后给你 n个数 表示 每个恒星的能量大小 p[i] 然后给定一个m 然后输入m 条恒星之间的连接关系 再给定一个q  q行 如果 query x 表示查询与 x 号恒星连接并且能量最多的恒星编号 如果 destroy x y 表示破环 x恒星与y恒星 之间的连接 题解: 传统的做法是先把输入的点加入并查集建立关系,然后开始判断询问,遇到destroy就将那两个点之间的关系断开,但是很明显,这样很难做到将其连接关系断开 正难则反 我们先把未被destroy的恒星

Network Connections UVA 793(并查集)

Network Connections Time Limit: 3000ms            Memory Limit: 131072KB [PDF Link]  Network Connections  Bob, who is a network administrator, supervises a network of computers. He is keeping a log connections between the computers in the network. Ea

L - Connections in Galaxy War - zoj 3261

题意:有一个帝国在打仗,敌方会搞一些破坏,总共用N个阵地,每个阵地都有一个武力值,当第一地方收到攻击的时候他可以进行求助,当然求助的对象只能是武力值比他高的,如果求助失败就输出 ‘-1’, 求助成功就输出 帮助对象的的下标,如果有多个相同武力值的阵地输出下标最小的那个. 输入的第一行是N,表示又N个阵地(0到n-1),下面一行输入每个阵地的武力值,接着输入一个M,下面有M行,表示两点可以想通,接着有Q次查询,query 是查询这个点能不能有帮助他的,destroy表示摧毁两点之间的联系,值得注意