Spoj topic 913qtree2-query on a tree II (Link Cut tree query path K-point)

Source: Internet
Author: User

Qtree2-query on a tree IINo Tags

You were given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3 ... N-1. Each edge have an integer value of assigned to it, representing its length.

We'll ask you to perfrom some instructions of the following form:

    • DIST a b  : Ask for the distance between Node a  and node b
      or
    • kth a b k  : Ask for The k -th node in the path from node  a  to node b

Example:
N = 6
1 2 1//Edge connects Node 1 and Node 2 have cost 1
2 4 1
2 5 2
1 3 1
3 6 2

Path from node 4 to node 6 is 4, 2, 1, 3, 6
DIST 4 6 : Answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : Answer is 3 (the 4-th node in the path from node 4 to node 6 is 3)

Input

The first line of input contains an integer T, the number of test cases (T <= 25). T test cases follow.

for each test case:

    • in the first line there was an Integer n   (n  <= 10000)
    • n -1 lines, the i-th line describes the i-th edge:a line with three integer S a b C  denotes an edge between a , b  of cost c   ( Span style= "font-weight:700" >c  <= 100000)
    • the Next lines contain instructions < Span style= "font-weight:700" > "DIST a B"  or  "KTH a b k"
    • the end of each test case was signified by the string "done ".

There is one blank line between successive tests.

Output

For each "DIST" or "KTH" operation, the write one integer representing its result.

Print one blank line after each test.

Example
Input:161 2 4 5 3 6 2DIST 4 6KTH 4 6 4DONEOutput:5

3

Title: DIST 4 6 Ask 4 to 6 distance, KTH 4 6 4, ask 4 to 6 for the 4th point

AC Code

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <iostre  Am> #define MAXN 10010using namespace std;  int head[10010],cnt,vis[10010];  struct S {int u,v,w,next;  }edge[10010<<1];      struct LCT {int bef[10010],pre[10010],next[10010][2],key[10010],sum[10010],belong[10010],num[10010];          void Init () {memset (pre,0,sizeof (pre));        memset (Next,0,sizeof (next));  } void Pushup (int x) {sum[x]=key[x]+sum[next[x][1]]+sum[next[x][0]];    num[x]=num[next[x][1]]+num[next[x][0]]+1;            } void rotate (int x,int kind) {int y,z;            Y=PRE[X];            Z=pre[y];            Next[y][!kind]=next[x][kind];            Pre[next[x][kind]]=y;            Next[z][next[z][1]==y]=x;            Pre[x]=z;            Next[x][kind]=y;          Pre[y]=x;      Pushup (y);            } void Splay (int x) {int rt; ForRT=X;PRE[RT];RT=PRE[RT]);                if (X!=RT) {BEF[X]=BEF[RT];                bef[rt]=0; while (Pre[x]) {if (next[pre[x]][0]==x) {Rotat                    E (x,1);                } else rotate (x,0);          } pushup (x);            }} void access (int x) {int fa;                for (Fa=0;x;x=bef[x]) {splay (x);                pre[next[x][1]]=0;                Bef[next[x][1]]=x;                NEXT[X][1]=FA;                Pre[fa]=x;                bef[fa]=0;                Fa=x;          Pushup (x);          }} int query (int x,int y) {access (Y);              for (Y=0;x;x=bef[x]) {splay (x);              if (!bef[x]) return sum[y]+sum[next[x][1]];              pre[next[x][1]]=0; Bef[next[x][1]]=x;              Next[x][1]=y;              Pre[y]=x;              bef[y]=0;              Y=x;          Pushup (x); }} int Select (int x,int k) {while (num[next[x][0]]+1!=k) {if (num[next[x][0]]+1>k) x=next[x][0];else{k-=num[next[x][ 0]]+1;X=NEXT[X][1];}} return x;}          int KTH (int y,int x,int k) {access (Y);              for (Y=0;x;x=bef[x]) {splay (x); if (!bef[x]) {if (num[next[x][1]]+1==k) return X;elseif (num[next[x][1]]+1>k) return Select (next[x][1],num[next[x][            1]]-k+1); return select (Y,k-num[next[x][1]]-1);}              pre[next[x][1]]=0;              Bef[next[x][1]]=x;              Next[x][1]=y;              Pre[y]=x;              bef[y]=0;              Y=x;          Pushup (x);      }}}lct;void Add (int u,int v,int W) {edge[cnt].u=u;      Edge[cnt].v=v;      Edge[cnt].w=w;      Edge[cnt].next=head[u];  head[u]=cnt++;      } void BFs (int u) {int i,y;      queue<int>q;      memset (vis,0,sizeof (VIS));      Vis[u]=1; Q. push (U);          while (!q.empty ()) {U=q.front ();          Q.pop ();              for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;                  if (!vis[v]) {lct.bef[v]=u;                  LCT.KEY[V]=LCT.SUM[V]=EDGE[I].W;                  Vis[v]=1;              Q.push (v); }}}}int main () {int t;scanf ("%d", &t), while (t--) {int n,i;scanf ("%d", &n); Cnt=0;memset (Head,-1,sizeo F (head)), for (i=1;i<n;i++) {int u,v,w;scanf ("%d%d%d", &u,&v,&w); add (u,v,w); add (v,u,w);} Lct.init (); BFS (1); Char Op[10];while (scanf ("%s", op)!=eof) {if (!strcmp (OP, "done")) break;if (op[0]== ' D ') {int x, y; scanf ("%d%d", &x,&y);p rintf ("%d\n", Lct.query (x, y));} Else{int x,y,z;scanf ("%d%d%d", &x,&y,&z);p rintf ("%d\n", LCT. KTH (x, Y, z));}}}


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

Spoj topic 913qtree2-query on a tree II (Link Cut tree query path K-point)

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.