题目链接:https://cn.vjudge.net/problem/ZOJ-3261

题意

有n个星星,之间有m条边

现一边询问与x星连通的最大星的编号,一边拆开一些边

思路

一开始是真不会,甚至想用dfs模拟

最后查了一下,这个题原来是要离线操作,拆边就变为合并

这很为难哈哈,本以为有个什么更好的数据结构(动态树?)

存边我们用一个set<int>来存一个数字即可(bfs这类写多了就很容易考虑到压缩数据

还有一个重要的点,就是并查集的join可以用来维护一个最大(小)数据作为跟节点的值

代码

#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10000, maxq=50000;
struct IdxMap{
int val, idx;
IdxMap(int val=0, int idx=0):
val(val), idx(idx) {}
bool operator < (const IdxMap &a) const{
if (val!=a.val) return val>a.val;
return idx<a.idx;
}
}imap[maxn+5];
struct Operate{
// true for destory(add)
bool ifadd; int a, b;
Operate(bool ifadd=false, int a=-1, int b=-1):
ifadd(ifadd), a(a), b(b) {}
}operate[maxq+5];
struct Node{
int pre, data;// rank, data;
Node(int pre=0, int data=0):// int rank=0, int data=0):
pre(pre), data(data) {}// rank(rank), data(data) {}
}node[maxn+5];
int n, m, q, asize=0, ans[maxq+5];
set<int> edge; // smeller proir int find(int x){
return (node[x].pre==x)?x:(node[x].pre=find(node[x].pre));
} void join(int a, int b){
a=find(a); b=find(b);
if (a==b) return;
// if (node[a].rank==node[b].rank) node[a].rank++;
if (node[a].data>=node[b].data) node[b].pre=a;
else node[a].pre=b;
} inline int getcode(const int &a, const int &b){
if (a<b) return a*maxn+b;
return b*maxn+a;
} int findBiggest(int x){
// int &val=node[x].data;
// for (int i=0; i<n; i++){
// if (val>=imap[i].val) return -1;
// if (find(x)==find(imap[i].idx)) return imap[i].idx;
// }return -1;
int root=find(x);
if (node[root].data>node[x].data) return root;
return -1;
} int main(void){
int first=true;
while(scanf("%d", &n)==1 && n){
edge.clear();
asize=0; for (int i=0, tmp; i<n; i++){
scanf("%d", &tmp);
imap[i]=IdxMap(tmp, i);
node[i]=Node(i, tmp);
}sort(imap, imap+n);
scanf("%d", &m);
for (int i=0, a, b; i<m; i++){
scanf("%d%d", &a, &b);
edge.insert(getcode(a, b));
} char str[25];
scanf("%d", &q);
for (int i=0, a, b; i<q; i++){
scanf("%s", str);
if (str[0]=='d'){
scanf("%d%d", &a, &b);
operate[i]=Operate(true, a, b);
edge.erase(getcode(a, b));
}else if (str[0]=='q'){
scanf("%d", &a);
operate[i]=Operate(false, a, -1);
}
} for (set<int>::iterator it=edge.begin(); it!=edge.end(); it++)
join((*it)/maxn, (*it)%maxn); for (int i=q-1; i>=0; i--){
if (operate[i].ifadd) join(operate[i].a, operate[i].b);
else ans[asize++]=findBiggest(operate[i].a);
}
if (!first) printf("\n");
else first=false;
for (int i=asize-1; i>=0; i--) printf("%d\n", ans[i]);
} return 0;
}
Time Memory Length Lang Submitted
260ms 2124kB 2764 C++ (g++ 4.7.2) 2018-03-20 23:26:36

> 18-03-21 Update:并查集维护最值

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

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

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

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

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

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

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

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

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

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

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

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

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

  7. zoj 3261 Connections in Galaxy War

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

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

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

  9. ZOJ3261 Connections in Galaxy War 并查集

    分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...

随机推荐

  1. (六)api网关服务 zuul-过滤器

    开启上文服务: Zuul给我们的第一印象通常是这样:它包含了对请求的路由和过滤两个功能,其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础.过滤器功能则负责对请求的处理过 ...

  2. MFC补码原码反码转换工具

    /*_TCHAR str[100] = { 0 }; wsprintf(str, _T("%d"),num);*/ ; CString str; m_edit1.GetWindow ...

  3. BZOJ 3165 李超线段树

    思路: 李超线段树 我是把线段转成斜率的形式搞得 不知道有没有更简单的方法 //By SiriusRen #include <cmath> #include <cstdio> ...

  4. Linux 安装软件的几种方式

    目录 几种安装方式 源代码编译安装 借助软件包管理器安装 二进制格式安装 总结 参考 几种安装方式 源代码编译安装 源代码包的安装一般为下载软件源代码,然后编译安装.常见的 C 程序软件的安装步骤是 ...

  5. mongodb报错:connection refused because too many open connections: 819

    问题: 发现mongodb无法连接,查看mongodb日志,出现大量的如下报错: [initandlisten] connection refused because too many open co ...

  6. 登录生成令牌token存于redis

    package com.medic.rest.province.base.home; import java.util.HashMap;import java.util.List;import jav ...

  7. 【BZOJ4487】【JSOI2015】染色问题

    题意: 棋盘是一个n×m的矩形,分成n行m列共n*m个小方格.现在萌萌和南南有C种不同颜色的颜料,他们希望把棋盘用这些颜料染色,并满足以下规定:  1.  棋盘的每一个小方格既可以染色(染成C种颜色中 ...

  8. javaweb集成swagger

    一.添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  9. autosar

    AUTOSAR – RTE(1)基本概念 1. RTE概述 The Run-Time Environment (RTE) is at the heart of the AUTOSAR ECU arch ...

  10. 学习爬虫:《Python网络数据采集》中英文PDF+代码

    适合爬虫入门的书籍<Python网络数据采集>,采用简洁强大的Python语言,介绍了网络数据采集,并为采集新式网络中的各种数据类型提供了全面的指导.第一部分重点介绍网络数据采集的基本原理 ...