SPOJ QTREE - Query on a tree(树链剖分)
2015-10-03 22:43
495 查看
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
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.
For each "QUERY" operation, write one integer representing its result.
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: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3 分析:入门题。细节还要看下。#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int maxn=10010; struct node{ int to,next; }e[maxn*2]; int head[maxn],tot,pos; int top[maxn],fa[maxn],deep[maxn],num[maxn],p[maxn],son[maxn],fp[maxn]; int t,n; int op[maxn][3]; void init() { tot=0;pos=0; CLEAR(head,-1); CLEAR(son,-1); } void addedge(int u,int v) { e[tot].to=v;e[tot].next=head[u]; head[u]=tot++; } 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=e[i].next) { int v=e[i].to; if(pre==v) continue; dfs1(v,u,d+1); num[u]+=num[v]; if(son[u]==-1||num[v]>num[son[u]]) son[u]=v; } } void dfs2(int u,int d) { top[u]=d; p[u]=pos++; fp[p[u]]=u; if(son[u]==-1) return ; dfs2(son[u],d); for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(son[u]!=v&&v!=fa[u]) dfs2(v,v); } } int sum[maxn<<2]; void pushup(int rs) { sum[rs]=max(sum[rs<<1],sum[rs<<1|1]); } void update(int x,int c,int l,int r,int rs) { if(l==r) { sum[rs]=c; return ; } int mid=(l+r)>>1; if(x<=mid) update(x,c,l,mid,rs<<1); else update(x,c,mid+1,r,rs<<1|1); pushup(rs); } int query(int x,int y,int l,int r,int rs) { if(l>=x&&r<=y) return sum[rs]; int mid=(l+r)>>1; int res=0; if(x<=mid) res=max(res,query(x,y,l,mid,rs<<1)); if(y>mid) res=max(res,query(x,y,mid+1,r,rs<<1|1)); return res; } int Find(int u,int v) { int f1=top[u],f2=top[v]; int res=0; while(f1!=f2) { if(deep[f1]<deep[f2]) { swap(f1,f2); swap(u,v); } res=max(res,query(p[f1],p[u],1,pos,1)); u=fa[f1];f1=top[u]; } if(u==v) return res; if(deep[u]>deep[v]) swap(u,v); return max(res,query(p[son[u]],p[v],1,pos,1)); } int main() { int x,y,w; scanf("%d",&t); while(t--) { scanf("%d",&n); init(); for(int i=0;i<n-1;i++) { scanf("%d%d%d",&op[i][0],&op[i][1],&op[i][2]); addedge(op[i][0],op[i][1]); addedge(op[i][1],op[i][0]); } dfs1(1,0,0); dfs2(1,1); CLEAR(sum,0);//build(1,1,n); for(int i=0;i<n-1;i++) { if(deep[op[i][0]]>deep[op[i][1]]) swap(op[i][0],op[i][1]); update(p[op[i][1]],op[i][2],1,pos,1); } char str[10]; while(~scanf("%s",str)) { if(str[0]=='D') break; scanf("%d%d",&x,&y); if(str[0]=='Q') printf("%d\n",Find(x,y)); else update(p[op[x-1][1]],y,1,pos,1); } } return 0; }
相关文章推荐
- iOS大典之UIScrollView
- [java学习笔记]java语言基础概述之转义字符&break&continue
- android--day02(UI、事件)
- iOS图文攻略之UILabel属性
- UITableViewDelegate方法
- 选择Key-Value Store
- Key-Value存储系统简介
- (一三〇)UITextField的光标操作扩展
- (一三〇)UITextField的光标操作扩展
- Permutation Sequence
- iOS图文攻略之UIView 属性
- Leetcode catalogue
- 关于queue和stack没有clear方法
- sqlite采用UUID主键和int型自增主键性能对比测试
- goquery使用
- UITextField  中文输入法
- Android UI 优化——使用HierarchyViewer工具
- 2015/10/3 iOS 笔记 细节 iOS9中UIAlertController的简单使用 ScrollView NSTimer
- Arduino2-库文件及基本函数
- 1007. Maximum Subsequence Sum (25)