题目链接:

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. 【css】清除浮动的几种方式

    [css]清除浮动的几种方式   因为浮动框不在普通的文档流中,所以它不占据空间.如下面的代码: .news { background-color:gray; border:1px solid bla ...

  2. [转]Vue.js 目录结构

    本文转自:http://www.runoob.com/vue2/vue-directory-structure.html 上一章节中我们使用了 npm 安装项目,我们在 IDE(Eclipse.Ato ...

  3. [转]WxEmojiView

    本文转自:https://github.com/icindy/WxEmojiView 来源信息 author: Di (微信小程序开发工程师) organization: WeAppDev(微信小程序 ...

  4. python——高级特性(2)

    迭代 在python中迭代是通过for ....in...完成的,只要是可迭代对象都可以迭代 #!usr/bin/python #-*- coding:UTF-8 -*- #tuple迭代 t=[(1 ...

  5. 记一次吐血的暴力模拟qaq 【多项式输出】

    题目描述 一元 n 次多项式可用如下的表达式表示: 其中,aixi称为 i 次项,ai 称为 i 次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 1. 多项式中 ...

  6. Cheatsheet: 2017 07.01 ~ 07.31

    Other 8 Key Application Performance Metrics & How to Measure Them The Code Review: The Most Impo ...

  7. IDEA快捷键使用说明

    sout :  输出打印语句 System.out.println(); "内容".sout  : 也是打印 System.out.println("内容"); ...

  8. mysql 中显示 table 的基本信息

    mysql> show table status like 'j_position' \G . row *************************** Name: j_position ...

  9. csharp: Linq keyword example

    /// <summary> /// http://www.dotnetperls.com/linq /// </summary> public partial class Li ...

  10. Java基础之引用(String,char[],Integer)总结于牛客网的专项练习题

    1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...