HDU 5296 annoying problem (LCA template + Tree DFS sequence experience)

Source: Internet
Author: User

Problem Descriptioncoco has a tree, whose nodes is conveniently labeled by,..., N, which have n-1 Edge,each Edge has a We Ight. An existing set S is initially empty.
Now there is kinds of operation:

1 x:if the node x is not in the set S, and add node X to the set S
2 X:if the node x is in the set S,delete node x from the set S

Now there are a annoying problem:in order to select a set of the edges from the tree after each operation which makes any and node s in set S connected. What is the minimum of the sum of the selected edges ' weight?


Inputone integer number T is described in the first line represents the group number of testcases. (t<=10)
For each test:
The first line has 2 integer number N,q (0<n,q<=100000) describe the number of nodes and the number of operations.
The following n-1 lines each line have 3 integer number u,v,w describe that between node U and Node v have an edge weight w. (1<=u,v<=n,1<=w<=100)
The following q lines each line has 2 integer number x, y describe one operation. (X=1 or 2,1<=y<=n)



Outputeach TestCase outputs a line of ' case #x: ', X starts from 1.
The next Q line represents the answer to each operation.


Sample Input
16 51 2 21 5 25 6 22 4 22 3 21 51 31 41 22 5

Sample Output
Case #1:06884

Authorfzuacm
Source2015 multi-university Training Contest 1

Test instructions

Given a tree (satisfying n points, n-1 edges), and a set of points that were originally empty, there are two operations:

Action 1: Add a point x to the set S (if X is not inside the collection)

Action 2: Delete the point x from the set S (if x is inside the set)

For each operation, a set of edges from the tree is found to satisfy any two-point connectivity in S, and the minimum Bong (Benquan) is obtained.

Analysis:

Remember that the starting question for LCA (HDU2586) is the shortest distance between any two points in the tree, with the formula

D (A, b) min = Dist[a] + dist[b]-2*dist[lca (A, b)], where Dist is the distance from the node to the root node

This problem is clearly the problem of LCA, but the formula is not very easy to find, the question is to say:

Pre-processing the DFS sequence first
For the Add Point U operation:
Each time you find a point in the collection with a Dfs order that adds a point that is smaller than he is the largest point and the smallest point bigger than him,

Assuming that these two points are X, Y (if not found, we look for the maximum and minimum two points of the dictionary order, for the reasons given below)
The cost of each increase is dis[u]-Dis[lca (x,u)]-Dis[lca (x, y)]; where Dis remembers the distance from the point to the root node
For the delete point U operation:
Each time you remove a point from the collection and then calculate the cost reduction, the calculation formula is the same as the increment calculation method
Reasons to select two points based on the DFS sequence:
If the DFS sequence can be found in a collection that is smaller than the operating point and the operating point, then the cost of the change is equal to
The distance from the operation point to the chain with the two points as the end point can be calculated using the above formula

If the DFS sequence in the collection is smaller or larger than the operating point,

The cost of the change is the distance from the operation point to the end of the chain at the point where the dictionary is the largest and the smallest dictionary order, or it can be calculated using the formula above

Here you say the DFS sequence, x, Y is also the DFS sequence, in fact, the DFS sequence is simply a representation of the relationship between U,x,y, the real calculation is still to restore to the original tree node to calculate

Therefore, compared to the general LCA template, you also need to open an array to save the DFS sequence corresponding node

About the DFS sequence, has been feeling vaguely unclear, this time to take this question to understand a bit

First, 鶸 's LCA Template:

struct node{int U, V, W, next;};    struct lca{int dp[2 * N][M];    BOOL Vis[n];    int tot, head[n];    Node E[2*n];        void Addedge (int u, int v, int w, int &k) {e[k].u = u, e[k].v = V, e[k].w = w;        E[k].next = Head[u];    Head[u] = k++;    } int ver[2 * n], d[2 * n], first[n], Dis[n];//dis[i] indicates the distance of node I from the root node//first[i] indicates the DFS Order//node number depth point number position distance of node I        void init () {mem (head,-1);        MEM (vis,0);    tot = 0;dis[1] = 0;        } void Dfs (int u, int dep) {Vis[u] = 1;        Ver[++tot] = u;        First[u] = tot;        D[tot] = DEP; for (int k = Head[u]; K! =-1; k = e[k].next) {if (!vis[e[k].v]) {int v = e[                K].V, w = E[K].W;                DIS[V] = Dis[u] + W;                DFS (V, DEP + 1);                Ver[++tot] = u;            D[tot] = DEP;        }}}} void ST (int n) {for (int i = 1; I <= n; ++i) dp[i][0] = i; for (int j = 1; (1 << j) <= N; ++J) {for (int i = 1; i + (1 << J)-1 <= N; ++i) {int a = Dp[i][j                -1], B = dp[i + (1 << (j-1))][j-1]; DP[I][J] = D[a] < D[b]?            A:B;        }}} int RMQ (int l, int r) {int k = 0;        while ((1 << (k + 1)) <= R-l + 1) k++;        int a = Dp[l][k], B = dp[r-(1 << k) + 1][k]; return D[a] < D[b]?    A:B;        } int Lca (int u, int v) {int x = First[u], y = first[v];        if (x > Y) Swap (x, y);    return ver[rmq (x, y)]; }}ans;

Draw a diagram to help understand the DFS sequence:















The first table, I, is the sequence in which the tree is traversed sequentially in sequence,

Ver represents the number of points visited during the traversal, and D indicates the depth of the point I visited during the traversal

The key is the second table, where first is the DFS sequence inside the 鶸 template, and how does first come from?

In fact, the first table in the first time each point is accessed the corresponding I value, the Red pen is marked by the weak DFS sequence

Online to see some people's DFS sequence is from 1,2,3,4 ... such a continuous natural number, but it doesn't matter, my DFS sequence discretization is actually the same

The DFS sequence preserves the nonlinear tree structure with a linear structure, in addition, a very important property of the DFS sequence is:

All nodes of a subtree are contiguous in the DFS sequence

The DFS sequence has many uses in the tree structure, and LCA is just one of them (the end of the article is affixed to the DFS sequencing application of the relevant information)

In addition to the DFS sequence, what is the BFS sequence, tree-cut, LCT what (鶸 said temporarily not ... )

Back to the chase .... This question ....

LCA template Nothing to say, the calculation of the part of the code is very clear, on the code:

#define MEM (a,x) memset (A,x,sizeof (a)) #include <iostream> #include <cstdio> #include <cstring># include<algorithm> #include <queue> #include <set> #include <stack> #include <cmath># include<map> #include <stdlib.h> #include <cctype> #include <string>using namespace std; typedef long LONG ll;const int N = 400010;const int M = 45;struct node{int u, V, W, next;};    struct lca{int dp[2 * N][M];    BOOL Vis[n];    int tot, head[n];    Node E[2*n];        void Addedge (int u, int v, int w, int &k) {e[k].u = u, e[k].v = V, e[k].w = w;        E[k].next = Head[u];    Head[u] = k++;    }//Node number depth point number position distance int ver[2 * N], d[2 * n], first[n], Dis[n];//dis[i] indicates node I distance from root node//first[i] indicates DFS order for node I        int P[2*n];//p[i] Indicates the DFS sequence is the node of I//i.e.: if first[i] = j, then p[j] = i void init () {mem (head,-1);        MEM (vis,0);    tot = 0;dis[1] = 0;        } void Dfs (int u, int dep) {Vis[u] = 1; Ver[++toT] = u;        First[u] = tot;        P[tot] = u;        D[tot] = DEP; for (int k = Head[u]; K! =-1; k = e[k].next) {if (!vis[e[k].v]) {int v = e[                K].V, w = E[K].W;                DIS[V] = Dis[u] + W;                DFS (V, DEP + 1);                Ver[++tot] = u;            D[tot] = DEP;        }}}} void ST (int n) {for (int i = 1; I <= n; ++i) dp[i][0] = i; for (int j = 1; (1 << j) <= N; ++J) {for (int i = 1; i + (1 << J)-1 <= N; ++i) {int a = Dp[i][j                -1], B = dp[i + (1 << (j-1))][j-1]; DP[I][J] = D[a] < D[b]?            A:B;        }}} int RMQ (int l, int r) {int k = 0;        while ((1 << (k + 1)) <= R-l + 1) k++;        int a = Dp[l][k], B = dp[r-(1 << k) + 1][k]; return D[a] < D[b]?    A:B; } int Lca (int u, int v) {int x = First[u], y = first[v];        if (x > Y) Swap (x, y);    return ver[rmq (x, y)];    }}ans;set<int>s;bool vis[n+5];//Determines whether a point is in the set int Cal (int u)//calculates the added cost of adding a DFS-ordered point to the set of points {if (S.empty ()) return 0;  The int x,y;//x,y is the smallest point larger than the point of the DFS order U and the largest point smaller than it set<int>::iterator it = s.upper_bound (u);//returns the element iterator position of the first key value greater than u in the collection if (it = = S.end () | | it = = S.begin ())//No bigger or bigger than him. {x = Ans.p[*s.rbegin ()];//*s.rbegin () is the DFS sequence, where the nodes in the original tree are saved in x y = ans.p[*s    . Begin ()];        } else {x = Ans.p[*it];        it--;    y = Ans.p[*it]; } U = Ans.p[u];//u also swapped back to the node on the original tree to calculate return ans.dis[u]-Ans.dis[ans. Lca (X,u)]-Ans.dis[ans. Lca (y,u)] + Ans.dis[ans. Lca (x, y)];}    int main () {int t;scanf ("%d", &t), int kas = 0;        while (t--) {int n,m;scanf ("%d%d", &n,&m);        S.clear ();        Ans.init (); int k = 0;            for (int i = 1,u,v,w;i < N;++i) {scanf ("%d%d%d", &u,&v,&w); Ans.            Addedge (U,V,W,K); SwaP (u,v); Ans.        Addedge (U,V,W,K);        } Ans.dfs (a); Ans.        ST (2*n-1); int op,u;        printf ("Case #%d:\n", ++kas);        int sun = 0;mem (vis,0);            for (int i = 0;i < M;++i) {scanf ("%d%d", &op,&u);                    if (op = = 1) {if (!vis[u]) {Vis[u] = 1;                Sun + = Cal (Ans.first[u]);//Use the DFS sequence of U to calculate S.insert (Ans.first[u]);                    }} else {if (Vis[u]) {Vis[u] = 0;                    S.erase (Ans.first[u]);                Sun-= Cal (Ans.first[u]);        }} printf ("%d\n", Sun); }} return 0;}

Refer to other people's puzzle, but others ' LCA template is not the same as mine, other people's DFS sequence to find out is the continuous natural number starting from 1: Click to open the link

Look at the application summary of the DFS sequence: Click the Open link


HDU 5296 annoying problem (LCA template + Tree DFS sequence experience)

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.