SPOJ375[QTREE - Query on a tree] 树链剖分
2017-08-24 08:46
281 查看
Descripion:
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:
13
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE
Output:
13
solution:树链剖分
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int N = 1e4 + 7; struct Edge{ int nxt, to; }e[N<<1]; int head , tot=0; void addeage(int u, int v){ e[++tot].nxt=head[u], e[tot].to=v; head[u]=tot; } int eg [4], pos , sz , top , fat , dep , son ; int id=0, n; void dfs1(int u, int fa){ son[u]=0; sz[u]=1; dep[u]=dep[fa]+1; for ( int i=head[u]; i; i=e[i].nxt ){ int v=e[i].to; if( v==fa ) continue; fat[v]=u; dfs1(v, u); sz[u]+=sz[v]; if( sz[v]>sz[son[u]] ) son[u]=v; } } void dfs2(int u, int fa, int fq){ pos[u]=++id; top[u]=fq; if( son[u] ){ dfs2(son[u], u, fq); } for ( int i=head[u]; i; i=e[i].nxt ){ int v=e[i].to; if( v==fa || v==son[u] ) continue; dfs2(v, u, v); } } #define ls nd<<1 #define rs nd<<1|1 struct Node{ int maxv; }tr[N<<2]; void pushup(int nd){ tr[nd].maxv=max(tr[ls].maxv, tr[rs].maxv); } void modify(int nd, int l, int r, int p, int val){ if( l==r ){ tr[nd].maxv=val; return ; } int mid=(l+r)>>1; if( mid>=p ) modify(ls, l, mid, p, val); else modify(rs, mid+1, r, p, val); pushup(nd); } int query(int nd, int l, int r, int L, int R){ if( L<=l && r<=R ){ return tr[nd].maxv; } int mid=(l+r)>>1; int mx=0; if( mid>=L ) mx=max(mx, query(ls, l, mid, L, R)); if( mid<R ) mx=max(mx, query(rs, mid+1, r, L, R)); return mx; } void swap(int &a, int &b){ a=a^b; b=a^b; a=a^b; } int query2(int u, int v){ int f1=top[u], f2=top[v]; int ret=0; while( f1!=f2 ){ if( dep[f1]<dep[f2] ) swap(f1,f2), swap(u, v); // printf("%d %d\n", pos[f1], pos[u] ); ret=max(ret, query(1,1,n,pos[f1],pos[u]) ); u=fat[f1]; f1=top[u]; } if( u==v ) return ret; if( dep[u]>dep[v] ) swap(u, v); ret=max(ret, query(1,1,n,pos[son[u]],pos[v]) ); return ret; } void init(){ memset(head,0,sizeof(head)); tot=0; id=0; } int main(){ int T; scanf("%d", &T); while( T-- ){ init(); scanf("%d", &n ); for ( int i=1; i<n; i++ ){ int x, y, z; scanf("%d%d%d", &x, &y, &z ); eg[i][1]=x, eg[i][2]=y, eg[i][3]=z; addeage(x,y), addeage(y,x); } dfs1(1,1); dfs2(1,1,1); // for ( int i=1; i<=n; i++ ) printf("%d ", pos[i] ); // printf("\n"); for ( int i=1; i<n; i++ ){ if( dep[eg[i][1]]<dep[eg[i][2]] ) swap(eg[i][1], eg[i][2]); modify(1,1,n,pos[eg[i][1]],eg[i][3]); // printf("%d %d\n", pos[eg[i][1]], eg[i][3] ); } for (;;){ char ch[10]; int x, y; scanf("%s", ch); if( ch[0]=='D' ) { break; } scanf("%d%d", &x, &y ); if( ch[0]=='Q' ){ printf("%d\n", query2(x,y) ); }else if( ch[0]=='C' ){ modify(1,1,n,pos[eg[x][1]],y); } } } }
相关文章推荐
- SPOJ QTREE Query on a tree --树链剖分
- SPOJ - QTREE Query on a tree 树链剖分
- SPOJ 375 QTREE - Query on a tree 树链剖分+线段树(单点更新 区间查询最值)
- SPOJ QTREE- Query on a tree (树链剖分)
- SPOJ-375 QTREE - Query on a tree (树链剖分 边权转点权)
- SPOJ 375 QTREE Query on a tree 树链剖分水题
- SPOJ QTREE Query on a tree(树链剖分-点更新-区间最值查询-入边)
- SPOJ QTREE - Query on a tree 【树链剖分模板】
- SPOJ QTREE Query on a tree (树链剖分)
- SPOJ 375 Query on a tree(树链剖分)(QTREE)
- 树链剖分模板题-SPOJ QTREE Query on a tree
- [ SPOJ - QTREE]Query on a tree && 树链剖分
- 【SPOJ】QTREE - Query on a tree(树链剖分+线段树(基于边权))
- SPOJ QTREE Query on a tree --树链剖分
- SPOJ QTREE Query on a tree(树链剖分)
- 【树链剖分】[SPOJ-QTREE]Query on a tree
- SPOJ 375 树链剖分 QTREE - Query on a tree
- [spoj QTREE Query on a tree]树链剖分
- spoj 375 QTREE - Query on a tree(树链剖分,边权)
- SPOJ 题目 375 QTREE - Query on a tree(树链剖分)