“玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作
+V表示有一个新的城市连接到了V号城市
-V表示V号城市断开了连接,同时V的子城市也会断开连接
每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号最小的城市
输入数据保证正确,每次添加与删除的城市一定是与首都相连的
题解:每次都只需要知道最远且编号最小的城市,所以直接使用优先队列存储
如果是+V就使用并查集(不能路径压缩)添加上然后加入优先队列,接着直接弹出首元素就是结果
如果是-V则把V指向0,接着弹出优先队列的第一个元素
如果他与1相连就是答案,否则将他到0这条路上的点都连上0删除他,继续弹出
注意这儿有个trick就是如果没有城市与1相连,答案就是1
- #include<set>
- #include<map>
- #include<queue>
- #include<stack>
- #include<cmath>
- #include<vector>
- #include<string>
- #include<cstdio>
- #include<cstring>
- #include<iomanip>
- #include<stdlib.h>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- #define eps 1E-8
- /*注意可能会有输出-0.000*/
- #define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
- #define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
- #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
- #define mul(a,b) (a<<b)
- #define dir(a,b) (a>>b)
- typedef long long ll;
- typedef unsigned long long ull;
- const int Inf=<<;
- const ll INF=1ll<<;
- const double Pi=acos(-1.0);
- const int Mod=1e9+;
- const int Max=2e5+;
- struct node
- {
- int key,value;//存编号与离1的距离
- bool operator<(const node &c)const//距离大的编号小的优先
- {
- if(value==c.value)
- return key>c.key;
- return value<c.value;
- }
- };
- priority_queue<struct node> ans;
- int fat[Max],val[Max];
- char str[Max];
- void Init(int n)
- {
- while(!ans.empty())
- ans.pop();
- node temp;//初始化一个元素1在队列底部
- temp.value=;
- temp.key=;
- ans.push(temp);
- for(int i=;i<=n;++i)
- {
- fat[i]=i;
- val[i]=;
- }
- return;
- }
- int Find(int x)
- {
- if(x==fat[x])
- return fat[x];
- return Find(fat[x]);
- }
- void Solveadd(int f,int s)//+
- {
- node temp;
- fat[s]=f;
- val[s]=val[f]+;
- temp.key=s;
- temp.value=val[s];
- ans.push(temp);
- return;
- }
- void Solvesub(int f)//-
- {
- node temp;
- int s;
- fat[f]=;
- while(!ans.empty())
- {
- temp=ans.top();
- if(Find(temp.key)==)
- return;
- s=temp.key;
- while(fat[s]!=)//链上赋为0
- {
- fat[s]=;
- }
- ans.pop();
- }
- return;
- }
- int main()
- {
- int t,n,m,coun;
- scanf("%d",&t);
- node temp;
- while(t--)
- {
- scanf("%d",&n);
- Init(n+);
- coun=;
- for(int i=;i<n;++i)
- {
- getchar();
- scanf("%c%d",&str[i],&m);
- if(str[i]=='+')
- {
- Solveadd(m,++coun);
- }
- else
- {
- Solvesub(m);
- }
- temp=ans.top();
- printf("%d\n",temp.key);
- }
- }
- return ;
- }
“玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)的更多相关文章
- “玲珑杯”ACM比赛 Round #12题解&源码
我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,日后补上剩余题的题解&源码吧! A ...
- “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】
A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...
- “玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)
“玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-0 ...
- “玲珑杯”ACM比赛 Round #1
Start Time:2016-08-20 13:00:00 End Time:2016-08-20 18:00:00 Refresh Time:2017-11-12 19:51:52 Public ...
- “玲珑杯”ACM比赛 Round #18
“玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...
- “玲珑杯”ACM比赛 Round #1 题解
A:DESCRIPTION Eric has an array of integers a1,a2,...,ana1,a2,...,an. Every time, he can choose a co ...
- 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想
http://www.ifrog.cc/acm/problem/1054 问删除一个字符后的最小循环节是多少. 比赛的时候想不出,不知道怎么暴力. 赛后看了别人代码才晓得.唉,还以为自己字符串还不错, ...
- “玲珑杯”ACM比赛 Round #18--最后你还是AK了(搜索+思维)
题目链接 DESCRIPTION INPUT OUTPUT SAMPLE INPUT 1 4 2 1 2 5 2 3 5 3 4 5 5 5 SAMPLE OUTPUT 35 HINT 对于样例, ...
- “玲珑杯”ACM比赛 Round #22 E 贪心,脑洞
1171 - 这个E大概是垃圾桶捡来的 Time Limit:2s Memory Limit:128MByte Submissions:138Solved:45 DESCRIPTION B君在做 CO ...
随机推荐
- Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问。
这几天在写一个导出word的功能,使用 Microsoft.Vbe.Interop.dll和Office.dll 在本地都可以正常运行,但是上传到服务器后就报错,如下图: 对于此问题,也在网上查了一些 ...
- 【译】使用 CocoaPods 模块化iOS应用
原文翻译自:Using CocoaPods to Modularize a Big iOS App 为你的移动应用选择正确的架构是一件相当大的事情,这会对你的工作流程造成影响,陷入面对的问题,可能是一 ...
- PL/SQL存储过程编程
PL/SQL存储过程编程 /**author huangchaobiao *Email:huangchaobiao111@163.com */ PL/SQL存储过程编程(上) 1. Oracle应用编 ...
- c语言第一章第一节 认识变量
声明:本人大一新生,闲着无聊..写写c语言教程..菜鸟一枚..大神勿喷!!! 接下来我们都用dev来进行编译..vc++太古老了,没提示功能,不好上手,并且老是出毛病..vs太大了,编个c不至于,运行 ...
- R语言 批量规划求解
昨天读到一个项目,是关于优化求解的. 约束条件如下: 公司里有很多客户,客户之所以不继续用我们的产品了,是因为他账户余额是负的,所以,为了重新赢回这些客户,公司决定发放优惠券cover掉客户账户的负余 ...
- Altium Designer 的entry sheet ,offsheet和port作用(转载)
1.图纸结构 图纸包括两种结构关系: 一种是层次式图纸,该连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系: 另一种是扁平式图纸,该连接关系是横向的,任何两张图纸之间都可以建立信号连 ...
- Orchard 模块开发学习笔记 (1)
创建模块 首先,打开Bin目录下的Orchard.exe 等到出现orchard>后, 看看命令列表中是否存在 codegen module 如果不存在,则需要先执行:feature enabl ...
- 项目vue2.0仿外卖APP(六)
goods 商品列表页开发 布局编写 除了商品之外还有购物车,还有个详情页,挺复杂的. 两栏布局:左侧固定宽度,右侧自适应,还是用flex. 因为内容可能会超过手机高度,超过就隐藏.左右两侧的内容是可 ...
- Volley框架使用笔记
1.初始化请求队列 RequestQueue RequestQueue queue= Volley.newRequestQueue(context); 2.StringRequest 网络请求 Get ...
- smali调试总结
一. 开始调试 smali调试从最早的重打包用各种JAVA IDE进行调试, 到后来的可以不用重打包用xposed插件, 在到最后的修改系统源码刷机或者修改boot.img刷机一劳永逸 apk可调试可 ...