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 ...
随机推荐
- python-列表、字典、元组的员工信息处理接口(第二篇(五):基于列表、字典和元组的员工信息处理接口)
Python之旅]第二篇(五):基于列表.字典和元组的员工信息处理接口 python 列表 字典 元组 员工信息处理接口 摘要: 1.基本需求 编写一个查询员工信息表的程序,实现如下功能: ( ...
- javascript MD5加密
/* * Javascript MD5 library - version 0.4 * * Coded (2011) by Luigi Galli - LG@4e71.org - * http://f ...
- c# 学习笔记(二)
c#3.0 新特性 扩展方法 扩展方法允许编写和声明它的类之外的关联类的方法 用于没有源代码或者类是密封的,需要给类扩展新方法时 1.扩展方法必须被声明为static2.扩展方法声明所在的类必须被声 ...
- VI文件编辑操作说明
vi Hello.c (回车后就进入,按i或a键开始编辑.要退出按ESC,进入中间模式,按冒号 :后面跟命令):wq (保存并退出):q!(退出不保存)
- Swift - 07 - 布尔类型
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 最全java的读写操作(转载)
Java的I/O系统中的输入输出流为我们进行开发提供了很多便利,利用其强大的封装性,通过各种组合能够实现多种多样的功能.但是Java提供了很多输入输出流类,在概念和使用上有很多相似之处,所以给很多开发 ...
- javascript基础学习(十一)
javascript之BOM 学习要点: BOM介绍 Window对象 一.BOM介绍 浏览器对象模型简称为BOM(Brower Object Model),BOM由很多对象构成,对象与对象之间有着相 ...
- Mysql 数据类型使用说明
FLOAT 和DOUBLE 类型支持使用标准的浮点运算进行近似计算. DECIMAL类型用于存储精确的小数. 因为cpu不支持对DECIMAL的直接计算,所以在Mysql5.0及更高的版本中,MYSQ ...
- MySQL分库分表环境下全局ID生成方案
在大型互联网应用中,随着用户数的增加,为了提高应用的性能,我们经常需要对数据库进行分库分表操作.在单表时代,我们可以完全依赖于数据库的自增ID来唯一标识一个用户或数据对象.但是当我们对数据库进行了分库 ...
- SxsTrace工具使用方法(转)
http://blog.sina.com.cn/s/blog_494e45fe0102dtt3.html Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库 ...