“玲珑杯”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 ...
随机推荐
- SQL Server监控报警架构_如何添加报警
一.数据库邮件报警介绍 数据库邮件是从SQL Server数据库引擎发送电子邮件企业解决方案,使用简单传输协议(SMTP)发送邮件.发送邮件进程与数据库的进程隔离,因此可不用担心影响数据库服务器. 数 ...
- SVN发布网站
1.问题 2.分析 如果机器先安装.NET framework,后安装IIS就会出现此问题,原因是.NET framework未注册. 3.注册.NET framework
- OPP Services Log
SELECT FCPP.CONCURRENT_REQUEST_ID REQ_ID, FCP.NODE_NAME, FCP.LOGFILE_NAME FROM FND_CONC_PP_ACTIONS ...
- 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation
1. xcodeproj工程损坏时,.m文件没有加入编译. 2. SVN报错:clean the working copy and then retry the operation http://bl ...
- virut详细分析
Virut分析 0x00.综合描述 virut样本的执行过程大体可以分为六步:第一步,解密数据代码,并调用解密后的代码:第二步,通过互斥体判断系统环境,解密病毒代码并执行:第三步,创建内存映射文件,执 ...
- PHP Ueditor 富文本编辑器
2016年12月11日 08:46:59 星期日 百度的简版富文本编辑器umeditor很久没更新了 全功能版本的配置项跟umeditor还是有区别的, 这里说下ueditor怎么对接到项目中去, 主 ...
- js识别当前用户设备的几个方法
公司要做一个APP下载页面,里面需要判断是安卓还是苹果访问本页面,最开始想偷懒直接在给IOSAPP返回IOSAPP商店地址,然后Android直接进行访问.但想着毕竟做两个页面不利于后期维护和修改,打 ...
- checked 全选 反选 示例
不多说看例子: 右上侧全选,然后每个栏又有一个栏目全选. 反选解决办法: function selectSubscibe(_class) { $("." + _class + &q ...
- Reverse Core 第三部分 - 21章 - Windows消息钩取
@author: dlive @date: 2016/12/19 0x01 SetWindowsHookEx() HHOOK SetWindowsHookEx( int idHook, //hook ...
- codeforces 484D Kindergarten (dp、贪心)
题意:给n个数,分成若干个连续组,每组获益为max-min,输出最大获益. 参考:http://blog.csdn.net/keshuai19940722/article/details/408735 ...