Spoj 375 qtree-query on a tree chain split LCT dynamic tree

Source: Internet
Author: User

qtree-query on a tree 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 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 a integer N (n <= 10000), in the next N-1 lines, the i-th line describe s 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" and 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:
1

3
1 2 1
2 3 2
query 1 2 change
1 3
query 1 2
done

Output:
1
3

Analysis:

Establish a node for each point with a value of 0.

Establishes a node for each edge, with a value of edge.

Then you can use LCT maintenance

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <
Vector> using namespace std;
    #define MAXN 500007 #define INF 1000000000 #define LL int struct node{Node *fa,*ch[2];
    BOOL Rev,root;
    int Val;
ll MINV;
};
Node POOL[MAXN];
Node *NIL,*TREE[MAXN];
int cnt = 0;
    void init () {cnt = 1;
    Nil = tree[0] = pool;
    Nil->ch[0] = nil->ch[1] = nil;
    Nil->val = 0;
NIL-&GT;MINV = 0;
    } Node *newnode (int val,node *f) {pool[cnt].fa = f;
    Pool[cnt].ch[0]=pool[cnt].ch[1]=nil;
    Pool[cnt].rev = false;
    Pool[cnt].root = true;
    Pool[cnt].val = val;
    POOL[CNT].MINV = val;
Return &pool[cnt++];
    }///left and right subtree reversal ****** really turns the node into a root void Update_rev (node *x) {if (x = = nil) return;
    X->rev =!x->rev;
Swap (x->ch[0],x->ch[1]);
    //splay update information ****** void update (Node *x) {if (x = = nil) return;
    X-&GT;MINV = x->val;
    Node*y = x->ch[0]; if (Y->minv > X->miNV) X-&GT;MINV = y->minv;
    y = x->ch[1];
if (Y->minv > X->minv) X-&GT;MINV = y->minv;
        }//splay push information ****** void pushdown (Node *x) {if (X->rev! = False) {Update_rev (x->ch[0]);
        Update_rev (x->ch[1]);
    X->rev = false;
    }}//splay pushes information ****** void push (Node *x) {if (!x->root) push (X-&GT;FA) in Root-->x's path;
Pushdown (x);
    }//The node x is rotated to the position of the father in splay ****** void rotate (node *x) {node *f = x->fa, *ff = f->fa;
    int t = (f->ch[1] = = x);
    if (f->root) X->root = True, F->root = false;
    else ff->ch[ff->ch[1] = = f] = x;
    X-&GT;FA = FF;
    F->ch[t] = x->ch[t^1];
    X-&GT;CH[T^1]-&GT;FA = f;
    X->ch[t^1] = f;
    F-&GT;FA = x;
Update (f);
    }//Rotate node x to the root of the splay where x is located ****** void splay (Node *x) {push (x);
    Node *f, *ff;
        while (!x->root) {f = X->fa,ff = f->fa; if (!f->root) if ((ff->ch[1]==f) && (f->ch[1] = =x)) rotate (f);
        else rotate (x);
    Rotate (x);
} update (x);
    }//Take x to the root of the path and form a path****** node *access (node *x) {node *y = nil,*z;
        while (x! = nil) {splay (x);
        X->ch[1]->root = true;
        (x->ch[1] = y)->root = false;
        Update (x);
        y = x;
    x = x->fa;
} return y;
    }//Turn node x into root ****** void be_root (node *x) {access (x);
    Splay (x);
Update_rev (x);
    }//Connect X to node F on ****** void link (node *x, node *f) {be_root (x);
X-&GT;FA = f;
    } node * FIND (node *root) {if (root->ch[0] = = nil) return root;
Return find (Root->ch[0]);
} NODE*ROAD[MAXN];
    int main () {int n,q,t;
    Node*x,*y,*z;
    scanf ("%d", &t);
    Char word[20];

        while (t--) {scanf ("%d", &n);
        Init ();
        int u,v,t;
        for (int i = 1;i <= n; i++) Tree[i] = NewNode (0,nil);
            for (int i = 1;i < n; i++) {scanf ("%d%d%d", &u,&v,&t); Road[i] =x = NewNode (T,nil);
            Link (tree[u],x);
        Link (tree[v],x);
            } while (1) {scanf ("%s", word);
            if (word[0] = = ' D ') break;
            scanf ("%d%d", &u,&v);
                if (word[0] = = ' Q ') {be_root (tree[u]);
                y = Access (tree[v]);
            printf ("%d\n", Y-&GT;MINV);
                } else {splay (road[u]);
                Road[u]->val = v;
            Update (Road[u]);
}}} return 0; }/* 3 100 1 1 2 1 2 3 2 0 2 1 2 3 0 3 1 2 5 0 2 * *


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.