spoj QTREE - Query on a tree(树链刨分)
来源:互联网 发布:虚拟货币交易平台源码 编辑:程序博客网 时间:2024/06/05 20:50
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.
树链刨分入门。题目有两种操作,修改某个边的值,查询两个点之间的最大边,用树链刨分刨分成轻重链重新编号,重链的点的编号是连续的,每次在一个重链中查询,这样就成了线段树的单点更新和区间查询的问题;
#include<bits/stdc++.h>using namespace std;const int MAXN=10010;int head[MAXN],tot;int son[MAXN],top[MAXN],fa[MAXN],p[MAXN],fp[MAXN],num[MAXN],deep[MAXN];struct Edge{ int to,next;}edge[2*MAXN];struct Node{ int l,r; int Max;}segTree[MAXN*4];int e[MAXN][3];int pos;void init(){ memset(head,-1,sizeof(head)); memset(son,-1,sizeof(son)); tot=0;pos=0;}void addedge(int x,int y){ edge[tot].to=y; edge[tot++].next=head[x]; head[x]=tot-1;}void dfs1(int u,int pre,int d){ deep[u]=d; fa[u]=pre; num[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(v!=pre) { dfs1(v,u,d+1); num[u]+=num[v]; if(son[u]==-1||num[v]>num[son[u]]) son[u]=v; } }}void getpos(int u,int sp){ top[u]=sp; if(son[u] != -1) { p[u]=pos++; fp[p[u]]=u; getpos(son[u],sp); } else { p[u]=pos++; fp[p[u]]=u; return; } for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(v!=son[u]&&v!=fa[u]) getpos(v,v); }}void build(int i,int l,int r){ segTree[i].l=l; segTree[i].r=r; segTree[i].Max=0; if(l == r)return; int mid=(l+r)/2; build(i<<1,l,mid); build((i<<1)|1,mid+1,r);}void push_up(int i){ segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);}void update(int i,int k,int val){ if(segTree[i].l==k&&segTree[i].r==k) { segTree[i].Max=val; return; } int mid=(segTree[i].l + segTree[i].r)/2; if(k<=mid)update(i<<1,k,val); else update((i<<1)|1,k,val); push_up(i);}int query(int i,int l,int r){ if(segTree[i].l==l&&segTree[i].r==r) return segTree[i].Max; int mid=(segTree[i].l+segTree[i].r)/2; if(r<=mid) return query(i<<1,l,r); else if(l>mid) return query((i<<1)|1,l,r); else return max(query(i<<1,l,mid),query((i<<1)|1,mid+1,r));}int find(int u,int v){ int f1=top[u],f2=top[v]; int tmp=0; while(f1!=f2) { if(deep[f1]<deep[f2]) { swap(f1,f2); swap(u,v); } tmp=max(tmp,query(1,p[f1],p[u])); u=fa[f1];f1=top[u]; } if(u==v) return tmp; if(deep[u]>deep[v]) swap(u,v); return max(tmp,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++) { scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]); addedge(e[i][0],e[i][1]); addedge(e[i][1],e[i][0]); } dfs1(1,0,0); getpos(1,1); build(1,0,pos-1); for(int i=0;i<n-1;i++) { if(deep[e[i][0]]>deep[e[i][1]]) swap(e[i][0],e[i][1]); update(1,p[e[i][1]],e[i][2]); } char op[10]; int u,v; while(scanf("%s",op)==1) { if(op[0]=='D')break; scanf("%d%d",&u,&v); if(op[0]=='Q') printf("%d\n",find(u,v)); else update(1,p[e[u-1][1]],v); } } return 0;}
0 0
- spoj QTREE - Query on a tree(树链刨分)
- SPOJ QTREE(Query on a tree树链剖分)
- SPOJ QTREE 375. 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 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 (lct)
- 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
- LeetCode 23 - Merge k Sorted Lists
- LeetCode 241 - Different Ways to Add Parentheses
- 【笔记】今天貌似遇到了Python 3的bug
- 高手之路——Po学校学习笔记-第七课
- 方法欺骗
- spoj QTREE - Query on a tree(树链刨分)
- android程序员养成记录----------第一篇
- 高手之路——Po学校学习笔记-第八课
- JVM 垃圾回收器工作原理及使用实例介绍
- uva 120
- spring-mybatis整合oracle
- 关于java数组比较器排序
- php入门到放弃 . Day1
- Retrofit学习笔记