ZOJ - 3261 Connections in Galaxy War(并查集删边)
https://cn.vjudge.net/problem/ZOJ-3261
题意
银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可通过通道找到能量值最大的星球来帮忙。但是有一些通道被怪兽破坏了。
现在先给出原来的所有通道, 然后进行询问,询问有两种方式:
destroy a b: 连接a,b的通道被怪兽破坏了
query a: 询问a能否通过通道找到救兵,只能找能量值比自己大的救兵。
分析
逆向思维,先离线存储所有的输入操作,然后把被完全破坏之后的通道连接起来,然后再从最后一个询问往前面推,当有destroy a b时就把a,b再Union起来。
这里使用哈希思想来判断一条边是否被破坏了,注意格式。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ;
int fa[maxn];
int n,m;
struct ND{
char op[];
int a,b;
};
vector<ND> ask;
int HASH = maxn;
vector<pair<int,int> >edge;
int w[maxn];
int ans[];
map<int,bool> ma;
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy){
if(w[fx]>w[fy]){
fa[fy]=fx;
}else if(w[fx]<w[fy]){
fa[fx]=fy;
}else{
if(fx>fy){
fa[fx]=fy;
}else{
fa[fy]=fx;
}
}
}
} int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
bool flag=false;
while(~scanf("%d",&n)){
for(int i=;i<n;i++) scanf("%d",&w[i]);
for(int i=;i<=n;i++) fa[i]=i;
edge.clear();
ask.clear();
ma.clear();
scanf("%d",&m);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
edge.push_back(make_pair(x,y));
}
int q;
scanf("%d",&q);
while(q--){
ND tmp;
scanf("%s",tmp.op);
if(tmp.op[]=='q'){
scanf("%d",&tmp.a);
}else{
scanf("%d%d",&tmp.a,&tmp.b);
if(tmp.a>tmp.b) swap(tmp.a,tmp.b);
ma[tmp.a*HASH+tmp.b]=true;
}
ask.push_back(tmp);
}
q=edge.size();
for(int i=;i<q;i++){
if(ma[edge[i].first*HASH+edge[i].second]) continue;
Union(edge[i].first,edge[i].second);
}
// for(int i=0;i<n;i++) printf("%d ",fa[i]);puts("");
q=ask.size();
int cnt=;
for(int i=q-;i>=;i--){
if(ask[i].op[]=='q'){
int f=find(ask[i].a);
if(w[f]>w[ask[i].a]) ans[cnt++]=f;
else ans[cnt++]=-;
}else{
// cout<<ask[i].a<<' '<<ask[i].b<<endl;
Union(ask[i].a,ask[i].b);
// for(int i=0;i<n;i++) printf("%d ",fa[i]);puts("");
}
}
if(flag) puts("");
flag=true;
for(int i=cnt-;i>=;i--) printf("%d\n",ans[i]); }
return ;
}
ZOJ - 3261 Connections in Galaxy War(并查集删边)的更多相关文章
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...
- 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)
这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...
- 题解报告:zoj 3261 Connections in Galaxy War(离线并查集)
Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...
- zoj 3261 Connections in Galaxy War(并查集逆向加边)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)
题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...
- zoj 3261 Connections in Galaxy War
点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...
- ZOJ-3261 Connections in Galaxy War 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...
- ZOJ3261 Connections in Galaxy War 并查集
分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...
随机推荐
- rt-thread之 for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++) 理解
@2019-01-30 [小记] 利用宏 INIT_EXPORT() --- __attribute__() 将函数重新放置在自定义段,执行时去该段获取函数地址 RT-Thread 的 INIT_BO ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- 工厂方法模式(Factory Method)和抽象工厂模式(Abstact Factory)
分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的.工厂模式在<Java 与模式>中分为三类:1)简单工厂模式(Simple Facto ...
- What can Reactive Streams offer EE4J?
https://developer.lightbend.com/blog/2018-02-06-reactive-streams-ee4j/index.html By James Roper (@jr ...
- pandas 连接数据库直接查表建立dataframe。loc,sort_values数据清洗操作
#导入pandas import pandas as pd import numpy as np #导入SqlAlchemy from sqlalchemy import create_engine ...
- flask 连接MogoDB数据库
# -*- encoding: utf-8 -*- from flask import Flask,request,jsonify,render_template #导入pymongo来连接mongo ...
- cookie 常用操作
Cookie:指网站为了辨别用户身份.进行会话跟踪而储存在用户本地的 key/value 型数据(通常经过加密), cookie key/value 型数据可以由服务器端自己定义. Cookie是由服 ...
- spring cron表达式(定时器)
转: spring cron表达式(定时器) 写定时器时用到,记录一下: Cron表达式是一个字符串,字符串以5或6个空格隔开,分开工6或7个域,每一个域代表一个含义,Cron有如下两种语法 格式: ...
- PostCSS理解与运用
1.PostCSS是什么 它可以被理解为一个平台,可以让一些插件在上面跑 它提供了一个解析器,可以将CSS解析成抽象语法树 通过PostCSS这个平台,我们能够开发一些插件,来处理CSS.热门插件如a ...
- Vue(小案例)底部tab栏和顶部title栏的实现
---恢复内容开始--- 一.前言 1.底部tab栏实现 2.顶部title栏实现 二.主要内容 1.底部tab栏实现(将底部导航提取到公共的组件中) 具体效果:当点击切换不同的tab的时候,对应 ...