题目链接:http://www.spoj.com/problems/QTREE/en/
QTREE - Query on a tree
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: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3
题意:给你一颗数,求u到v的路径中最大的边权大小;
思路:线段树单点更新区间查询;
树链剖分板子;
最后把边权往下落,去掉lca那个点的权值;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=2e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; ///数组大小 struct edge{ int v,next; }edge[N<<1]; int head[N<<1],edg,id,n; /// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数 int a[N],ran[N],top[N],tid[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid int u[N],v[N],w[N]; void init() { memset(son,-1,sizeof(son)); memset(head,-1,sizeof(head)); edg=0; id=0; } void add(int u,int v) { edg++; edge[edg].v=v; edge[edg].next=head[u]; head[u]=edg; } void dfs1(int u,int fath,int deep) { fa[u]=fath; siz[u]=1; dep[u]=deep; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fath)continue; dfs1(v,u,deep+1); siz[u]+=siz[v]; if(son[u]==-1||siz[v]>siz[son[u]]) son[u]=v; } } void dfs2(int u,int tp) { tid[u]=++id; top[u]=tp; ran[tid[u]]=u; if(son[u]==-1)return; dfs2(son[u],tp); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa[u])continue; if(v!=son[u]) dfs2(v,v); } } /// 线段树 int sum[N<<2]; void pushup(int pos) { sum[pos]=max(sum[pos<<1],sum[pos<<1|1]); } void build(int l,int r,int pos) { if(l==r) { sum[pos]=a[ran[l]]; return; } int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos); } void update(int p,int c,int l,int r,int pos) { if(l==r) { sum[pos]=c; return; } int mid=(l+r)>>1; if(p<=mid)update(p,c,l,mid,pos<<1); if(p>mid) update(p,c,mid+1,r,pos<<1|1); pushup(pos); } int query(int L,int R,int l,int r,int pos) { if(L<=l&&r<=R)return sum[pos]; int mid=(l+r)>>1; int ans=0; if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<1)); if(R>mid) ans=max(ans,query(L,R,mid+1,r,pos<<1|1)); return ans; } int up(int l,int r) { int ans=0; while(top[l]!=top[r]) { if(dep[top[l]]<dep[top[r]])swap(l,r); ans=max(ans,query(tid[top[l]],tid[l],1,n,1)); l=fa[top[l]]; } if(dep[l]==dep[r])return ans; if(dep[l]<dep[r])swap(l,r); //cout<<tid[r]<<" "<<tid[l]<<" "<<endl; ans=max(ans,query(tid[son[r]],tid[l],1,n,1)); return ans; } char s[10]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); init(); for(int i=1;i<n;i++) { scanf("%d%d%d",&u[i],&v[i],&w[i]); add(u[i],v[i]); add(v[i],u[i]); } dfs1(1,-1,1); dfs2(1,1); for(int i=1;i<n;i++) { if(fa[u[i]]==v[i])a[u[i]]=w[i]; else a[v[i]]=w[i]; } build(1,n,1); while(1) { scanf("%s",s); if(s[0]=='D')break; int a,b; scanf("%d%d",&a,&b); if(s[0]=='C') { if(fa[u[a]]==v[a])update(tid[u[a]],b,1,n,1); else update(tid[v[a]],b,1,n,1); } else { printf("%d\n",up(a,b)); } } } return 0; }