题目链接: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. Office 2016 word无法粘贴(Ctrl + V)

    最近下载了一个 Office 2016 专业版 使用,发现 word 无法使用 Ctrl + V 粘贴东西,由于经常需要复制粘贴东西,无法粘贴影响很大 查了很多资料,尝试过很多的方法,终于发现问题的所 ...

  2. 性能测试工具LoadRunner06-LR之Virtual User Generator 事务(Transaction)

    定义 为了衡量某个操作的性能,需要在操作的开始和结束位置插入这样一个范围,这就定义了一个transaction. 原因 从性能测试的角度出发,我们需要知道不同的操作所花费的时间,这样就能衡量不同的操作 ...

  3. Sqlite操作的一些关键类的官方说明与Intent的startactivityforresult方法

    Intent: 该功能可以用于通过intent来跳转界面时候传递信号给原理的页面,以便做出一些处理: sqlite的使用: 该方法得到的sqlitedatabase可读可写,而getreadabled ...

  4. ubuntu下搭建android开发环境核心篇安装AndroidStudio、sdk、jdk

    本文系转载http://blog.csdn.net/lsyz0021/article/details/52215996 一.安装前的准备 1.1.如果你还没有安装ubuntu 14.04 LTS系统, ...

  5. LeetCode 860.柠檬水找零(C++)

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

  6. 用一层for循环初始化三维数组

    ][][]; ; i < * * ; i++) { a[i / ][(i / ) % ][i % ] = i; printf(, (i / ) % , i % ); // printf(&quo ...

  7. IPM的修炼之路

    总结了一下最近一年半来看到的产品经理方面的素养资料. 产品经理: 必备素质:市场洞察,抽象概括,创新想象,心思细腻,热爱产品,具备一定的企业家精神等. 是通才:市场,项目,设计,管理,用户,统计,心理 ...

  8. jquery中load()加载页面,刷新之后,加载的页面不显示的解决办法

    <script language="javascript" type="text/javascript"> $(function(){ $(&quo ...

  9. React.js 小书 Lesson6 - 使用 JSX 描述 UI 信息

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson6 转载请注明出处,保留原文链接和作者信息. 这一节我们通过一个简单的例子讲解 React.j ...

  10. C++/CLI中的const literal initonly 友元(转)

    C++/CLI中的const literal initonly 友元 C++/CLI中的const Visual C++并不允许在const对象上调用成员函数,除非该成员函数也被声明为const. C ...