“玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
来源:互联网 发布:js代码重复度检查 编辑:程序博客网 时间:2024/05/17 15:20
初始时有个首都1,有n个操作+V表示有一个新的城市连接到了V号城市-V表示V号城市断开了连接,同时V的子城市也会断开连接每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号最小的城市输入数据保证正确,每次添加与删除的城市一定是与首都相连的
每次都只需要知道最远且编号最小的城市,所以直接使用优先队列存储如果是+V就使用并查集(不能路径压缩)添加上然后加入优先队列,接着直接弹出首元素就是结果如果是-V则把V指向0,接着弹出优先队列的第一个元素如果他与1相连就是答案,否则将他到0这条路上的点都连上0删除他,继续弹出注意这儿有个trick就是如果没有城市与1相连,答案就是1然后为了避免这个trick,初始时刻就把1加到优先队列里就可以了。
////Created by just_sort 2016/12/30//Copyright (c) 2016 just_sort.All Rights Reserved//#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp>#include <ext/pb_ds/hash_policy.hpp>#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <sstream> //isstringstream#include <iostream>#include <algorithm>using namespace std;using namespace __gnu_pbds;typedef long long LL;typedef pair<int, LL> pp;#define REP1(i, a, b) for(int i = a; i < b; i++)#define REP2(i, a, b) for(int i = a; i <= b; i++)#define MP(x, y) make_pair(x,y)const int maxn = 100010;const int maxm = 2e5;const int maxs = 10;const int INF = 1e9;typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;//headstruct node{ int id, dis; node(){} node(int id, int dis) : id(id), dis(dis) {} bool operator<(const node &rhs) const{ if(dis == rhs.dis) return id > rhs.id; return dis < rhs.dis; }};priority_queue <node> ans;int fa[maxn], dis[maxn];void init(int n){ while(!ans.empty()) ans.pop(); ans.push(node{1, 0}); for(int i = 0; i <= n; i++) fa[i] = i, dis[i] = 0;}int Find(int x){ if(x == fa[x]) return x; return Find(fa[x]);}void add(int f, int s){ fa[s] = f; dis[s] = dis[f] + 1; ans.push(node(s, dis[s]));}void sub(int f){ fa[f] = 0; while(!ans.empty()){ node now = ans.top(); if(Find(now.id) == 1) return ; int s = now.id; while(fa[s] != 0){ fa[s] = 0; s = fa[s]; } ans.pop(); }}char cmd;int main(){ int tt, x, n, newnode; scanf("%d", &tt); while(tt--){ scanf("%d", &n); init(n + 1); newnode = 1; for(int i = 0; i < n; i++){ getchar(); scanf("%c%d", &cmd, &x); if(cmd == '+'){ add(x, ++newnode); } else{ sub(x); } node nn = ans.top(); printf("%d\n", nn.id); } } return 0;}
0 0
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
- 玲珑学院OJ 1072 Capture【并查集+优先队列】
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #4 B Best couple
- 二分+倍增思想 “玲珑杯”ACM比赛 Round #13/B
- “玲珑杯”ACM比赛 Round #19 B.Buildings【二分+RMQ】
- “玲珑杯”ACM比赛 Round #19 B(RMQ大法好啊,比线段树快得多!!!!)
- 玲珑杯-Round #7.B-1072-Capture
- “玲珑杯”ACM比赛 Round #4 E -- array(dp)
- “玲珑杯”ACM比赛 Round #5 C -- Coco(递归)
- “玲珑杯”ACM比赛 Round #18 A(穷举)
- “玲珑杯”ACM比赛 Round #18 C(dp)
- “玲珑杯”ACM比赛 Round #4
- 玲珑杯”ACM比赛 Round #5
- “玲珑杯”ACM比赛 Round #11 " ---1097
- “玲珑杯”ACM比赛 Round #11 D
- “玲珑杯”ACM比赛 Round #11
- 远控上线
- iOS适配问题---用2@图和3@图怎么是使用
- Lua中的I/O库
- Java分层概念
- 415. Add Strings
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
- android studio 如何打包jar(细节)
- 请问怎样在百度地图中实现根据地点名称得到该地点的经纬度
- 关于苹果内购的验证防止被刷单
- 2016秋招笔试面试题二:Android及网络协议部分
- i.MX283 Linux 2.6 内核编译
- 性能特性测试系列3——QT线程与std::thread(上)之std::thread
- 类型转换
- 1.Hibernate之入门