SPOJ375--Query on a tree(树链剖分)
来源:互联网 发布:99scsc最新域名 编辑:程序博客网 时间:2024/05/16 15:08
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.
We will ask you to perfrom some instructions of the following form:
- CHANGE i ti : change the cost of the i-th edge to ti
or - QUERY a b : ask for the maximum edge cost on the path from node a to node b
Input
The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.
For each test case:
- In the first line there is an integer N (N <= 10000),
- In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
- The next lines contain instructions "CHANGE i ti" or "QUERY a b",
- The end of each test case is signified by the string "DONE".
There is one blank line between successive tests.
Output
For each "QUERY" operation, write one integer representing its result.
Example
Input:131 2 12 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:13
树链剖分入门题了。不懂可以先看这篇博文:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define maxn 100080#define maxm 200160#define lson id<<1,l,mid#define rson id<<1|1,mid+1,rint first[maxn],e;int vv[maxm],nxt[maxm],ww[maxm];int fa[maxn];//父亲节点int top[maxn];//top[v]表示v所在的重链的顶端节点int dep[maxn];//深度int size[maxn];//size[v]表示以v为根的子树的节点数int p[maxn];//p[v]表示以v与其父亲节点的连边在线段树中的位置int fp[maxn];//和数组相反int son[maxn];//重儿子int pos;//dfs1函数确定fa,dep,size,sonvoid init(){pos = 1;e = 0;memset(first,-1,sizeof(first));memset(son,-1,sizeof(son));}void addedge(int u,int v,int w){vv[e] = v;ww[e] = w;nxt[e] = first[u];first[u] = e++;vv[e] = u;ww[e] = w;nxt[e] = first[v];first[v] = e++;}void dfs1(int u,int pre,int d){fa[u] = pre;dep[u] = d;size[u] = 1;for(int i = first[u];i != -1;i = nxt[i]){int v = vv[i];if(v == pre)continue;dfs1(v,u,d+1);size[u] += size[v];if(son[u] == -1 || size[v] > size[son[u]])son[u] = v;}}//dfs2确定top,pvoid dfs2(int u,int t){top[u] = t;if(son[u] != -1){p[u] = pos++;//p[u]表示连向u的这条边在线段树中的位置fp[p[u]] = u;dfs2(son[u],t);}else {p[u] = pos++;fp[p[u]] = u;return;}for(int i = first[u];i != -1;i = nxt[i]){int v = vv[i];if(v == son[u] || v == fa[u])continue;dfs2(v,v);}}struct ST{int l,r,Max;}st[maxn<<2];void build(int id,int l,int r){st[id].l = l;st[id].r = r;st[id].Max = 0;if(l == r)return;int mid = (l+r) >> 1;build(lson);build(rson);}void PushUp(int id){st[id].Max = max(st[id<<1].Max,st[id<<1|1].Max);}void Update(int id,int pos,int k){if(st[id].l == pos && st[id].r == pos){st[id].Max = k;return;}if(st[id<<1].r >= pos)Update(id<<1,pos,k);else if(st[id<<1|1].l <= pos)Update(id<<1|1,pos,k);PushUp(id);}int query(int id,int l,int r){if(st[id].l == l && st[id].r == r)return st[id].Max;int mid = (l+r) >> 1;if(st[id<<1].r >= r)return query(id<<1,l,r);else if(st[id<<1|1].l <= l)return query(id<<1|1,l,r);return max(query(id<<1,l,st[id<<1].r),query(id<<1|1,st[id<<1|1].l,r));}int find(int u,int v)//找u和v路径上的最大边{int f1 = top[u],f2 = top[v];int ans = 0;while(f1 != f2){if(dep[f1] < dep[f2]){swap(f1,f2);swap(u,v);}ans = max(ans,query(1,p[f1],p[u]));u = fa[f1];f1 = top[u];}if(u == v)return ans;if(dep[u] > dep[v])swap(u,v);return max(ans,query(1,p[son[u]],p[v]));}int main(){int T;int n;scanf("%d",&T);while(T--){init();scanf("%d",&n);for(int i = 0;i < n-1;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);addedge(u,v,w);}dfs1(1,0,0);dfs2(1,1);build(1,1,n);char ope[10];for(int i = 0;i < n;i++){for(int j = first[i];j != -1;j = nxt[j]){int v = vv[j];if(dep[i] < dep[v]){Update(1,p[v],ww[j]);}}}while(scanf("%s",ope)!=EOF && ope[0] != 'D'){int u,v;scanf("%d%d",&u,&v);if(ope[0] == 'Q'){printf("%d\n",find(u,v));}else {int cnt1 = 2*u-1;int cnt2 = 2*u-2;int cnt3 = dep[vv[cnt1]] > dep[vv[cnt2]]?vv[cnt1]:vv[cnt2];Update(1,p[cnt3],v);}}}return 0;}
0 0
- SPOJ375--Query on a tree(树链剖分)
- spoj375 Query on a tree(树链剖分)
- spoj375 Query on a tree 树链剖分
- spoj375 Query on a tree 树链剖分
- 【树链剖分】spoj375 Query on a tree
- kyeremal-spoj375-Query on a tree-树链剖分
- 【SPOJ375】Query on a tree-树链剖分
- [SPOJ375]Query on a tree
- SPOJ375 Query on a tree
- SPOJ375 Query on a tree
- spoj375 Query on a tree
- 树链剖分+线段树 spoj375 Query on a tree
- 【SPOJ375】 Query on a tree——树链剖分
- Spoj375 Query on a tree 裸·树链剖分
- SPOJ375 QTREE - Query on a tree
- Spoj375.Query on a tree——树链剖分+线段树(基于边权)
- SPOJ375——Query on a tree(树链剖分模板详解以及入门)
- 【spoj375】Query on a tree【树链剖分】【或者动态树,那样常数就完了T_T】
- ICMP报文的格式和种类
- 嵌入式 精确获取文件的大小
- 村子四野,麦垄田边,星星散落着一颗两颗的青翠野菜
- nginx常用命令
- fastreport中几个函数意思
- SPOJ375--Query on a tree(树链剖分)
- 2.序列求和
- 无论怎样,我们也是要离开的
- Web应用程序的概述
- 墨西哥总统:被拆掉两次的亭子
- 面对知网的查重率,如何修改你的毕业论文
- HDU 1005Number Sequence
- 皇上是船,百姓是水。水能载舟亦能覆舟
- 第四周项目一