L - Connections in Galaxy War - zoj 3261
题意:有一个帝国在打仗,敌方会搞一些破坏,总共用N个阵地,每个阵地都有一个武力值,当第一地方收到攻击的时候他可以进行求助,当然求助的对象只能是武力值比他高的,如果求助失败就输出 ‘-1’, 求助成功就输出 帮助对象的的下标,如果有多个相同武力值的阵地输出下标最小的那个。
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
using namespace std; const int maxn = ;
const int BigNum = ; struct node
{
int op, u, v;//op等于1代表查询,等于0代表摧毁
}data[maxn*];//保存查询数据 int f[maxn], val[maxn];
//为了方便查询,把x值扩大10w倍+y值保存下来,use记录这个路是否被摧毁
int h[maxn], use[maxn]; int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
void Union(int u, int v)
{
u = Find(u), v = Find(v); if(u != v)
{
if(val[u] < val[v])
f[u] = v;
else if(val[u] > val[v])//写成了两个一样的。。。。
f[v] = u;
else if(u < v)
f[v] = u;
else
f[u] = v;
}
} int main()
{
int N, t=; while(scanf("%d", &N) != EOF)
{
int i, M, u, v, Q;
char s[]; for(i=; i<N; i++)
{
f[i] = i;
scanf("%d", &val[i]);
} scanf("%d", &M); for(i=; i<M; i++)
{
scanf("%d%d", &u, &v);
if(u > v)swap(u, v);
h[i] = u + v*BigNum;
use[i] = ;
} sort(h, h+M); scanf("%d", &Q); for(i=; i<Q; i++)
{
scanf("%s", s); if(s[] == 'd')
{
scanf("%d%d", &u, &v); if(u > v)swap(u, v);
data[i].u = u;data[i].v = v;
data[i].op = ;
int k = lower_bound(h, h+M, u+v*BigNum) - h;
use[k] = ;
}
else
{
scanf("%d", &u);
data[i].op = , data[i].u = u;
}
} for(i=; i<M; i++)
{
u = h[i] % BigNum, v = h[i] / BigNum;
if(use[i] == )
Union(u, v);
} stack<int>sta; for(i=Q-; i>=; i--)
{
if(data[i].op == )
Union(data[i].u, data[i].v);
else
{
u = Find(data[i].u); if(val[u] <= val[data[i].u])
sta.push(-);
else
sta.push(u);
}
} if(t++)printf("\n");
while(sta.size())
{
printf("%d\n", sta.top());
sta.pop();
}
} return ;
}
/*
5
1 2 3 4 5
4
0 1
1 2
2 3
3 4
5
query 0
query 1
query 2
query 3
query 4
*/
L - Connections in Galaxy War - zoj 3261的更多相关文章
- (并查集)Connections in Galaxy War -- zoj --3261 还没写
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 http://acm.hust.edu.cn/vjudge/ ...
- Connections in Galaxy War ZOJ - 3261 (并查集)
点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...
- Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边
#include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...
- 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)
这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...
- ZOJ3261:Connections in Galaxy War(逆向并查集)
Connections in Galaxy War Time Limit: 3 Seconds Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...
- Connections in Galaxy War(逆向并查集)
Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...
- Connections in Galaxy War (逆向并查集)题解
Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...
- zoj 3261 Connections in Galaxy War
点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...
随机推荐
- Day9 - Python 多线程、进程
Python之路,Day9, 进程.线程.协程篇 本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线 ...
- (转)教你如何使用php session
学会php session可以在很多地方使用,比如做一个后台登录的功能,要让程序记住用户的session,其实很简单,看了下面的文章你就明白了. PHP session用法其实很简单它可以把用 ...
- Android开源项目 Universal imageloader 源码研究之项目框架
Universal imageloader 的代码并不复杂 重点是缓存,线程池任务 下面都用UML图进行了绘制 基本使用流程就是 初始化配置,设置Options参数,最后Dispaly提交下载 pub ...
- Linux filesystem structures.
1. / – Root Every single file and directory starts from the root directory. Only root user has write ...
- (转帖) 如何將值delay n個clock? (SOC) (Verilog)
来源:http://www.cnblogs.com/oomusou/archive/2009/06/15/verilog_dly_n_clk.html /* (C) OOMusou 2009 http ...
- 对DNSPOD添加域名解析的一些见解
1.主机记录这步比较简单,输入“www”表示比较常规的域名例如www.abc.com,“@”表示abc.com,“ * ”表示泛解析,匹配所有*.abc.com的域名. 2.记录类型这步,一般常用A记 ...
- Cacti以MB为单位监控流量
Cacti自带的流量监控阀值模板为“Interface – Traffic”,只能监控bytes,在添加阀值之后,报警的流量信息以bytes为单位,查看很不友好,可以通过以下方法将btyes转换成MB ...
- DbProviderFactories.GetFactory Oracle.ManagedDataAccess.Client
因为最近项目,要使用微软的EF框架不安装Oracle客户端的情况下,访问Oracle数据库.调用如下代码的时候会报错. System.Data.Common.DbProviderFactories.G ...
- Linux下面/usr/local和opt目录
1./opt This directory is reserved for all the software and add-on packages that are not part of the ...
- Java中final关键字的用法