题意:初始时有个首都1,有n个操作

+V表示有一个新的城市连接到了V号城市

-V表示V号城市断开了连接,同时V的子城市也会断开连接

每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号最小的城市

输入数据保证正确,每次添加与删除的城市一定是与首都相连的

题解:每次都只需要知道最远且编号最小的城市,所以直接使用优先队列存储

如果是+V就使用并查集(不能路径压缩)添加上然后加入优先队列,接着直接弹出首元素就是结果

如果是-V则把V指向0,接着弹出优先队列的第一个元素

如果他与1相连就是答案,否则将他到0这条路上的点都连上0删除他,继续弹出

注意这儿有个trick就是如果没有城市与1相连,答案就是1

  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<vector>
  7. #include<string>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iomanip>
  11. #include<stdlib.h>
  12. #include<iostream>
  13. #include<algorithm>
  14. using namespace std;
  15. #define eps 1E-8
  16. /*注意可能会有输出-0.000*/
  17. #define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
  18. #define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
  19. #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
  20. #define mul(a,b) (a<<b)
  21. #define dir(a,b) (a>>b)
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. const int Inf=<<;
  25. const ll INF=1ll<<;
  26. const double Pi=acos(-1.0);
  27. const int Mod=1e9+;
  28. const int Max=2e5+;
  29. struct node
  30. {
  31. int key,value;//存编号与离1的距离
  32. bool operator<(const node &c)const//距离大的编号小的优先
  33. {
  34. if(value==c.value)
  35. return key>c.key;
  36. return value<c.value;
  37. }
  38. };
  39. priority_queue<struct node> ans;
  40. int fat[Max],val[Max];
  41. char str[Max];
  42. void Init(int n)
  43. {
  44. while(!ans.empty())
  45. ans.pop();
  46. node temp;//初始化一个元素1在队列底部
  47. temp.value=;
  48. temp.key=;
  49. ans.push(temp);
  50. for(int i=;i<=n;++i)
  51. {
  52. fat[i]=i;
  53. val[i]=;
  54. }
  55. return;
  56. }
  57. int Find(int x)
  58. {
  59. if(x==fat[x])
  60. return fat[x];
  61. return Find(fat[x]);
  62. }
  63. void Solveadd(int f,int s)//+
  64. {
  65. node temp;
  66.  
  67. fat[s]=f;
  68. val[s]=val[f]+;
  69.  
  70. temp.key=s;
  71. temp.value=val[s];
  72. ans.push(temp);
  73.  
  74. return;
  75. }
  76. void Solvesub(int f)//-
  77. {
  78. node temp;
  79. int s;
  80. fat[f]=;
  81. while(!ans.empty())
  82. {
  83. temp=ans.top();
  84. if(Find(temp.key)==)
  85. return;
  86.  
  87. s=temp.key;
  88. while(fat[s]!=)//链上赋为0
  89. {
  90. fat[s]=;
  91. }
  92. ans.pop();
  93. }
  94. return;
  95. }
  96. int main()
  97. {
  98. int t,n,m,coun;
  99. scanf("%d",&t);
  100. node temp;
  101. while(t--)
  102. {
  103. scanf("%d",&n);
  104. Init(n+);
  105. coun=;
  106. for(int i=;i<n;++i)
  107. {
  108. getchar();
  109. scanf("%c%d",&str[i],&m);
  110. if(str[i]=='+')
  111. {
  112. Solveadd(m,++coun);
  113. }
  114. else
  115. {
  116. Solvesub(m);
  117. }
  118. temp=ans.top();
  119. printf("%d\n",temp.key);
  120. }
  121. }
  122. return ;
  123. }

“玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)的更多相关文章

  1. “玲珑杯”ACM比赛 Round #12题解&源码

    我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,日后补上剩余题的题解&源码吧!                                     A ...

  2. “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】

    A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...

  3. “玲珑杯”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 ...

  4. “玲珑杯”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 ...

  5. “玲珑杯”ACM比赛 Round #18

    “玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...

  6. “玲珑杯”ACM比赛 Round #1 题解

    A:DESCRIPTION Eric has an array of integers a1,a2,...,ana1,a2,...,an. Every time, he can choose a co ...

  7. 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想

    http://www.ifrog.cc/acm/problem/1054 问删除一个字符后的最小循环节是多少. 比赛的时候想不出,不知道怎么暴力. 赛后看了别人代码才晓得.唉,还以为自己字符串还不错, ...

  8. “玲珑杯”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 对于样例, ...

  9. “玲珑杯”ACM比赛 Round #22 E 贪心,脑洞

    1171 - 这个E大概是垃圾桶捡来的 Time Limit:2s Memory Limit:128MByte Submissions:138Solved:45 DESCRIPTION B君在做 CO ...

随机推荐

  1. 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 在本地都可以正常运行,但是上传到服务器后就报错,如下图: 对于此问题,也在网上查了一些 ...

  2. 【译】使用 CocoaPods 模块化iOS应用

    原文翻译自:Using CocoaPods to Modularize a Big iOS App 为你的移动应用选择正确的架构是一件相当大的事情,这会对你的工作流程造成影响,陷入面对的问题,可能是一 ...

  3. PL/SQL存储过程编程

    PL/SQL存储过程编程 /**author huangchaobiao *Email:huangchaobiao111@163.com */ PL/SQL存储过程编程(上) 1. Oracle应用编 ...

  4. c语言第一章第一节 认识变量

    声明:本人大一新生,闲着无聊..写写c语言教程..菜鸟一枚..大神勿喷!!! 接下来我们都用dev来进行编译..vc++太古老了,没提示功能,不好上手,并且老是出毛病..vs太大了,编个c不至于,运行 ...

  5. R语言 批量规划求解

    昨天读到一个项目,是关于优化求解的. 约束条件如下: 公司里有很多客户,客户之所以不继续用我们的产品了,是因为他账户余额是负的,所以,为了重新赢回这些客户,公司决定发放优惠券cover掉客户账户的负余 ...

  6. Altium Designer 的entry sheet ,offsheet和port作用(转载)

    1.图纸结构 图纸包括两种结构关系: 一种是层次式图纸,该连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系: 另一种是扁平式图纸,该连接关系是横向的,任何两张图纸之间都可以建立信号连 ...

  7. Orchard 模块开发学习笔记 (1)

    创建模块 首先,打开Bin目录下的Orchard.exe 等到出现orchard>后, 看看命令列表中是否存在 codegen module 如果不存在,则需要先执行:feature enabl ...

  8. 项目vue2.0仿外卖APP(六)

    goods 商品列表页开发 布局编写 除了商品之外还有购物车,还有个详情页,挺复杂的. 两栏布局:左侧固定宽度,右侧自适应,还是用flex. 因为内容可能会超过手机高度,超过就隐藏.左右两侧的内容是可 ...

  9. Volley框架使用笔记

    1.初始化请求队列 RequestQueue RequestQueue queue= Volley.newRequestQueue(context); 2.StringRequest 网络请求 Get ...

  10. smali调试总结

    一. 开始调试 smali调试从最早的重打包用各种JAVA IDE进行调试, 到后来的可以不用重打包用xposed插件, 在到最后的修改系统源码刷机或者修改boot.img刷机一劳永逸 apk可调试可 ...