题目链接: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 ;
}

最新文章

  1. 安装centos6.5如何选择安装包
  2. JS 中没有按地址(引用)传递,只有按值传递
  3. JSF 与 HTML 标签的联系
  4. CentOS下 pycharm开发环境搭建
  5. PAT 1009. 说反话 (20) JAVA
  6. hadoop2.0初识1.3
  7. c.BIO连接器与NIO连接器的对比
  8. SpringMVC 拦截器
  9. HDU 4548
  10. ERP系统实施与企业内部控制管理实践
  11. IconRes提供免费高质量的Material风格android官方图标库
  12. MySQL 优化方案
  13. c# 面相对象4-多态性
  14. C++使用对象指针
  15. 谨慎能捕千秋蝉(三)——界面操作劫持与HTML5安全
  16. 固定GridView标题栏,冻结列功能实现
  17. Docker(九):Docker容器卷插件
  18. C++的入口函数
  19. supervisor使用,配置和安装(包括监控守护进程httpd,keepalived)
  20. 【Qt编程】Qt学习笔记&lt;二&gt;

热门文章

  1. 10.Web组件复用
  2. Java类名 方法名 常量 变量的命名习惯
  3. php连接数据库mysql数据库
  4. 3-----Scrapy框架的命令行详解
  5. 将个人博客与github关联
  6. 时间比较早晚java
  7. The Falling Leaves UVA - 699
  8. 性能测试工具LoadRunner13-LR之Virtual User Generator 创建java脚本以及小结
  9. Huawei English Corner
  10. (转)ssh-keygen 中文手册