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 ...
随机推荐
- 洛谷比赛 Joe的数
/* 开始暴力+滚动数组70 后来发现不用循环很多 找p的倍数 找%p意义下为0的就好了 */ #include<iostream> #include<cstdio> #inc ...
- excel数据导入到sqlserver中---------工作笔记
调用页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...
- html.day02
1.链接标签 a <a href=”http://www.baidu.com”></a> <img src=”aaa”/> 一般情况下: 来源 用 src ...
- XML约束
XML约束--能够看懂约束内容,根据约束内容写出符合规则的xml文件. DTD约束 1)导入dtd方式 内部导入 <!DOCTYPE note [ <!ELEMENT note (to,f ...
- hibernate_validator_08
内置的约束条件 Hibernate Validator包含了一些基本的使用比较广的约束,下面是一些Hibernate Validator给出的最常用的约束.另外Hibernate Validator还 ...
- 模拟vector
实现了vector的模板,insert, erase, push_back, iterator #include<iostream> #include<string.h> #i ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- 你好,C++(33)对象生死两茫茫 6.2.3 一个对象的生与死:构造函数和析构函数
6.2.2 使用类创建对象 完成某个类的声明并且定义其成员函数之后,这个类就可以使用了.一个定义完成的类就相当于一种新的数据类型,我们可以用它来定义变量,也就是创建这个类所描述的对象,表示现实世界中 ...
- C++ Built-In Array 的语义
C++ 编译花了大量精力使得class和原始类(primitive types)的用法一致.比如array的应用: A a[100]:// A is class int b[100]: 虽然a是用户定 ...
- 【NEERC 2003】有向图破坏
[题目描述] Alice和Bob正在玩如下的游戏.首先Alice画一个有N个顶点,M条边的有向图.然后Bob试着摧毁它.在一次操作中他可以找到图中的一个点,并且删除它所有的入边或所有的出边. Alic ...