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 ...
随机推荐
- Android音频开发之——如何播放一帧音频
本文重点关注如何在Android平台上播放一帧音频数据.阅读本文之前,建议先读一下<Android音频开发(1):基础知识>,因为音频开发过程中,经常要涉及到这些基础知识,掌握了这些重要的 ...
- codevs1044四子连棋(Dfs)
/* 数据范围太小 暴力暴力 Dfs直接 终止条件嘛 就是4中目标棋局 挨着枚举一遍就好了 搜索的起点一定是空格 当然 空格周围有黑有白 黑先走或者白先走答案可能不一样 所以 维护一个b 表示这一步走 ...
- java之JDBC
java之JDBC 一.什么是JDBC Java 数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提 ...
- require.js的使用的坑!
require.js的使用心德: 都是自我的理解所得: first:为什么使用? 1,web开发js的占用比例越来越大,引入的插件也越来越多,维护困难,一个一个的script的写要废 2,模块开发的需 ...
- springmvc的几点见解
@Controller //标注在Bean的类定义处 @RequestMapping //可以标注在类定义处,将Controller和特定请求关联起来. //可以标注在方法签名处,以便进一步对请求进行 ...
- (转)echo和print的区别
在实际使用中, print 和 echo 两者的功能几乎是完全一样.可以这么说,凡是有一个可以使用的地方,另一个也可以使用.但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出多 ...
- 利用TOAD实现把EXCEL数据导入oracle数据库
利用TOAD实现把EXCEL数据导入oracle数据库 工具: Toad11.7z(百度搜索,直接下载) 1.将Excel文件中某些字段导入到Oracle数据库的对应表 连接想要导入的数据库 ,然 ...
- scrolView
禁止UIScrollView垂直方向滚动,只允许水平方向滚动 scrollview.contentSize = CGSizeMake(长度, 0); 禁止UIScrollView水平方向滚动,只允许 ...
- SVN的使用(转发)
http://my.oschina.net/joanfen/blog/194491?fromerr=LM5QY3YF
- poj3620
#include<iostream>#include<string>#include<stack>#include<vector>#include< ...