LA 5031 图询问
题目链接:https://vjudge.net/contest/159527#problem/A
题意:(求一个 图 中的连通分量中的 第 k 大)
一张图,n 个点,m 条边,
有一些操作:
删除 ID 为 x 的边,(从 1 到 m);
询问 x 所在的连通分量 里面第 k 大的权值;
把结点 X 的权值 改成 V;
求:
所有的询问后,计算平均值;
每个连通分量都是一颗Treap树,加边操作,就是树的合并;
刘汝佳采用的是离线算法,我还是第一次听说,但是还是可以按照题意直接模拟的(我猜,但是很麻烦);因为在Treap中删边不同于删点;
什么是离线算法呢?
把操作顺序反过来处理,执行完所有 删除边操作,然后建Treap,要是不在同一个连通分量里面(并查集判断),这就涉及到递归合并Treap树了,这里采用了启发式合并;
然后反向操作,遇到 D,就是加边(加边操作同上),
询问,就是在 X 所在连通分量里面,寻找第 k 大;
改权,就是删除这个点,然后从新加点;
#include <bits/stdc++.h> using namespace std; struct Node
{
Node *ch[];
int r; //优先级
int v; //值
int s; //结点总数 Node(int v):v(v)
{
ch[] = ch[] = NULL;
r = rand();
s = ;
} bool operator < (const Node& rhs) const
{
return r < rhs.r;
} int cmp(int x) const
{
if(x==v) return -;
return x < v ? : ;
} void maintain()
{
s = ;
if(ch[]!=NULL) s+=ch[]->s;
if(ch[]!=NULL) s+=ch[]->s;
} }; void rotate(Node* &o,int d)
{
Node* k = o->ch[d^];
o->ch[d^] = k ->ch[d];
k->ch[d] = o;
o->maintain();
k->maintain();
o = k;
} void insert(Node* &o,int x)
{
if(o==NULL) o = new Node(x);
else
{
int d = (x < o->v? : );
insert(o->ch[d],x);
if(o->ch[d]->r > o->r)
rotate(o,d^);
}
o->maintain();
} void remove(Node* &o,int x)
{
int d = o->cmp(x);
if(d==-)
{
Node* u = ;
if(o->ch[]!=NULL&&o->ch[]!=NULL)
{
int d2 = (o->ch[]->r > o->ch[]->r ? : );
rotate(o,d2);
remove(o->ch[d2],x);
}
else
{
if(o->ch[]==NULL)
o = o->ch[];
else o = o->ch[];
}
}
else
remove(o->ch[d],x); if(o!=NULL) o->maintain();
} const int maxc = + ;
struct Command
{
char type;
int x,p;
} commands[maxc]; const int maxn = + ;
const int maxm = + ;
int n,m;
int weight[maxn],from[maxm],to[maxm],removed[maxm]; int pa[maxn];
int findset(int x)
{
return pa[x]!=x ? pa[x] = findset(pa[x]):x;
} Node* root[maxn]; //Treap int kth(Node* o,int k)
{
if(o==NULL||k<=||k> o->s) return ;
int s = (o->ch[]==NULL?:o->ch[]->s);
if(k==s+) return o->v;
else if(k<=s) return kth(o->ch[],k);
else return kth(o->ch[],k-s-);
} void mergeto(Node* &src,Node* &dest)
{
if(src->ch[]!=NULL) mergeto(src->ch[],dest);
if(src->ch[]!=NULL) mergeto(src->ch[],dest);
insert(dest,src->v);
delete src;
src = NULL;
} void removetree(Node* &x)
{
if(x->ch[]!=NULL) removetree(x->ch[]);
if(x->ch[]!=NULL) removetree(x->ch[]);
delete x;
x = NULL;
} void add_edge(int x)
{
int u = findset(from[x]),v=findset(to[x]);
if(u!=v)
{
if(root[u]->s < root[v]->s)
{
pa[u] = v;
mergeto(root[u],root[v]);
}
else
{
pa[v] = u;
mergeto(root[v],root[u]);
}
}
} int query_cnt;
long long query_tot;
void query(int x,int k)
{
query_cnt++;
query_tot +=kth(root[findset(x)],k);
} void change_weight(int x,int v)
{
int u = findset(x);
remove(root[u],weight[x]);
insert(root[u],v);
weight[x] = v;
} int main()
{
int kase = ;
while(scanf("%d%d",&n,&m)==&&n)
{
for(int i=; i<=n; i++)
scanf("%d",&weight[i]);
for(int i=; i<=m; i++)
scanf("%d%d",&from[i],&to[i]);
memset(removed,,sizeof(removed)); int c = ;
for(;;)
{
char type;
int x,p=,v = ;
scanf(" %c",&type);
if(type=='E') break;
scanf("%d",&x);
if(type=='D') removed[x]= ; //删掉的边
if(type=='Q') scanf("%d",&p);
if(type=='C')
{
scanf("%d",&v);
p = weight[x];
weight[x] = v;
}
commands[c++] = (Command)
{
type,x,p
};
} //最终的图
for(int i=; i<=n; i++)
{
pa[i] = i;
if(root[i]!=NULL) removetree(root[i]);
root[i] = new Node(weight[i]);
}
for(int i=; i<=m; i++)
{
if(!removed[i]) //id为i这条边没有被删掉
add_edge(i);
} query_cnt = query_tot = ;
for(int i=c-; i>=; i--)
{
if(commands[i].type=='D') add_edge(commands[i].x); //加上边
if(commands[i].type=='Q') query(commands[i].x,commands[i].p);//第p大
if(commands[i].type=='C') change_weight(commands[i].x,commands[i].p);
} printf("Case %d: %.6lf\n",++kase,query_tot/(double)query_cnt); } return ;
}
LA 5031 图询问的更多相关文章
- LA - 5031 - Graph and Queries
题意:一个N个点(编号从1开始),M条边的无向图(编号从1开始),有3种操作: D X:把编号为X的边删了: Q X K:查询编号为X的结点所在连通分量第K大的元素: C X V:将编号为X的结点的权 ...
- LA 5031 Graph and Queries —— Treap名次树
离线做法,逆序执行操作,那么原本的删除边的操作变为加入边的操作,用名次树维护每一个连通分量的名次,加边操作即是连通分量合并操作,每次将结点数小的子树向结点数大的子树合并,那么单次合并复杂度O(n1lo ...
- 使用vsphere client 克隆虚拟机
免费的VMWare ESXi5.0非常强大,于是在vSphere5.0平台中ESXi取代了ESX.,使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,都是windows2003操作系统,难道必须 ...
- [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295 分析:很巧妙的一道spfa从搜索的角度是搜索在所有1中搜索删除哪T个1,对整个图询问,这 ...
- poj1236 强连通
题意:有 n 个学校每个学校可以将自己的软件共享给其他一些学校,首先,询问至少将软件派发给多少学校能够使软件传播到所有学校,其次,询问添加多少学校共享关系可以使所有学校的软件能够相互传达. 首先,第一 ...
- 虚拟化之vmware-vsphere (web) client
两种客户端 vsphere client 配置>软件>高级设置里的变量 uservars.supressshellwarning=1 vsphere web client 安装完vSphe ...
- 如何使用vsphere client 克隆虚拟机
vSphere 是VMware公司推出一套服务器虚拟化解决方案. 工具/原料 vSphere 测试系统 方法/步骤 1.进入vSphere client,关闭需要克隆的虚拟机win7 2.选中ESXi ...
- 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic
本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...
- vSphere Client克隆虚拟机
免费的VMWare ESXi5.5非常强大,使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,系统一个一个安装很麻烦.VMware ESXi.VMware vCenter Server 和 vS ...
随机推荐
- C# 事务提交(非数据库)
.Net 2.0开始支持 static void Main(string[] args) { using (TransactionScope ts = new TransactionScope()) ...
- classloader 学习
classloader就是把类文件加载到jvm中供虚拟机使用,先看一个magic小例子: 首先,我定义一个alex/vicky包,然后在这个包内定义一个接口: public interfaceISer ...
- 锁、CAS操作和无锁队列的实现
https://blog.csdn.net/yishizuofei/article/details/78353722 锁的机制 锁和人很像,有的人乐观,总会想到好的一方面,所以只要越努力,就会越幸运: ...
- 谷歌Chrome浏览器离线安装包
下载地址(自选版本) 链接: https://pan.baidu.com/s/1_gVP32tBNTR0pHhQbbM8Iw 密码: rmak 有能力的可以自行到下方地址下载: 最新稳定版:https ...
- Python查看类或Module的版本号
>>> import keras >>> print keras.__version__ 1.2.0
- Deep Learning 和 Knowledge Graph howto
领军大家: Geoffrey E. Hinton http://www.cs.toronto.edu/~hinton/ 阅读列表: reading lists and survey papers fo ...
- js 页面按钮提交后 创建显示loading div 操作完成后 再隐藏或删除 进度div
预期效果: 1.点击Save按钮,创建及显示loading div框 2.Save操作完成后,再删除loading 及弹出提示结果 <html> <head> </hea ...
- 安卓压力测试之monkey
步骤: 1.把要测试的apk包放在 SDK-platfrom-tools下 2.配置adb.exe的环境变量 3.手机连接上电脑(虚拟机和真机只能连接一个) 4.运行:adb devices 查看 ...
- TouchSlide 触屏滑动特效插件
TouchSlide 是纯javascript打造的触屏滑动特效插件,面向手机.平板电脑等移动终端,能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. 插件开源.体积小.简单实用.功能强大,是 ...
- GCC操作
GCC编译器常用选项 生成动态链接库: gcc file.c -fPIC -o file.so, PIC表示Position-Independent Code: 独立地址代码 编译: gcc -c f ...