ZOJ-3261-Connections in Galaxy War [逆向并查集]
2017-07-26 08:45
507 查看
题目传送门
题意:
每个星星都有自己的power,星星之间有隧道可以联通,星星只能向比自己power更多的星星求救,如果最大的power相同就向序号更小的星星求救。
有两种操作:
“query x” 询问x可以求救的星星序号
“destroy x y” 破坏x - y连接的通道
思路:
如果直接对并查集进行删除操作会很麻烦,所以就将所有操作全部存起来逆向操作,先将所有没有被破坏的通道合并,然后逆向询问,破坏即为建边合并。
题意:
每个星星都有自己的power,星星之间有隧道可以联通,星星只能向比自己power更多的星星求救,如果最大的power相同就向序号更小的星星求救。
有两种操作:
“query x” 询问x可以求救的星星序号
“destroy x y” 破坏x - y连接的通道
思路:
如果直接对并查集进行删除操作会很麻烦,所以就将所有操作全部存起来逆向操作,先将所有没有被破坏的通道合并,然后逆向询问,破坏即为建边合并。
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <set> using namespace std; int N, M, Q; int val[12000]; map<int, map<int,int> >mark; struct node1{ int x,y; }P[22000]; struct node2{ char ch[10]; int x,y; }K[50000+1000]; int fa[12000], ans[50000+1000]; void init() { for (int i = 0; i < 10000; i++) { fa[i]=i; } return ; } int find(int x) { while (x!=fa[x]) x = fa[x]; return x; } void unite(int x, int y) { int a = find(x); int b = find(y); if (a!=b) { if (val[a]==val[b]) { if (a<b) fa[b] = a; else fa[a] = b; } else { if (val[a]>val[b]) fa[b] = a; else fa[a] = b; } } } int main(void) { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); bool flag = false; while (~scanf("%d", &N)) { init(); for (int i = 0; i < N; i++) scanf("%d", &val[i]); scanf("%d", &M); for (int i = 0; i < M; i++) scanf("%d %d", &P[i].x, &P[i].y); scanf("%d", &Q); for (int i = 0; i < Q; i++) { scanf(" %s", K[i].ch); if (K[i].ch[0]=='q') scanf("%d", &K[i].x); else { scanf("%d %d", &K[i].x, &K[i].y); mark[K[i].x][K[i].y] = mark[K[i].y][K[i].x] = 1; } } for (int i = 0; i < M; i++) { if (mark[P[i].x][P[i].y]==1) continue; unite(P[i].x, P[i].y); } int cnt = 0; for (int i = Q-1; i>=0; i--) { if (K[i].ch[0]=='q') { int a = find(K[i].x); if (a==K[i].x) ans[cnt++] = -1; else { if (val[a]>val[K[i].x]) ans[cnt++]=a; else ans[cnt++]=-1; } } else { unite(K[i].x, K[i].y); } } if (flag) printf("\n"); else flag = true; for (int i = cnt-1; i>=0; i--) printf("%d\n", ans[i]); mark.clear(); } return 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 GalaxyWar(并查集:离线处理)
- 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 (逆序并查集 + map)
- 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 ,并查集删边