题意:初始时有个首都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(并查集+优先队列)的更多相关文章

  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. Spring 的动态数据源实现

    1. 配置多个数据源 这里以两个c3p0数据库连接池的数据源作为实例.在Spring框架下使用c3p0的数据库需要加入c3p0-0.9.1.2.jar(现在最新的)这个支持包.这里以数据同步项目为例: ...

  2. 日期格式转换 java 2016-09-03T00:00:00.000+08:00

    /**  * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  (yyyy-MM-dd'T'HH:mm:ss.SSSZ) TO  yyyy-MM-dd HH:mm:ss  * @ ...

  3. python基础知识(四)

    摘要:主要涉及lambda表达式.python内置函数(open文件重点).冒泡排序 一.lambda表达式 适用于创建简单函数,也叫匿名函数, 函数名 = lambda 参数 : 返回值 funct ...

  4. ur c题练习

    ur的c果然sxbk啊 ur5:“三个莫比乌斯反演掷地有声"——摘自v(c)f(z)k(y)语录,无删改 ur2:有根树分治裸题,复杂度玄学$O(n\sqrt{n})$. 首先,转化为统计k ...

  5. [ActiveMQ]初识ActiveMQ

    初识ActiveMQ ActiveMQ介绍 官方网站:http://activemq.apache.org/ 最新版本:ActiveMQ 5.14.1(2016-10-28) 最新版本下载链接:htt ...

  6. 学习 opencv---(4) 分离颜色通道 && 多通道混合

    上篇文章中我们讲到了使用addWeighted函数进行图像混合操作,以及将ROI和addWeighted函数结合起来使用,对指定区域进行图像混合操作. 而为了更好地观察一些图像材料的特征,有时需要对R ...

  7. docker版wordpress

    拉取wordpress镜像 docker pull wordpress:latest 创建mysql 容器docker run --name wordpress-mysql -e MYSQL_ROOT ...

  8. JavaFx导出文件

    导出文件格式可选 protected void handExportDateAction(ActionEvent event) { // ShowDialog.showConfirmDialog(FX ...

  9. js:使用js过程中遇到的一个小问题

    在一个作业中使用了js,函数A调用函数B.当A和B中均含有变量i的时候,相关操作结果可能会出错. 将B中的i替换为j(j不存在于A中)后,结果正确. 目前考虑原因是两个变量i有相关性(或者说实际上就是 ...

  10. javaEE基础08

    javaEE基础08 一.继承 特点:继承父类的属性和方法,单继承(多继承) 特性:方法的复写(重写) 比如:人可以养狗 人------>狗:整体和部分(拥有)关系 关键字:extends 结构 ...