思路:很巧妙的解法。如果按照常规一边读入,一边合并并查集,删边实在没办法做。

首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查集。可以用一个栈保存答案。

注意:当有两个power相同的时候,选择编号更小的。输出之间有空行。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;
int p[maxn], a[maxn];
struct node{
	int x, y;
}b[maxn<<1];

struct oper{
	char s[20];
	int x, y;
}q[maxn*5];

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

void unionset(int x, int y) {
	int rx = find(x), ry = find(y);
	if(a[rx] == a[ry]) { //当二者的值相同
		if(rx < ry) p[ry] = rx;
		else p[rx] = ry;
	}
	else if(a[rx] < a[ry]) {
		p[rx] = ry;
	}
	else p[ry] = rx;
}

int main() {
	int n, m, kase = 0;
	while(scanf("%d", &n) == 1) {
		if(kase++) printf("\n");
		for(int i = 0; i <= n; ++i) p[i] = i;
		for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
		scanf("%d", &m);
		int x, y;
		for(int i = 1; i <= m; ++i) {
			scanf("%d%d", &b[i].x, &b[i].y);
			if(b[i].x > b[i].y) swap(b[i].x, b[i].y);
		}
		int Q;
		scanf("%d", &Q);
		getchar();
		map<PI, int>ha;
		char s[30];
		for(int i = 1; i <= Q; ++i) {
			fgets(s, sizeof(s), stdin);
			if(s[0] == 'd') {
				sscanf(s, "%s%d%d", &q[i].s, &q[i].x, &q[i].y);
				if(q[i].x > q[i].y) swap(q[i].x, q[i].y);
				PI pi = make_pair(q[i].x, q[i].y);
				ha[pi] = 1;
				//printf("%s %d %d\n", q[i].s, q[i].x, q[i].y);
			}
			else {
				sscanf(s, "%s%d", &q[i].s, &q[i].x);
				//printf("%s %d\n", q[i].s, q[i].x);
			}
		}
		//建立并查集
		for(int i = 1; i <= m; ++i) {
			if(!ha.count(make_pair(b[i].x, b[i].y))) {
				unionset(b[i].x, b[i].y);
			}
		}
		//逆向
		stack<int>ans;
		for(int i = Q; i > 0; --i) {
			if(q[i].s[0] == 'q') {
				int r = find(q[i].x);
				if(a[r] <= a[q[i].x]) ans.push(-1);
				else ans.push(r);
			}
			else unionset(q[i].x, q[i].y);
		}
		while(!ans.empty()) {
			printf("%d\n", ans.top());
			ans.pop();
		}
	}
	return 0;
} 

如有不当之处欢迎指出!

ZOJ - 3261 逆向并查集的更多相关文章

  1. zoj 3261 逆向并查集+离线处理

    题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...

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

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

  3. HDU - 4496 City 逆向并查集

    思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...

  4. HDU_4496_逆向并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 逆向并查集,先读取,然后从后向前join每次保存答案即可. #include<iostream> ...

  5. 逆向并查集 hrbust 1913

    #include<iostream> //由于拆除并查集的方法太难或者没有#include<cstdio> //可以先将所有没有拆的桥连接 再逆向操作 断开变成连接 反向输出# ...

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

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

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

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

  8. BZOJ 1016 星球大战starwar(逆向-并查集)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1015 题意:给出一个图.每次删掉一个点,求删掉之后连通块个数. 思路:正着做不好做,我们 ...

  9. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

随机推荐

  1. 亲测可用的国内maven镜像

    maven作为一个项目管理工具确实非常好用,之前oschina的中央仓库可用,现在oschina的maven服务器关了,于是自己倒腾了一个nexus,苦于自己的服务器是入门级的,下载速度实在让人着急. ...

  2. 前端工程构建工具FIS3

    FIS3 是面向前端的工程构建工具.解决前端工程中性能优化.资源加载(异步.同步.按需.预加载.依赖管理.合并.内嵌).模块化开发.自动化工具.开发规范.代码部署等问题. 一.安装 全局安装fis3 ...

  3. 李忠益TP5商城项目笔记(待完成)

    商品种类的无限极分类 $data=db('goods_type')->field(['*','concat(path,",",id)'=>'paths'])->o ...

  4. 【转】sed 的参数

    一.15个参数 1.r 从文件读入 [root@watchout2 ~]# cat file12345 [root@watchout2 ~]# cat newfile abcde [root@watc ...

  5. H3c交换机配置端口镜像详情

    端口镜像 需要将G0/0/1口的全部流量镜像到G0/0/2口,即G0/0/1为源端口,G0/0/2为目的端口. 配置步骤 1.进入配置模式:system-view: 2.创建本地镜像组:mirrori ...

  6. linux基本语法和常用运维命令

    linux上的操作一般是命令行操作,看起来很高大上,让人畏而远之. Help!Help! 忽然间闯入的linux黑黑的世界,怎么办,不要慌.赶紧敲出一个help命令,然后回车,黑色的窗口就会展示一些常 ...

  7. thinkphp使用自带webserver

    进入命令行,进入 tp5/public 目录后,输入如下命令:php -S localhost:8888 router.php 然后进行访问

  8. ABP官方文档翻译 6.4 导航

    导航 创建菜单 注册导航提供者 显示菜单 每一个网络应用都会有一些菜单用来在pages/screens之间导航.ABP提供了通用的基础设施来创建并显示菜单. 创建菜单 应用可以由不同的模块组成,每一个 ...

  9. 突然觉得前端js挺不错的

    由于工作的需要,现在对前端技术有一定的要求,但是发现普遍前端技术都比较弱,遇到问题基本上没有人能帮助你解决,只好自己学习了. 从事java开发已经好久了,真的是好久了,但是水平一直还是一般吧. 从今天 ...

  10. js 面向对象 继承

    继承方式有四种: 1.call 2.apply 3.prototype 4.for in call 和 apply 的主要区别: call 传参数只能一个一个的传, apply 因为是用数组,所以可以 ...