首页 > 其他分享 >ZOJ 3261 Connections in Galaxy War (并查集+离线处理)

ZOJ 3261 Connections in Galaxy War (并查集+离线处理)

时间:2023-02-03 11:03:45浏览次数:55  
标签:star int ZOJ 离线 Connections 命令 query include coms


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 dimension. Then many problems were raised when some of the stars wanted to seek help from the others.

In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star A directly or indirectly. In addition, this star should be more powerful than the star A. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes star A couldn't find such star for help.

Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.

 

Input

 

There are no more than 20 cases. Process to the end of file.

For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line contains N integers p0p1, ... , pn-1 (0 <= pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers ab (0 <= ab <= N - 1, a != b), which means star a and star b has a connection tunnel. It's guaranteed that each connection will only be described once.

In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line will be written in one of next two formats.

"destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a and star b was available before the monsters' attack.

"query a" - star a wanted to know which star it should turn to for help

There is a blank line between consecutive cases.

Output

For each query in the input, if there is no star that star a can turn to for help, then output "-1"; otherwise, output the serial number of the chosen star.

Print a blank line between consecutive cases.

 

Sample Input

 

2 10 20 1 0 1 5 query 0 query 1 destroy 0 1 query 0 query 1

 

Sample Output

 

1 -1 -1 -1

     有n个点和m条边,每个点带有一个权值p[i]。现在给出Q条命令,要你输出对应的答案。命令格式如下:

        query u :该命令需要输出当前与u点相连的点编号x,x要满足p[x]是所有与u相连的点中最大的 且 p[x]>p[u]。如果有多个满足条件的x存在,那么就输出编号最小的那个x点的编号。

        destroy u v:该命令将删除u与v的边。(保证在执行该命令前u与v之间有一条边)

 每个点看成并查集中的一个点,那么query u的时候就是查找u所属的连通分量中p[]值最大的点编号。所以我们可以始终让一个连通分量的根就是那个p[]值最大的点 且 在合并连通分量的时候依然维持这一性质。

        由于并查集只能增加边,而题目的命令却只有删除边,所以自然想到将所有命令先预读入内存,然后从最后一条命令往前,一条一条添加边处理。

        具体处理过程如下:

        首先我们读入M条边,但是此时先不连接任何边。接着我们读入每条命令,并且将会被删除的边标记出来。然后我们将那些没有被删除的边都用并查集连接起来。

        然后我们逆序处理每条命令(从最后一条命令开始),如果是query命令就是返回该分量的根编号(还需要判断根的p值是否>u的p值)。如果是destory u v 命令,其实就是添加u v边的命令。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
const int INF = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;

const int maxn=10000+5;

int p[maxn];//power值

//并查集
int fa[maxn];
int findset(int x)
{
return fa[x]==-1? x:fa[x]=findset(fa[x]);
}
int bind(int u,int v)
{
int fu=findset(u);
int fv=findset(v);
if(fu != fv)
{
if(p[fu]>p[fv] || (p[fu]==p[fv] && fu<fv) )//fu是根
{
fa[fv]=fu;
}
else//fv是根
{
fa[fu]=fv;
}
return 1;
}
return 0;
}

//边
struct Edge
{
int u,v;
Edge() {}
Edge(int u,int v):u(u),v(v) {}

bool operator<(const Edge &rhs)const
{
return u<rhs.u || (u==rhs.u && v<rhs.v);
}
} edges[20000+5];
bool vis[20000+5];

//命令
struct command
{
int type;
int v;
} coms[50000+5];

int main()
{
int n,m,Q;
bool first=true;
while(scanf("%d",&n)==1)
{
if(!first)
printf("\n");
first=false;
for(int i=0; i<n; i++)
scanf("%d",&p[i]);
scanf("%d",&m);
map<Edge,int> mp;//边与边的编号的映射
for(int i=0; i<m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(u>v)
swap(u,v);
edges[i]=Edge(u,v);//可优化
mp[edges[i]]=i;
}
memset(vis,0,sizeof(vis));//vis[i]==true表第i条边已被删除
scanf("%d",&Q);
for(int i=0; i<Q; i++)
{
char str[100];
int u,v;

scanf("%s",str);
if(str[0]=='q')
{
scanf("%d",&u);

coms[i].type=0;
coms[i].v=u;
}
else if(str[0]=='d')
{
scanf("%d%d",&u,&v);
if(u>v) swap(u,v);
int id=mp[Edge(u,v)];//获取对应边的编号

vis[id]=1;//删除此边
coms[i].type = 1;
coms[i].v=id;
}
}
//连通所有未被删除的边
memset(fa,-1,sizeof(fa));
for(int i=0; i<m; i++)if(vis[i]==false)
{
bind(edges[i].u,edges[i].v);
}
//逆序处理所有命令并将query结果保存在vc中
vector<int> vc;
for(int i=Q-1; i>=0; i--)
{
if(coms[i].type == 0)//query命令
{
int root = findset(coms[i].v);
if(p[root]>p[coms[i].v])
vc.push_back(root);
else
vc.push_back(-1);
}
else//destroy命令
{
int u=edges[coms[i].v].u, v=edges[coms[i].v].v;
bind(u,v);
}
}
for(int i=vc.size()-1; i>=0; i--)
printf("%d\n",vc[i]);
}
return 0;
}

 

标签:star,int,ZOJ,离线,Connections,命令,query,include,coms
From: https://blog.51cto.com/u_15952369/6035583

相关文章

  • BZOJ3262 陌上花开(cdq分治模板)
    Description有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),用三个整数表示。现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。定义一朵花A比另一朵......
  • 离线分治(cdq)
    离线分治主要是一类用于解决数据结构类型题目的算法,顾名思义,这种算法是一类离线解决问题的分治算法.离线,意味着题目必须能够提前预知整个操作序列而不是只能回答一个操作后......
  • BZOJ 3224 普通平衡树 (BST+Treap)
    题目描述:您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:插入数值x。删除数值x(若有多个相同的数,应只删除一个)。查询数值x的排名(若有多个相同的数,应......
  • BZOJ1503 郁闷的出纳员 (Treap)
    DescriptionOIER公司是一家大型专业化软件公司,有着数以万计的员工。作为一名出纳员,我的任务之一便是统计每位员工的工资。这本来是一份不错的工作,但是令人郁闷的是,我们......
  • 下载ansible离线包
    安装源[root@TimeSync~]#yuminstall-yyum-utilsepel-release创建目录[root@TimeSync~]#mkdir-p/root/ansible下载软件[root@TimeSync~]#yuminstall-y--d......
  • 离线安装docker
    1、先下载docker的安装包下载地址:https://download.docker.com/linux/static/stable/x86_64/这里我们下载docker-19.03.9.tgz,然后上传到服务器上解压tar-zxvfdocker-1......
  • linux离线部署python项目
    离线部署直接在内网隔离的环境中。不能直接pipinstall或者apt-getinstall(Ubuntu、Debain)准备:与离线环境相同版本的服务器Python(web)项目依赖pipwheel强大的pip命......
  • 离线强化学习在序列决策中的应用
    从样本利用效率,看强化学习的分类on-policy:每次更新策略需要在重新收集数据,更新数据来自于当前策略,行为策略和目标策略是同一个策略off-policy:行为策略和目标策略不......
  • BZOJ 1852 [MexicoOI06] 最长不下降序列
    https://darkbzoj.cc/problem/1852首先解决初始排序的问题:先把\(i,j\)对应的两组数\((a_i,b_i),(a_j,b_j)\)分为“必要”,“非必要”两类。“必要”,指\(i\)必须......
  • 百度离线地图地点搜索 离线地图poi搜索
    1.场景和需求:在局域网开发的web项目,不能连接公网1需要使用离线地图展示设备点位;2需要实现地图的城市范围内的离线搜索,可以检索到百度地图上的点位,类似与百度地图首......