You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You're also given a sequence of operations and you need to process them as requested. Here's a list of the possible operations that you might encounter:
- Deletes an edge from the graph.
The format is [D X], where X is an integer from 1 to M, indicating the ID of the edge that you should delete. It is guaranteed that no edge will be deleted more than once.
- Queries the weight of the vertex with K-th maximum value among all vertexes currently connected with vertex X (including X itself).
The format is [Q X K], where X is an integer from 1 to N, indicating the id of the vertex, and you may assume that K will always fit into a 32-bit signed integer. In case K is illegal, the value for that query will be considered as undefined, and you should return 0 as the answer to that query.
- Changes the weight of a vertex.
The format is [C X V], where X is an integer from 1 to N, and V is an integer within the range [-106, 106].
The operations end with one single character, E, which indicates that the current case has ended. For simplicity, you only need to output one real number - the average answer of all queries.
Input
There are multiple test cases in the input file. Each case starts with two integers N and M (1N2 * 104, 0M6 * 104), the number of vertexes in the graph. The next N lines describes the initial weight of each vertex (- 106[weight][i]106). The next part of each test case describes the edges in the graph at the beginning. Vertexes are numbered from 1 to N. The last part of each test case describes the operations to be performed on the graph. It is guaranteed that the number of query operations [Q X K] in each case will be in the range [1, 2 * 105], and there will be no more than 2 * 105 operations that change the values of the vertexes [C X V].
There will be a blank line between two successive cases. A case with N = 0, M = 0 indicates the end of the input file and this case should not be processed by your program.
Output
For each test case, output one real number - the average answer of all queries, in the format as indicated in the sample output. Please note that the result is rounded to six decimal places.
Explanation for samples:
For the first sample:
D 3 - deletes the 3rd edge in the graph (the remaining edges are (1, 2) and (2, 3))
Q 1 2 - finds the vertex with the second largest value among all vertexes connected with 1. The answer is 20.
Q 2 1 - finds the vertex with the largest value among all vertexes connected with 2. The answer is 30.
D 2 - deletes the 2nd edge in the graph (the only edge left after this operation is (1, 2))
Q 3 2 - finds the vertex with the second largest value among all vertexes connected with 3. The answer is 0 (Undefined).
C 1 50 - changes the value of vertex 1 to 50.
Q 1 1 - finds the vertex with the largest value among all vertex connected with 1. The answer is 50.
E - This is the end of the current test case. Four queries have been evaluated, and the answer to this case is (20 + 30 + 0 + 50) / 4 = 25.000.
For the second sample, caution about the vertex with same weight:
Q 1 1 - the answer is 20
Q 1 2 - the answer is 20
Q 1 3 - the answer is 10
Sample Input
3 3 10 20 30 1 2 2 3 1 3 D 3 Q 1 2 Q 2 1 D 2 Q 3 2 C 1 50 Q 1 1 E 3 3 10 20 20 1 2 2 3 1 3 Q 1 1 Q 1 2 Q 1 3 E 0 0
Sample Output
Case 1: 25.000000 Case 2: 16.666667
N个节点M条边的无向图,每个节点都有一个整数权值,有三种操作。
D X 删除ID为X的边。Q X K,计算与节点X连通的节点中(包括本身)第K大的值。如果不存在为0。C X V,把结点X的权值改为V。输出所有Q指令计算结果的平均值。
Treap名次树就是排序二叉树多了个rank,这棵树的rank值符合堆的性质,父亲的rank大。这是为了避免二叉树插入顺序的极端情况,每新建结点的时候随机一个rank值,和把所有结点随机排序再插入等价。
插入的时候先按一般排序二叉树插入,再判断根结点和插入这个结点的子树的根结点的rank,如果不符合堆的性质,就旋转。旋转分为左旋和右旋,左旋是把根变成右结点的左结点,原来右结点的左结点变成原来根的右结点,原来根的右结点变成根。右旋是把根变成左结点的右结点,原来左结点的右结点变成原来根的左结点,原来根的左结点变成根。
离线处理,把所有操作反过来。最开始把最后剩下的边全加到图里。遇到D操作就相当于把这条边加到图里,遇到C操作就是把V改成X。这样倒着来是因为删边不好删,加边只需要找出两个端点所在的集合合并就行了。注意把结点数少的合并到结点数多的集合中,对于任意结点来说,每次移动到新的树汇中时,该结点所在的树的大小至少加倍,所以任意结点至多被移动logN次,每次移动需要O(logN)的时间,因此总时间复杂度为O(nlog^2n)。
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXNODE 8*MAXN
#define eps 1e-10
using namespace std;
typedef long long LL;
const int MAXN=20010;
const int MAXM=60010;
const int MAXC=500010;
int N,M,K,query_cnt;
LL query_tot;
int pa[MAXN],weight[MAXN],removed[MAXM];
char str[10];
struct Node{
Node* ch[2]; //左右子树
int r; //优先级
int v; //值
int s; //结点总数
Node(int v):v(v){
ch[0]=ch[1]=NULL;
r=rand(); //随机一个r值
s=1;
}
bool operator < (const Node& rhs) const{
return r<rhs.r;
}
int cmp(int x) const{
if(x==v) return -1;
return x<v?0:1;
}
void maintain(){
s=1;
if(ch[0]!=NULL) s+=ch[0]->s;
if(ch[1]!=NULL) s+=ch[1]->s;
}
}* root[MAXN];
struct Edge{
int from,to;
}edge[MAXM];
struct Command{
char op;
int x,k;
}command[MAXC];
//d=0左旋,d=1右旋
void rotate(Node* &o,int d){
Node* k=o->ch[d^1];
o->ch[d^1]=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?0:1);
insert(o->ch[d],x);
if(o->ch[d]->r>o->r) rotate(o,d^1);
}
o->maintain();
}
void remove(Node* &o,int x){
int d=o->cmp(x);
if(d==-1){
Node* u=o;
if(o->ch[0]!=NULL&&o->ch[1]!=NULL){
int d2=(o->ch[0]->r>o->ch[1]->r?1:0);
rotate(o,d2);
remove(o->ch[d2],x);
}
else{
if(o->ch[0]==NULL) o=o->ch[1];
else o=o->ch[0];
delete u;
}
}
else remove(o->ch[d],x);
if(o!=NULL) o->maintain();
}
int findset(int x){
return x==pa[x]?x:pa[x]=findset(pa[x]);
}
//把src的结点移动到dest
void mergeto(Node* &src,Node* &dest){
if(src->ch[0]!=NULL) mergeto(src->ch[0],dest);
if(src->ch[1]!=NULL) mergeto(src->ch[1],dest);
insert(dest,src->v);
delete src;
src=NULL;
}
int kth(Node* o,int k){
if(o==NULL||k<=0||k>o->s) return 0;
int s=(o->ch[1]==NULL?0:o->ch[1]->s);
if(k==s+1) return o->v;
else if(k<=s) return kth(o->ch[1],k);
else return kth(o->ch[0],k-s-1);
}
void add_edge(int x){
int u=findset(edge[x].from),v=findset(edge[x].to);
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]);
}
}
}
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;
}
void removetree(Node* x){
if(x->ch[0]!=NULL) removetree(x->ch[0]);
if(x->ch[1]!=NULL) removetree(x->ch[1]);
delete x;
x=NULL;
}
int main(){
freopen("in.txt","r",stdin);
int cas=0;
while(scanf("%d%d",&N,&M)!=EOF&&(N||M)){
for(int i=1;i<=N;i++) scanf("%d",&weight[i]);
for(int i=1;i<=M;i++) scanf("%d%d",&edge[i].from,&edge[i].to);
memset(removed,0,sizeof(removed));
K=0;
int x,k,v;
while(scanf("%s",str),str[0]!='E'){
k=v=0;
scanf("%d",&x);
if(str[0]=='D') removed[x]=1;
else if(str[0]=='Q') scanf("%d",&k);
//k->v
else if(str[0]=='C'){
scanf("%d",&v);
k=weight[x];
weight[x]=v;
}
command[K++]=(Command){str[0],x,k};
}
for(int i=1;i<=N;i++){
pa[i]=i;
if(root[i]!=NULL) removetree(root[i]);
root[i]=new Node(weight[i]);
}
for(int i=1;i<=M;i++) if(!removed[i]){
add_edge(i);
}
query_tot=query_cnt=0;
for(int i=K-1;i>=0;i--){
if(command[i].op=='D') add_edge(command[i].x);
else if(command[i].op=='Q') query(command[i].x,command[i].k);
else if(command[i].op=='C') change_weight(command[i].x,command[i].k);
}
printf("Case %d: %.6lf\n",++cas,query_tot/(double)query_cnt);
}
return 0;
}