POJ 2763 Housewife Wind 树链剖分
来源:互联网 发布:c语言标识符命名规则 编辑:程序博客网 时间:2024/05/21 06:59
题目大意:
就是给出一棵树然后p次操作, 每次操作询问从当前位置到某个位置的路径的权值和, 另外一个操作是修改某条边的权值
大致思路:
树链剖分第二题...
因为写线段树的在建树的时候没有pushUp调试了一个小时才发现....感觉自己智商好捉急.....
不过因为这个原因对于树链剖分的过程更熟悉了...
首先在处理边的时候, 我们先将边上的信息转移到点上, 这里我们将所有的边的信息转移到其深度更大的结点上
于是在处理两点之间的距离时, 沿着剖分之后的路径走的时候, 如果到了top[x] == top[y]的情况(top[x] 表示点x所在链的深度最小的点, 即链的顶点)
如果x == y那么就直接结束, 如果top[x] < top[y]则应该加上x到hson[y]之间的点的权值和, hson[y]为y的重儿子(top[x] == top[y] && x != y只会出现在重链上)
于是整个过程就很简单了, 整体复杂度O(n*logn*logn)
代码如下:
Result : Accepted Memory : 8912 KB Time : 1610 ms
/* * Author: Gatevin * Created Time: 2015/9/7 20:41:59 * File Name: Sakura_Chiyo.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 100010int top[maxn];int grandson[maxn];int dep[maxn];int siz[maxn];int belong[maxn];int father[maxn];int Q[maxn];int cnt;int hson[maxn];int n, q, s;bool vis[maxn];int id[maxn];int antiID[maxn];struct Edge{ int u, v, w, nex; Edge(int _u, int _v, int _w, int _nex) { u = _u, v = _v, w = _w, nex = _nex; } Edge(){}};int head[maxn];//用于邻接表int tot;//总边数Edge edge[maxn << 1];int w[maxn];//将其父边的权值信息降到这个结点处void add_Edge(int x, int y, int w){ edge[++tot] = Edge(x, y, w, head[x]); head[x] = tot; return;}void split(){ cnt = 0; int l = 0, r = 1; dep[Q[r] = 1] = 1; father[r] = -1; w[r] = 0; while(l < r) { int x = Q[++l]; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; w[y] = edge[j].w; dep[Q[++r] = y] = dep[x] + 1; father[y] = x; } } for(int i = n; i; i--) { int x = Q[i], p = -1; siz[x] = 1; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; siz[x] += siz[y]; if(p == -1 || (p > 0 && siz[y] > siz[p])) p = y; } if(p == -1) { hson[x] = -1; grandson[++cnt] = x; belong[top[cnt] = x] = cnt; } else { hson[x] = p; belong[x] = belong[p]; top[belong[x]] = x; } } int idx = 0; memset(vis, 0, sizeof(vis)); for(int i = n; i; i--)//完成树上的点到线段树控制的区间的映射 { int x = Q[i]; if(vis[x]) continue; vis[x] = 1; id[x] = ++idx;//实际上的结点x对应的区间位置 antiID[idx] = x; while(father[x] != -1 && belong[father[x]] == belong[x] && !vis[father[x]]) { x = father[x]; id[x] = ++idx; antiID[idx] = x; vis[x] = 1; } } return;}//线段树单点修改区间求和#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1int val[maxn << 2];void pushUp(int rt){ val[rt] = val[rt << 1] + val[rt << 1 | 1]; return;}void build(int l, int r, int rt){ if(l == r) { val[rt] = w[antiID[l]]; return; } int mid = (l + r) >> 1; build(lson); build(rson); pushUp(rt); return;}void update(int l, int r, int rt, int pos, int value){ if(l == r) { val[rt] = value; return; } int mid = (l + r) >> 1; if(pos <= mid) update(lson, pos, value); else update(rson, pos, value); pushUp(rt); return;}int query(int l, int r, int rt, int L, int R){ if(l >= L && r <= R) return val[rt]; int mid = (l + r) >> 1; int ret = 0; if(mid >= L) ret += query(lson, L, R); if(mid + 1 <= R) ret += query(rson, L, R); return ret;}int answer(int x, int y)//询问x到y的路径和{ int ans = 0; while(top[belong[x]] != top[belong[y]]) { if(dep[top[belong[x]]] < dep[top[belong[y]]]) swap(x, y); ans += query(1, n, 1, id[x], id[top[belong[x]]]); x = father[top[belong[x]]]; } if(x == y) return ans;//注意这里结点代表的是向上与父亲节点连边的权值, 当x == y的时候直接结束 //否则两者在同一条链上, 且一定是重链 if(dep[x] < dep[y]) swap(x, y);//x是在下面的那个 ans += query(1, n, 1, id[x], id[hson[y]]);//hson[y]是y的重儿子(考虑到点权代表的是与父亲连边的边权) return ans;}void change(int x, int w){ x <<= 1;//双向边 int u = edge[x].u, v = edge[x].v; if(father[u] == v) update(1, n, 1, id[u], w); else update(1, n, 1, id[v], w); return;}int main(){ while(scanf("%d %d %d", &n, &q, &s) != EOF) { memset(head, -1, sizeof(head)); tot = 0; int u, v, w; for(int i = 1; i < n; i++) { scanf("%d %d %d", &u, &v, &w); add_Edge(u, v, w); add_Edge(v, u, w); } split(); build(1, n, 1); int op, x; while(q--) { scanf("%d", &op); switch(op) { case 0: scanf("%d", &u); printf("%d\n", answer(s, u)); s = u; break; case 1: scanf("%d %d", &x, &w);//将第x条边的权值改为w change(x, w); break; } } } return 0;}/*11 10 11 2 12 6 22 7 37 8 47 11 51 3 93 5 53 9 79 10 101 4 80 80 110 50 50 100 30 61 3 1990 110 4*/
0 0
- poj 2763 Housewife Wind(树链剖分)
- POJ 2763 Housewife Wind 树链剖分
- POJ 2763 Housewife Wind 树链剖分
- poj 2763 Housewife Wind(树链剖分)
- poj 2763 Housewife Wind 树链剖分
- POJ 2763 Housewife Wind (树链剖分)
- POJ 2763 Housewife Wind 树链剖分
- POJ 2763 Housewife Wind(树链剖分)
- poj 2763 Housewife Wind 树链剖分
- POJ 2763Housewife Wind 树链剖分
- POJ 2763 Housewife Wind (树链剖分)
- POJ 2763 Housewife Wind
- POJ 2763 Housewife Wind
- poj 2763 Housewife Wind
- POJ 2763 Housewife Wind
- POJ 2763 Housewife Wind
- poj 2763 Housewife Wind
- POJ 2763 Housewife Wind
- redis实现有序的消息队列
- uva 10410 树重建
- 使用Swift去掉字符串中的空格
- gridcontrol无数据自定义显示及滚动至底部翻页
- 腾讯2015研发笔试大题
- POJ 2763 Housewife Wind 树链剖分
- 学习资源分享,想了解某个协议的基础知识可以去H3C的网站学习,挺好~~
- eclipse为Android应用程序APK生成的私钥文件在哪里?
- Freeland团队暑期实践有感
- mysql出现权限问题无法创建新数据库&启动mysql失败问题,问题解决
- idea启动android项目时找不到已经运行的genymotion虚拟设备解决方案
- 遍历Map的四种方法
- uva 548 tree
- 星期二男孩问题