题目链接如下:
大意是说,给一个图,有三种操作,删边,改顶点权值,询问一个点所在的连通图里边的第k大的顶点权值是多少。
这题的思路是倒着来的,即从终态开始,一条边一条边地合并,没错就是用并查集合并捏,合并两个相连接的顶点。权值的改变用一个链式前向星来做就行了。
在最开始的一遍合并过程中,我们顺势建立了一个二叉树,这个二叉搜索树的中序遍历是大到小排列的,很好理解,就是为了方便找第k大的数。合并的过程把小树合并给大树。
修改一个顶点的权值,这里把对应节点的左右子树全部转到对应节点上边,然后修改该点父节点的指向。不过应该有更好的方法,就是直接选择邻接点作为根节点的替代。修改完之后需要更新子树大小,然后splay一下,更新路径上所有节点的子树大小。
这里root的作用记录的是并查集中的根的根,这个需要理解一下,这也是为什么在splay和insert还有rotate的时候我们要把并查集的根传进去,因为我们要维护这个根真正指向的平衡二叉树的根。
代码如下所示:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<queue>
#include<string>
using namespace std;
#define N 20010
int T=0,n,m;
int cnt,q,n1,h[N],now[N],ch[N][2],fa[N],size[N],v[N],f[N],root[N],data[N];
double ans;
bool flag[3*N];
char str[5];
struct node1{
int x;
int next;
}mp[11*N];
struct node2{
int x,y;
}a[3*N];
struct node3{
int x,y;
char ch;
}query1[20*N];
int find(int x){
return x==f[x] ? x : f[x]=find(f[x]);
}
void update(int x){
size[x]=1+size[ch[x][0]]+size[ch[x][1]];
}
// 将x向上旋转一次
// 如果旋转到根rt 则将root[rt]更新
void rotate(int &x,int rt){
int y=fa[x];
int z=fa[y];
int d=(ch[y][0]==x);// 旋转方向
if (z) ch[z][ch[z][1]==y]=x;
else root[rt]=x;
fa[y]=x;
fa[x]=z;
ch[y][d^1]=ch[x][d];
fa[ch[x][d]]=y;
ch[x][d]=y;
update(y);
update(x);
}
void splay(int x,int &rt,int rt1){
while (x!=rt){
int y=fa[x];
if (y==rt){
rotate(x,rt1);
return;
}else if (ch[y][0]==x ^ ch[fa[y]][0]==y){//不共线
rotate(x,rt1);
}else{// 共线
rotate(y,rt1);
rotate(x,rt1);
}
}
}
// 插入节点 旋转节点到根
void insert(int &p,int x,int father,int rt){
if (!p){
p=x;
size[p]=1;
ch[p][0]=ch[p][1]=0;
fa[p]=father;
splay(p,root[rt],rt);
return;
}
if (v[x]>=v[p]) insert(ch[p][0],x,p,rt);
else insert(ch[p][1],x,p,rt);
}
void merge(int xx,int yy){
int x= find(xx),y= find(yy);
if (x==y) return;
// 将y的树层序遍历插入x的树中
if (size[root[x]]<size[root[y]]) swap(x,y);
int op=0,cl=1;
data[1]=root[y];
while (op<cl){
int p=data[++op];
if (ch[p][0]) data[++cl]=ch[p][0];
if (ch[p][1]) data[++cl]=ch[p][1];
insert(root[x],p,0,x);
}
f[y]=x;
}
void change(int x,int y){
int xx=find(x);
if (size[root[xx]]==1){
v[x]=y;
return;
}
while (ch[x][0]*ch[x][1]) splay(ch[x][0],x,xx);
while (ch[x][0]) splay(ch[x][0],x,xx);
while (ch[x][1]) splay(ch[x][1],x,xx);
ch[fa[x]][ch[fa[x]][1]==x]=0;
size[fa[x]]--;
splay(fa[x],root[xx],xx);
v[x]=y;
insert(root[xx],x,0,xx);
}
int query(int p,int x){
if (x<=0 || size[p]<x) return 0;
if (x<=size[ch[p][0]]) return query(ch[p][0],x);
if (x==size[ch[p][0]]+1) return v[p];
return query(ch[p][1], x-size[ch[p][0]]-1);
}
int main()
{
while (scanf("%d %d",&n,&m)){
if (!n && !m) break;
T++;
memset(h,0,sizeof(h));
cnt=n;q=0;ans=0;n1=0;
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d",&x);
mp[i].x=x;
mp[i].next=h[i];
h[i]=i;
now[i]=i;
}
memset(flag,true, sizeof(flag));
for (int i = 1; i <= m; ++i) {
scanf("%d %d",&a[i].x,&a[i].y);
}
while (true){
scanf("%s",str);
if (str[0]=='E') break;
query1[++q].ch=str[0];
if (str[0]=='Q'){
scanf("%d %d",&query1[q].x, &query1[q].y);
}else if (str[0]=='D'){
scanf("%d",&query1[q].x);
flag[query1[q].x]=false;
}else{// C
int x,y;
scanf("%d %d",&x,&y);
query1[q].x=x;
query1[q].y=y;
mp[++cnt].x=y;
mp[cnt].next=h[x];
h[x]=cnt;
now[x]=cnt;
}
}
for (int i = 1; i <= n; ++i) {
f[i]=root[i]=i;
size[i]=1;
v[i]=mp[now[i]].x;
ch[i][0]=ch[i][1]=fa[i]=0;
}
for (int i = 1; i <= m; ++i) {
if (flag[i]) merge(a[i].x,a[i].y);
}
for (int i = q; i > 0; --i) {
int x=query1[i].x,y=query1[i].y;
if (query1[i].ch=='D') merge(a[x].x,a[x].y);
else if (query1[i].ch=='C'){
now[x]=mp[now[x]].next;
change(x,mp[now[x]].x);
}else{
n1++;
ans+=(double) query(root[find(x)],y);
// cout<<query(root[find(x)],y)<<endl;
}
}
// cout<<n1<<endl;
if (n1) printf("Case %d: %.6lf\n",T,ans/n1);
else printf("Case %d: 0\n",T);
}
return 0;
}
原文如下: