您的位置:首页 > 产品设计 > UI/UE

LA 5031 - Graph and Queries

2014-08-30 22:56 253 查看
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 (1

N

2 * 104,
0

M

6 * 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


大白书上的一道例题,转化为离线问题,先通过所有操作得到最终的图,然后倒序再做一遍操作,不过把删边操作改为加边(用并查集维护),修改值操作,前后反一反,求第k大值不仅仅是treap的专利了,所有的BST均可以实现,具体操作,如果左子树为k-1个元素,那么返回根;如果左子树元素个数lt小于k-1,那么递归调用kth(右子树,k-lt-1);如果左子树元素个数lt大于k-1,那么递归调用kth(左子树,k)。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;

struct treap{
treap* ch[2];
int r,v,s;

treap(int vx):v(vx){ch[0]=ch[1]=0;r=rand();s=1;}
int cmp(int x){
if(v==x) return -1;
return x<v?0:1;
}
int maintain(){
s=1;
if(ch[0]) s+=ch[0]->s;
if(ch[1]) s+=ch[1]->s;
}
};
void rotate(treap* &o,int d){
treap* k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
o->maintain();o=k;
}
void insert(treap* &o,int x){
if(!o) o=new treap(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(treap* &o,int x){
int d=o->cmp(x);
if(d==-1){
treap* u=o;
if(o->ch[0]&&o->ch[1]){
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]) o=o->ch[1];
else o=o->ch[0];
delete u;
}
}
else remove(o->ch[d],x);
if(o) o->maintain();
}
int kth(treap* o,int k){
if(!o||k<=0||k>o->s) return 0;
int s=o->ch[0]?o->ch[0]->s:0;
if(s+1==k) return o->v;
else if(k<=s) return kth(o->ch[0],k);
return kth(o->ch[1],k-s-1);
}
void removetree(treap* &o){
if(o->ch[0]) removetree(o->ch[0]);
if(o->ch[1]) removetree(o->ch[1]);
delete o;
o=0;
}
struct command{
char type;
int x,k;
}cd[500010];
treap* trp[20010];
int weight[20010],from[60010],to[60010],edge[60010];
int fa[20010];
int findset(int x){
return x==fa[x]?x:(fa[x]=findset(fa[x]));
}
void merge(treap* &src,treap* &dst){
if(src->ch[0]) merge(src->ch[0],dst);
if(src->ch[1]) merge(src->ch[1],dst);
insert(dst,src->v);
delete src;
src=0;
}
void add_edge(int s,int e){
s=findset(s),e=findset(e);
if(s!=e){
if(trp[s]->s<trp[e]->s) {fa[s]=e;merge(trp[s],trp[e]);}
else {fa[e]=s;merge(trp[e],trp[s]);}
}
}
int main()
{
int n,m,rk,x,v,cas=1;
char op;
memset(trp,0,sizeof trp);
while(scanf("%d%d",&n,&m),n||m){
for(int i=1;i<=n;i++) scanf("%d",weight+i);
for(int i=1;i<=m;i++) scanf("%d%d",from+i,to+i);
memset(edge,false,sizeof edge);
int tot=0;
while(scanf(" %c",&op),op!='E'){
scanf("%d",&x);
if(op=='D') {edge[x]=true;rk=0;}
else if(op=='Q') scanf("%d",&rk);
else{
scanf("%d",&v);
rk=weight[x];
weight[x]=v;
}
cd[tot++]=(command){op,x,rk};
}
for(int i=1;i<=n;i++){
fa[i]=i;
if(trp[i]) removetree(trp[i]);
trp[i]=new treap(weight[i]);
}
for(int i=1;i<=m;i++) if(!edge[i]) add_edge(from[i],to[i]);
int cnt=0;
long long res=0;
for(int i=tot-1;i>=0;i--){
if(cd[i].type=='D'){
int id=cd[i].x;
add_edge(from[id],to[id]);
}
else if(cd[i].type=='Q'){
int p=findset(cd[i].x);
res+=kth(trp[p],trp[p]->s+1-cd[i].k);
cnt++;
}
else{
int u=findset(cd[i].x);
remove(trp[u],weight[cd[i].x]);
insert(trp[u],cd[i].k);
weight[cd[i].x]=cd[i].k;
}
}
printf("Case %d: %.6f\n",cas++,1.0*res/cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: