SPOJ375 Query on a tree chain split

Source: Internet
Author: User

SPOJ375 Query on a tree chain split No Tags

You is given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...n-1.

We'll 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 in 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:

    • The first line there are 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 was signified by the string "done".

There is one blank line between successive tests.

Output

For each "QUERY" operation, the write one integer representing its result.

Example
input:131 2 3 2QUERY 1 2CHANGE 1 3QUERY 1 2doneoutput:13
Test instructions: to a tree, n points, n-1 edge, with edge right, multiple operations, there are two kinds of operation, change the Benquan of an edge, or ask two points on the path of the maximum edge right.
First tree chain split.
Tree chain is to split the tree into a number of chains, end-to-end, and then use data structure (line tree, etc.).
#include <bits/stdc++.h>#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intmaxn=1000100;Const intInf= (1<< in);intN;Charop[ -];intU,v,w;vector<int>G[MAXN];intDEP[MAXN],SON[MAXN],FA[MAXN],SIZ[MAXN];intTOP[MAXN];intID[MAXN];intnum;intVAL[MAXN];structtree{intU,v,val;}; Tree E[MAXN];structsegtree{intVal;}; Segtree T[MAXN<<2];voidDFS1 (intUintFintd) {Fa[u]=F; Dep[u]=D; Son[u]=0; Siz[u]=1;  for(intI=0; I<g[u].size (); i++){        intv=G[u][i]; if(v==f)Continue; DFS1 (V,u,d+1); if(Siz[v]>siz[son[u]]) son[u]=v; Siz[u]+=Siz[v]; }}voidDFS2 (intUintTP) {Top[u]=TP; Id[u]=++num; if(Son[u]) DFS2 (SON[U],TP);  for(intI=0; I<g[u].size (); i++){        intv=G[u][i]; if(v==fa[u]| | V==son[u])Continue;    DFS2 (V,V); }}voidPUSH_UP (intRT) {T[rt].val=max (t[rt<<1].val,t[rt<<1|1].val);}voidBuildintLintRintRT) {    if(l==S) {T[rt].val=Val[l]; return; }    intM= (l+r) >>1;    Build (Lson);    Build (Rson); PUSH_UP (RT);}voidUpdateintPintCintLintRintRT) {    if(l==R) {T[rt].val=C; return; }    intM= (l+r) >>1; if(p<=m) update (P,c,lson); ElseUpdate (P,c,rson); PUSH_UP (RT);}intQueryintLintRintLintRintRT) {    if(L&LT;=L&AMP;&AMP;R&LT;=R)returnT[rt].val; intM= (l+r) >>1; intres=-INF; if(l<=m) res=Max (Res,query (L,r,lson)); if(r>m) res=Max (Res,query (L,r,rson)); returnRes;}intChaintUintv) {    intres=-INF;  while(top[u]!=Top[v]) {        if(dep[top[u]]<Dep[top[v]) swap (U,V); Res=max (Res,query (Id[top[u]],id[u),1Num1)); U=Fa[top[u]]; }    if(u!=v) {        if(dep[u]>Dep[v]) swap (U,V); Res=max (Res,query (ID[SON[U]],ID[V),1Num1)); }    returnRes;}intMain () {//freopen ("In.txt", "R", stdin);    intCasen; CIN>>Casen;  while(casen--) {cin>>O;  for(intI=1; i<=n;i++) g[i].clear ();  for(intI=1; i<n;i++) {scanf ("%d%d%d",&u,&v,&W); E[i]={u,v,w};            G[u].push_back (v);        G[v].push_back (U); } num=0; DFS1 (1,0,1); DFS2 (1,1);  for(intI=1; i<n;i++){            if(dep[e[i].u]>DEP[E[I].V]) swap (E[I].U,E[I].V); VAL[ID[E[I].V]]=E[i].val; } Build (1Num1);  while(~SCANF ("%s", op)) {            if(op[0]=='D') Break; if(op[0]=='C') {scanf ("%d%d",&u,&W); intp=ID[E[U].V]; Update (P,W,1Num1); }            Else{scanf ("%d%d",&u,&v); printf ("%d\n", Cha (u,v)); }        }    }    return 0;}
View Code

SPOJ375 Query on a tree chain split

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.