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

Uva 5031 Graph and Queries(Treap)

2017-01-25 00:16 423 查看
题意:给一个n个点m条边的无向图,有三种操作:删除一条边;查询与节点X联通的弟k大的权值;将节点X的权值改为V;求最终查询的权值和的平均值。

思路:参照lrj书上的,将命令倒过来解题,建立多颗Treap,合并的时候采用启发式合并,时间复杂度就能降低。

#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
const int maxnode = 5 * 1e5 + 10;
const int maxq = 1e6 + 10;
const int maxn = 2 * 1e4 + 10;
const int maxm = 6 * 1e4 + 10;
using namespace std;

struct treap {
int root[maxn], treapcnt, key[maxnode], pr[maxnode];
int ch[maxnode][2], cnt[maxnode], sz[maxnode];
void init () {
memset(root, 0, sizeof root);
memset(sz, 0, sizeof sz);
memset(cnt, 0, sizeof cnt);
treapcnt = 1;
sz[0] = 0;
}
///更新孩子的大小
void update(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x]; }
///旋转
void _rotate(int &x, int t) {
int y = ch[x][t];
ch[x][t] = ch[y][t ^ 1];
ch[y][t ^ 1] = x;
update(x); update(y);
x = y;
}
///插入操作
void __insert(int &x, int k) {
if(x) {
if(key[x] == k) cnt[x]++;
else {
int t = key[x] < k;
__insert(ch[x][t], k);
if(pr[ch[x][t]] < pr[x]) _rotate(x, t);
}
} else { ///申请新节点
x = treapcnt++;
key[x] = k;
cnt[x] = 1;
pr[x] = rand();
ch[x][0] = ch[x][1] = 0;
}
update(x);
}
///删除操作
void __erase(int &x, int k) {
if(!x) return ;
if(key[x] == k) {
if(cnt[x] > 1) cnt[x]--;
else {
int ls = ch[x][0], rs = ch[x][1];
if(!ls && !rs) { x = 0; return ; } ///叶节点直接删除
int t = pr[ls] > pr[rs];
_rotate(x, t ^ 1); __erase(x, k);
}
} else __erase(ch[x][key[x] < k], k);
update(x);
}
///获得排名
int __getKth(int &x, int k) {
if(x == 0) return 0;
int siz = sz[ch[x][0]];
if(k <= siz) return __getKth(ch[x][0], k);
k -= cnt[x] + siz;
if(k <= 0) return key[x];
return __getKth(ch[x][1], k);
}
} tp;

struct CMD {
char op;
int x, k, la;
void input() {
if(op == 'D') scanf("%d", &x);
else scanf("%d %d", &x, &k);
}
} cmd[maxq];
int n, m, w[maxn], mov[maxm], kase = 1;
int from[maxm], to[maxm], p[maxn];
char s[10];

int findset(int x) { return x == p[x] ? x : p[x] = findset(p[x]); }

void merg(int &x, int y) {
while(tp.cnt[y]--) tp.__insert(x, tp.key[y]);
int ls = tp.ch[y][0], rs = tp.ch[y][1];
if(ls) merg(x, ls);
if(rs) merg(x, rs);
}

int main() {
while(scanf("%d %d", &n, &m) && n) {
tp.init();
memset(mov, 0, sizeof mov);
for(int i = 1; i <= n; i++) p[i] = i;
for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
for(int i = 1; i <= m; i++) scanf("%d %d", &from[i], &to[i]);
int q = 0;
while(scanf("%s", s) && s[0] != 'E') {
cmd[q].op = s[0];
cmd[q].input();
if(s[0] == 'D') mov[cmd[q].x] = 1;
if(s[0] == 'C') {
cmd[q].la = w[cmd[q].x];
w[cmd[q].x] = cmd[q].k;
}
q++;
}
for(int i = 1; i <= m; i++) {
if(mov[i]) continue;
int nx = findset(from[i]);
int ny = findset(to[i]);
p[nx] = ny;
}

for(int i = 1; i <= n; i++) {
int rt = findset(i);
tp.__insert(tp.root[rt], w[i]);
}

double sum = 0;
int tal = 0;
for(int i = q - 1; i >= 0; i--) {
char ch = cmd[i].op;
if(ch == 'D') {
int xx = cmd[i].x;
int f = from[xx], t = to[xx];
int nf = findset(f), nt = findset(t);
if(nf == nt) continue;
if(tp.sz[tp.root[nf]] > tp.sz[tp.root[nt]]) {
p[nt] = nf; merg(tp.root[nf], tp.root[nt]);
} else { p[nf] = nt; merg(tp.root[nt], tp.root[nf]); }
} else if(ch == 'C') {
int xx = cmd[i].x;
int kk = cmd[i].la;
int pre = findset(xx);
tp.__erase(tp.root[pre], w[xx]);
tp.__insert(tp.root[pre], kk);
w[xx] = kk;
} else {
int nx = findset(cmd[i].x);
int t = tp.sz[tp.root[nx]];
tal++;
if(cmd[i].k > t || cmd[i].k < 1) continue;
sum += tp.__getKth(tp.root[nx], t - cmd[i].k + 1);
}
}
printf("Case %d: %f\n", kase++, sum / tal);
aa97

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: