ZOJ - 3261 逆向并查集
思路:很巧妙的解法。如果按照常规一边读入,一边合并并查集,删边实在没办法做。
首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查集。可以用一个栈保存答案。
注意:当有两个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 逆向并查集的更多相关文章
- zoj 3261 逆向并查集+离线处理
题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- HDU - 4496 City 逆向并查集
思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...
- HDU_4496_逆向并查集
http://acm.hdu.edu.cn/showproblem.php?pid=4496 逆向并查集,先读取,然后从后向前join每次保存答案即可. #include<iostream> ...
- 逆向并查集 hrbust 1913
#include<iostream> //由于拆除并查集的方法太难或者没有#include<cstdio> //可以先将所有没有拆的桥连接 再逆向操作 断开变成连接 反向输出# ...
- Connections in Galaxy War (逆向并查集)题解
Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...
- ZOJ3261:Connections in Galaxy War(逆向并查集)
Connections in Galaxy War Time Limit: 3 Seconds Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...
- BZOJ 1016 星球大战starwar(逆向-并查集)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1015 题意:给出一个图.每次删掉一个点,求删掉之后连通块个数. 思路:正着做不好做,我们 ...
- HDU 4496 D-City(逆向并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...
随机推荐
- _2_head_中标签
创:20_3_2017修:5_4_2017 什么是title标签? --title 页面名(双) -- 整个html的页面名字,相当于一本书的书名 <title>北门吹雪</ti ...
- iOS UImage 与 RGB 裸数据的相互转换
iOS UImage 与 RGB 裸数据的相互转换 Touch the data of image in iOS Get data from a image 较简单,根据已有的 image 的属性,创 ...
- Linxu指令--date,cal
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(3)-前端实现
转载自:http://www.cnblogs.com/Auyuer/p/8086975.html MoonLight可视化订单需求区域分析系统实现功能: 在现实生活中,计算机和互联网迅速发展,人们越来 ...
- MySQL中判断日期间隔的方法
MySQL中查询一定时间间隔内的数据的方法比较常用,可以使用TO_DAYS.DATE_SUB等函数来实现. TO_DAYS函数的作用是返回指定日期从0年开始计算的天数. DATE_SUB函数的作用是通 ...
- html页面不显示中文
问题:HTML编辑中文后无法在网页上显示中文 原因:是适用的 Cufon字体包不支持中文造成的. 简单粗暴的解决方法:如果你本来打算是去掉Cufon,可以看一看<如何禁用Cufon功能>. ...
- js中的监听事件总结
javascript事件与功能说明大全:http://tools.jb51.net/table/javascript_event 1.滚动条监听事件 例1:监听滚动条距离页面顶端距离 <scri ...
- Node.js--安装express以及创建第一个express项目(windows)
1.根据新版的express出现了安装器的概念,安装express需要两个步骤(命令行找到nodejs目录全局安装): (1)npm install -g express@4.15.0 (也可省略 ...
- Azure Functions + Azure Batch实现MP3音频转码方案
客户需求 客户的环境是一个网络音乐播放系统,根据网络情况提供给手机用户收听各种码率的MP3歌曲,在客户没购买歌曲的情况下提供一个三十秒内的试听版本.这样一个系统非常明确地一个需求就是会定期需要将一批从 ...
- Model和ModelAndView
在请求处理方法可出现和返回的参数类型中,最重要的就是Model和ModelAndView.对于MVC框架,控制器Controller执行业务逻辑,用于产生模型数据Model,而试图View则用于渲染模 ...