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



题目:给出一些点,每个点有权值,然后有一些边,相连。无向的。然后有一些操作

query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大)

destory a,b 表示删除连接a,b的边

思路并查集,但是要逆向处理,所以先离线读入,从后向前处理,于是对于destroy操作,等价于连接两个点的操作,然后对于每个询问输出即可

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std;  

const int maxn = 10000 + 100;
const int INF = 0x3f3f3f3f;
int val[maxn];
int n, m, q;
set<pair<int, int> > edge;
stack<char> op;
stack<int> query;
stack<pair<int, int> > destroy;
stack<int> ans;
int fa[maxn];
int kase = 0;

int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void init() {
	edge.clear();
	for(int i = 0; i < n; i++) fa[i] = i;
	for(int i = 0; i < n; i++) cin >> val[i];
	cin >> m;
	while(m--) {
		int u, v; cin >> u >> v;
		edge.insert(make_pair(u, v));
	}
	cin >> q;
	while(q--) {
		char cmd[7]; cin >> cmd;
		int u, v;
		if(cmd[0] == 'q') {
			op.push('q');
			cin >> u;
			query.push(u);
		}
		else {
			op.push('d');
			cin >> u >> v;
			if(edge.count(make_pair(u, v))) edge.erase(make_pair(u, v));
			if(edge.count(make_pair(v, u))) edge.erase(make_pair(v, u));
			destroy.push(make_pair(u, v));
		}
	}
}

void solve() {
	if(kase++) cout << endl;
	for(set<pair<int, int> >::iterator it = edge.begin(); it != edge.end(); it++) {
		int u = (*it).first, v = (*it).second;
		if(val[find(u)] > val[find(v)]) {
			fa[fa[v]] = fa[u];
		}
		else if(val[fa[u]] == val[find(v)]) {
			if(fa[u] < fa[v]) fa[fa[v]] = fa[u];
			else fa[fa[u]] = fa[v];
		}
		else {
			fa[fa[u]] = fa[v];
		}
	}
	char cmd;
	while(!op.empty()) {
		cmd = op.top(); op.pop();
		if(cmd == 'q') {
			int qu = query.top(); query.pop();
			if(val[find(qu)] > val[qu]) ans.push(fa[qu]);
			else ans.push(-1);
		}
		else {
			pair<int, int> tmp = destroy.top(); destroy.pop();
			int u = tmp.first, v = tmp.second;
			if(val[find(u)] > val[find(v)]) {
				fa[fa[v]] = fa[u];
			}
			else if(val[fa[u]] == val[find(v)]) {
				if(fa[u] < fa[v]) fa[fa[v]] = fa[u];
				else fa[fa[u]] = fa[v];
			}
			else {
				fa[fa[u]] = fa[v];
			}
		}
	}
	while(!ans.empty()) {
		cout << ans.top() << endl;
		ans.pop();
	}
}

int main() {
	//freopen("input.txt", "r", stdin);
	while(scanf("%d", &n) == 1) {
		init();
		solve();
	}
}
时间: 2024-10-11 11:06:01

zoj 3261 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

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 (逆向+带权并查集)

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

ZOJ 3261 Connections in Galaxy War

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意: 一. 给定N个星球(0,...,N-1)及其对应的power(p0,...pn-1) 二. 给定M条星球a与b间(a!=b)的通路 三. 给出Q个指令,"destroy a b"代表此时星球a b间的通路(必定已连接)被破坏了,"query a"要求输出当时应对星球a进行救援的星球的编号 规则为: ①与星球a有直接或间接通路的星

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

https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可通过通道找到能量值最大的星球来帮忙.但是有一些通道被怪兽破坏了. 现在先给出原来的所有通道, 然后进行询问,询问有两种方式: destroy a b: 连接a,b的通道被怪兽破坏了 query a: 询问a能否通过通道找到救兵,只能找能量值比自己大的救兵. 分析 逆向思维,先离线存储所有的输入操作,

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; bo

HLJU 1105 cpc 喵喵的拆分集合 (并查集的逆向操作)

1105: 喵喵的拆分集合 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 37  Solved: 8 [Submit][Status][Web Board] Description 众所周知,DoubleQ是DS(Data Structure)粉,她最爱DS了.现在她要实现一个神奇的DS,支持下列两个操作: -删除某条边,表示为"D x",即为删除第x条边 -查询两点是否属于一个集合,表示为"Q a b",即为查询节

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