题目链接:

https://cn.vjudge.net/problem/ZOJ-3261

题目大意:

给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,
query a 询问与a直接或者间接想连的点中最大权值的是那个点,输出那个点,如果那个点的权值小于等于a的权值,那么就输出-1,还有另一种操作就是destroy a b意思是删除a b的关系。

解题思路:

此处需要删边,应该想到逆序离线处理。先将所有需要删除的边直接删除,将所有操作存下来逆序处理,对于需要删的边就变成添加这条边。求与a相连的权值最大的点,就直接求a这个连通块中的最大权值的点,可以用并查集求,添加边的时候就合并两个连通块即可。

注意合并连通块时,权值大的为父亲,如果权值相同,那么编号小的为父亲

在最后判断是否存在为-1的时候,需要用连通块根节点的权值和当前询问节点权值进行比较,不可用下标相等比较

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
int n, m;
int a[maxn];//每个点的权值
vector<int>Map[maxn];//存边(只保留编号小的指向编号大的边)
struct node//存储操作
{
int type, x, y;
}op[maxn];
vector<int>ans;
int fa[maxn];
int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
void Union(int x, int y)
{
x = Find(x), y = Find(y);
if(a[x] < a[y])
{
fa[x] = y;
}
else if(a[x] > a[y])
{
fa[y] = x;
}
else if(x != y)
{
if(x < y)
{
fa[y] = x;
}
else
{
fa[x] = y;
}
}
}
int main()
{
int flag = ;
while(scanf("%d", &n) != EOF)
{
if(flag++)printf("\n");
memset(op, , sizeof(op));
memset(a, , sizeof(a));
ans.clear();
for(int i = ; i < n; i++)scanf("%d", &a[i]), Map[i].clear(), fa[i] = i; scanf("%d", &m);
int u, v;
while(m--)
{
scanf("%d%d", &u, &v);
if(u > v)swap(u, v);
Map[u].push_back(v);
}
scanf("%d", &m);
char s[];
for(int i = ; i <= m; i++)
{
scanf("%s", s);
if(s[] == 'q')
op[i].type = , scanf("%d", &op[i].x);
else //先将边全部删除,逆序操作,逐渐增加边
{
op[i].type = ;
scanf("%d%d", &u, &v);
if(u > v)swap(u, v);
op[i].x = u, op[i].y = v;
Map[u].erase(find(Map[u].begin(), Map[u].end(), v));
}
}
for(int i = ; i < n; i++)
{
for(int j = ; j < Map[i].size(); j++)
{
Union(i, Map[i][j]);
}
}
for(int i = m; i >= ; i--)
{
if(op[i].type == )
{
u = Find(op[i].x);
if(a[u] <= a[op[i].x])u = -;
//不能仅仅判断u != op[i].x 因为根节点可能为其他点并且权值等于该节点权值,此时该节点也应该认为无连接
ans.push_back(u);
}
else
{
Union(op[i].x, op[i].y);
}
}
for(int i = ans.size() - ; i >= ; i--)
printf("%d\n", ans[i]);
}
return ;
}

ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集的更多相关文章

  1. ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)

    题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...

  2. 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

    这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...

  3. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  4. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...

  5. 题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

    Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...

  6. zoj 3261 Connections in Galaxy War(并查集逆向加边)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...

  7. Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边

    #include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...

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

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

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

    https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...

随机推荐

  1. IOS贝塞尔曲线圆形进度条和加载动画

    做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加.其中主要用到贝塞尔曲线.UIBezierPath是对CGContextRef的进一步 ...

  2. JS获取当前屏幕宽高

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  3. easyui 带参数的datagride

    <table id="tt" style="width:100%;height:355px" url="../aowei/Handler/Han ...

  4. 多线程-lock锁

    package 多线程.lock锁; import java.util.concurrent.locks.ReentrantLock; /*. * * //同步代码块 * * */ public cl ...

  5. 记Spring与跨域

    跨域 简单理解就是跨域名 (ip+端口) 在 52liming.com 中向demo.com中发起Ajax请求, 出于安全考虑会进行拦截 参考: 浏览器的同源策略 什么是JS跨域访问? 跨域资源共享 ...

  6. [LeetCode]Swap Nodes in Pairs题解

    Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  7. python中logging日志基本用法,和进程安全问题

    低配版 import logging logging.debug('debug message') # 调试模式 logging.info('info message') # 正常运转模式 loggi ...

  8. 安装和使用phpstorm

    下载网址:https://www.jetbrains.com/phpstorm/ 第一步:解压在官网上所下的文件,双击 Phpstorm10.0.3.exe 第二步:安装目录选择,自定义选择安装目录, ...

  9. 前端之困 · XSS CookBook

    方法论 发掘漏洞的时间要具体到是检测什么目标了,找 Google 的,和找腾讯的时间肯定不会一样. 至于是如何发现的,不同类型的 XSS 漏洞,可能不尽相同. 反射型 以及一些 DOM 型,一般建议是 ...

  10. 如何在iview组件中使用jsx

    最近选用的框架iview表单组件的render写法让人有点不习惯,尤其是在写比较复杂的逻辑的时候,还是感觉模板式的写法比较方便且可读性较强.而render函数除了支持配置写法外,还支持jsx的写法.由 ...