"Spoj qtree" tree chain split template

Source: Internet
Author: User

Use the line tree to solve, note that because the path is the maximum value, pay attention to the details.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 10010;# Define Lson (pos<<1) #define RSON (pos<<1|1) const int INF = (1 <<); int n;//-------------------------- ------------------------struct edge{int to,next;} EDGE[MAXN * 2];int head[maxn],tot;int top[maxn];int fa[maxn];int deep[maxn];int num[maxn];int p[MAXN];int fp[MAXN];int    Son[maxn];int pos;void init () {tot = 0;    memset (head,-1,sizeof (head));    pos = 1; memset (son,-1,sizeof (son));    void Addedge (int u,int v) {edge[tot].to = v;    Edge[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 = 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; Looking for a heavy son}}}void getpos (int u,int sp) {Top[u]= SP;    P[u] = pos++;    printf ("%d%d\n", U,p[u]);    Fp[p[u]] = u;    if (son[u] = =-1) return;    GetPos (SON[U],SP);        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);            }}//----------------------------------------------int MAXV[MAXN << 2]; The one side void build () {memset (maxv,0,sizeof (MAXV));} void pushup (int pos) {Maxv[pos] = max (Maxv[lson],maxv[rson]);}        void update (int l,int r,int to,int value,int pos) {if (L = = r) {Maxv[pos] = value;    Return    } int mid = (L + r) >> 1;    if (to <= mid) update (L,mid,to,value,lson);    else Update (mid + 1,r,to,value,rson); Pushup (POS);}    int query (int l,int r,int l,int r,int pos) {if (L <= l && R <= R) return Maxv[pos];    int mid = (L + r) >> 1;    int ret =-INF;    if (L <= mid) ret = max (Ret,query (L,mid,l,r,lson)); if (R > mid) ret = max(Ret,query (mid + 1,r,l,r,rson)); return ret;}    -------------------------------------------------------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,pos,p[f1],p[u],1));        U = fa[f1];    F1 = Top[u];    } if (U = = v) return tmp;    if (Deep[u] > Deep[v]) swap (U,V); Return Max (Tmp,query (1,pos,p[son[u]],p[v],1));} ----------------------------------------------------------struct e{int from,to,value;}    E[maxn];int Main () {int T;    int u,v;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();            for (int i = 0; i < n-1; i++) {scanf ("%d%d%d", &e[i].from,&e[i].to,&e[i].value);            Addedge (e[i].from,e[i].to);        Addedge (E[i].to,e[i].from);        } DFS1 (1,0,0);        GetPos (a);        Build (); for (int i = 0; i < n-1; i++) {if (Deep[e[i].from] > Deep[e[i].to]) swap (e[i].from,e[i].to);        Here is updating a line segment, not a dot update (1,pos,p[e[i].to],e[i].value,1);        } Char op[10];            while (scanf ("%s", op)! = EOF) {if (op[0] = = ' D ') break;            scanf ("%d%d", &u,&v);            if (op[0] = = ' Q ') {printf ("%d\n", Find (u,v));            } else{Update (1,pos,p[e[u-1].to],v,1);    }} if (T) puts (""); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Spoj qtree" tree chain split template

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.