题目链接: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 图询问的更多相关文章

  1. LA - 5031 - Graph and Queries

    题意:一个N个点(编号从1开始),M条边的无向图(编号从1开始),有3种操作: D X:把编号为X的边删了: Q X K:查询编号为X的结点所在连通分量第K大的元素: C X V:将编号为X的结点的权 ...

  2. LA 5031 Graph and Queries —— Treap名次树

    离线做法,逆序执行操作,那么原本的删除边的操作变为加入边的操作,用名次树维护每一个连通分量的名次,加边操作即是连通分量合并操作,每次将结点数小的子树向结点数大的子树合并,那么单次合并复杂度O(n1lo ...

  3. 使用vsphere client 克隆虚拟机

    免费的VMWare ESXi5.0非常强大,于是在vSphere5.0平台中ESXi取代了ESX.,使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,都是windows2003操作系统,难道必须 ...

  4. [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295 分析:很巧妙的一道spfa从搜索的角度是搜索在所有1中搜索删除哪T个1,对整个图询问,这 ...

  5. poj1236 强连通

    题意:有 n 个学校每个学校可以将自己的软件共享给其他一些学校,首先,询问至少将软件派发给多少学校能够使软件传播到所有学校,其次,询问添加多少学校共享关系可以使所有学校的软件能够相互传达. 首先,第一 ...

  6. 虚拟化之vmware-vsphere (web) client

    两种客户端 vsphere client 配置>软件>高级设置里的变量 uservars.supressshellwarning=1 vsphere web client 安装完vSphe ...

  7. 如何使用vsphere client 克隆虚拟机

    vSphere 是VMware公司推出一套服务器虚拟化解决方案. 工具/原料 vSphere 测试系统 方法/步骤 1.进入vSphere client,关闭需要克隆的虚拟机win7 2.选中ESXi ...

  8. 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic

    本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...

  9. vSphere Client克隆虚拟机

    免费的VMWare ESXi5.5非常强大,使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,系统一个一个安装很麻烦.VMware ESXi.VMware vCenter Server 和 vS ...

随机推荐

  1. 6 GPath

    1       GPath GPath是Groovy的表达式语言,类似xml的XPath.而二者的不同在于,GPath表达式可以应用于处理POJOs或者处理xml. 例如:a.b.c语句等同于a.ge ...

  2. 练习六十八:pyecharts模块练习

    用一个地图显示最热的几个城市温度 代码(用Geo模块) from pyecharts import Geo attr = ["西安","上海","苏州 ...

  3. asp.net core WebAPI学习以及 发布(***入门学习)

    A asp.net Core 系列[一]——创建Web应用 asp.net Core 系列[二]—— 使用 ASP.NET Core 和 VS2017 for Windows 创建 Web API a ...

  4. 08-----pymysql模块使用

    pymysql的下载和使用 exctue() 之sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetchall   一.pytmysql的下载和使用  ...

  5. docker 部署公司阿里云服务器 (一)

    持续更新... 背景环境: 阿里云ecs服务器 centos7.4          公网地址:xx.xx.xx.xx      内网地址:172.16.77.4 阿里云RDS 阿里云 Redis 第 ...

  6. 转 sql profile 绑定 litera and move profile to another db l for spa

    SQL TYPE 1:for bind value sql , first create a good plan with literal and with good  profile. then u ...

  7. SQL智能提示插件——SQLPrompt

    1.安装SQLPrompt,直接点击下一步即可,然后打开SQL Server 在菜单栏找到SQLPrompt选项注册该软件 2.先断开网络,然后运行注册机,将注册吗复制到序列号的地方,将两个勾选的复选 ...

  8. 正则表达式获取多个img src的值

    /** * 得到网页中图片的地址 */public static Set<String> getImgStr(String htmlStr) { Set<String> pic ...

  9. iOS-swift-枚举和结构体

    1.枚举 使用关键字 enum 创 建枚举. 枚举默认起始值为 0,可以自定义起始值. 在枚举中可以定义方法,和类中定义的一样. 使用关键字 rawValue 访问当前枚举的值. enum Rank: ...

  10. Hibernate系列4-----之删除

    1.和它的增改查兄弟不同,多了个until包定义了HibernateUntil类,让我们来一起看看吧 public class HibernateUntil { private static Conf ...