“玲珑杯”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 ...
随机推荐
- linux 远程连接工具——MTPuTTY
简介 MTPUTTY是一个非常流行的ssh客户端. 安装 官网地址:http://www.ttyplus.com/multi-tabbed-putty/ 下载并安装,运行软件,如图 添加服务器 结语 ...
- mySql 远程连接(is not allowed to connect to this MySQL server)
如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL serve ...
- KALI Linux problems & Study Red Hat | Ubuntu
Problem When you ask some website with https head.you may met the problem secure connection failed ...
- 通过transform修改位置 大小 旋转 形变
--通过transform修改位置(平移)- CGAffineTransformMakeTranslation(**X偏移量**, **Y偏移量**);- CGAffineTransformTrans ...
- 第3月第23天 EAIntroView
1. EAIntroView,界面和模型分离.EAIntroPage数据模型,EAIntroView界面. https://github.com/ealeksandrov/EAIntroView
- Tomcat 内存优化设置
vi /tomcat7.0/bin/catalina.sh 开发环境 #!/bin/sh JAVA_OPTS='-Xms128m -Xmx512m -XX:PermSize=128m' 服务器: #! ...
- xml编辑器
cstring转cha型方法在mfc中用过可行 int CstringToch(CString str, char *ch) { assert(ch); memset(ch, 0, sizeof(ch ...
- 原生JS实现购物车结算功能代码+zepto版
html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Visual Studio 2015中创建C#的Android项目提示"Value cannot be null"的解决方法
由于之前本机已安装过Android SDK,在安装Visual Studio 2015时跳过了,并没有为Xamarin指定对应路径导致.Visual Studio顶部菜单:Tools > Opt ...
- Web项目使用Oracle.DataAccess.dll 类库连接oracle数据库
首先我用的工具是oracle 32位免安装版+Oracle.DataAccess.dll 32位 文件版本4.121.1.0+vs2013 +win7 64位 Oracle.DataAccess.d ...