ZOJ-3261(Connections in Galaxy War)——并查集
来源:互联网 发布:电脑好多软件打不开 编辑:程序博客网 时间:2024/06/06 00:47
题目大意:有n个星球,每个星球有一定的power值,某些星球是直接或间接相连的。。。当某个星球想求助时会找到相连的里面的power值最大而且大于自己的一个星球。。。先在给定这些power值并给定两两相连的信息,然后又q个操作,destroy a b是删除a b直接相连的边(保证存在),query a求向谁求助,如果不能求助输出-1 。。。
解题思路:说实在,我是真的没想到这题会用并查集。(看完题解之后)这道题的应该逆向思考,有点离线的味道。这里求的是相连的点中权值最大的,具有集合代表性,即一个集合中的点他们所求的数据是相同的(当然除那个代表点之外)。但是这里有删边的操作,这是并查集无法实现的,所以我们需要逆向考虑。
把一开始给你的图称为原图,一步一步删边最后得到的图称为现图。我们先满足现图的条件,然后按题目操作的逆序反向进行query(查)与destroy(反向destroy就是union,即合并集合),这样就可以避开删边了。
答题思路就是这样,代码细节就不说了。不过,我还要介绍一下本题最大最大最大的坑点。
那就是查询点的代表点的时候,我以为Star A的代表点只要不为本身,那么就应该输出它的代表点。其实不是这样的,因为有在相同最大权值点中寻找编号最小的这一操作,这样会导致有些点的代表点权值与它的权值相同,但题目中要求寻求帮助点的全职必须严格大于Star A,所以这样的点是要输出-1的。
遇到这样厉害的题目,我也只能骂自己蠢了。
代码:
#include <set>#include <map>#include <list>#include <queue>#include <stack>#include <cmath>#include <string>#include <cstdio>#include <vector>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define PB push_back#define FOR(i,n,m) for(int i=n;i<=m;i++)#define ROF(i,n,m) for(int i=n;i>=m;i--)#define clr(i,j) memset(i,j,sizeof(i))#define maxn 10005typedef long long ll;typedef pair<int,int> pii;int pre[maxn];int pos[maxn];int que[50005][3];int anw[50005];int mapp[20005][2];map<int,int>ma;int n;int find(int x){ return pre[x]==x?x:pre[x]=find(pre[x]);}void bing(int a,int b){ int x=find(a); int y=find(b); if(x==y)return; if(pos[x]>pos[y])pre[y]=x; else if(pos[x]<pos[y])pre[x]=y; else if(x<y)pre[y]=x; else pre[x]=y;}int main(){ bool flag=false; while(~scanf("%d",&n)) { if(flag)printf("\n"); flag=true; ma.clear(); for(int i=0; i<n; i++) { scanf("%d",&pos[i]); pre[i]=i; } int m,a,b,q; scanf("%d",&m); for(int i=0; i<m; i++) scanf("%d %d",&mapp[i][0],&mapp[i][1]); scanf("%d",&q); char as[10]; for(int i=0; i<q; i++) { scanf("%s %d",as,&a); if(as[0]=='q') { que[i][0]=0; que[i][1]=a; } else { scanf("%d",&b); if(a>b)swap(a,b); ma[a*10000+b]=1; que[i][0]=1; que[i][1]=a; que[i][2]=b; } } for(int i=0; i<m; i++) { int c=mapp[i][0],d=mapp[i][1]; if(c>d)swap(c,d); if(!ma[c*10000+d])bing(c,d); } for(int i=q-1; i>=0; i--) { if(que[i][0])bing(que[i][1],que[i][2]); else { int w=find(que[i][1]); if(pos[que[i][1]]==pos[w])anw[i]=-1; else anw[i]=w; } } for(int i=0; i<q; i++) if(!que[i][0]) printf("%d\n",anw[i]); } return 0;}
复制去Google翻译翻译结果
0 0
- ZOJ 3261 Connections in Galaxy War(并查集)
- ZOJ-3261(Connections in Galaxy War)——并查集
- zoj 3261 Connections in Galaxy War(逆向并查集)
- ZOJ 3261 Connections in Galaxy War【并查集】
- ZOJ 3261 Connections in Galaxy War (并查集)
- zoj 3261 - Connections in Galaxy War(并查集)
- ZOJ 3261 Connections in Galaxy War 并查集
- Zoj 3261 Connections in Galaxy War【逆序并查集】
- ZOJ-3261-Connections in Galaxy War [逆向并查集]
- zoj Connections in Galaxy War(逆向并查集)
- ZOJ Problem Set - 3261 Connections in Galaxy War(并查集)
- Zoj 3261 Connections in Galaxy War (逆向并查集)
- zoj 3261 Connections in Galaxy War(并查集+离线逆向操作)
- ZOJ 3261 Connections in Galaxy War (离线处理+逆向并查集)
- Zoj 3563 Connections in Galaxy War 逆向并查集
- [并查集+路径压缩]zoj 3261:Connections in Galaxy War
- zoj 3261 Connections in Galaxy War 删边并查集模板
- ZOJ 3261Connections in Galaxy War 反向用并查集
- Object-Oriented Terms 面向对象术语
- SpringMVC form标签
- iOS层级结构
- 黑马程序员---OC基础---类和对象
- iOS蓝牙
- ZOJ-3261(Connections in Galaxy War)——并查集
- 数列求和
- Android控件系列之RadioButton&RadioGroup
- Matlab优化问题06—quadprog
- 设计模式学习--单例模式
- 全民Scheme(3):为什么就不讲道理呢,女人?
- css学习
- java中比较两个日期的先后
- Spring 源码导入到 Eclipse