ZOJ3261(Connections in Galaxy War)

题目传送门

在这里插入图片描述

题意

大概意思就是有n个星球,每个星球都有自己的力量,然后接下来有m条双向的边,连接两点顶点,如果a 连接 b,b 连接 c,那么可以认为a 和 c也是连接的。接下来有q次操作,操作分为两种:

  1. query操作,查询和x连接的力量最强的星球是哪个,如果有多个力量一样大的找出编号小的
  2. destroy操作,删除x 和 y相连接的边。
思路

这道题还是挺不错的一道并查集题目,首先需要想到一个小小的启发式合并,力量小的星球合到力量大的星球上。如果存在力量相同的情况就按照编号的大小,将大的合到小的上。其次这道题最最最最主要的思想还不是这里,并查集只是一个手段,最核心的思路是要逆向并查集

  1. s数组存储节点的父节点
  2. a数组存储每个星球的力量
  3. v数组存储操作和节点,这里比较巧。
  4. set数组集合存储当前需要合并的边,比较方便而且删除插入复杂度都是log(N)。
  5. ans向量存储答案,也可以用数组。
    所谓逆向就是先将所有的操作全部记下来,然后将没有destory的边节点全部合并到一个并查集中,然后倒过来做q次操作,也就是从最后一次操作开始。**如果是query操作那么就按照要求查询,如果是destroy操作那么就将节点合并。**最后直接从后往前遍历向量容器就可以得到答案了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 10005;
int s[maxn];
int a[maxn];
int v[50005][2];
set<int>st[maxn];
vector<int>ans;
inline void clear_set()
{
	for(int i = 0;i < maxn;i++){
		s[i] = i;
		st[i].clear();
	}
	memset(v,0,sizeof(v));
	ans.clear();
}
inline int find_set(int x)
{
	int r = x;
	while(r != s[r]){
		r = s[r];
	}
	while(x != r){
		int j = s[x];
		s[x] = r;
		x = j;
	}
	return r;
}
inline void union_set(int x,int y)
{
	int fx = find_set(x);
	int fy = find_set(y);
	if(fx != fy){
		if(a[fx] > a[fy]){			//力量小的合到力量大的下面 
			s[fy] = fx;
		}
		else if(a[fx] < a[fy]){
			s[fx] = fy;
		}
		else if(a[fx] == a[fy]){
			if(fx < fy){			//序号大的合到序号小的下面 
				s[fy] = fx;
			}
			else{
				s[fx] = fy;
			}
		}
	}
}
int main()
{
	int n,m,q,k = 0;
	while(~scanf("%d",&n)){
		if(k++){
			printf("\n");
		}
		clear_set();						//初始化 
		for(int i = 0;i < n;i++){			//输入力量 
			scanf("%d",&a[i]);
		}
		int x,y;
		scanf("%d",&m);
		for(int i = 0;i < m;i++){
			scanf("%d%d",&x,&y);
			if(x > y)		swap(x,y);		
			st[x].insert(y);				//代表x y之间有一条边 
		}
		scanf("%d",&q);
		for(int i = 0;i < q;i++){
			char str[15];
			scanf("%s",str);
			if(str[0] == 'q'){
				scanf("%d",&x);
				v[i][0] = x;v[i][1] = -1;	//-1代表这次是query操作 
			}
			else{
				scanf("%d%d",&x,&y);
				if(x > y)		swap(x,y);
				v[i][0] = x;v[i][1] = y;	
				st[x].erase(y);				//删掉这条边 
			}
		}
		
		for(int i = 0;i < n;i++){			//合并没有删除的边。 
			set<int>::iterator p;
			for(p = st[i].begin();p != st[i].end();p++){
				union_set(i,*p);
			}
		}
		for(int i = q-1;i >= 0;i--){		//倒过来查询,正向删除就变成了反向合并。 
			if(v[i][1] == -1){				//这是query操作,看上面 
				int fx = find_set(v[i][0]);
				if(a[fx] > a[v[i][0]]){		
					ans.push_back(fx);
				}
				else{						
					ans.push_back(-1);
				}
			}
			else{							//合并两个节点 
				union_set(v[i][0],v[i][1]);
			}
		}
		for(int i = ans.size()-1;i >= 0;i--){
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}

愿你走出半生,归来仍是少年~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值