HDOJ 5293 Tree chain problem LCA + Tree link splitting + Tree DP, hdoj5293

Source: Internet
Author: User

HDOJ 5293 Tree chain problem LCA + Tree link splitting + Tree DP, hdoj5293


[Question]

Given several chains on a tree and the weights of each chain, find the maximum weights of the chains that do not contain public nodes ....

[Solution]

Preprocessing the lca of each chain

Tree-like DP. d [I] indicates the maximum value that can be obtained when the node is obtained. sum [I] = sigma (d [k] | k is the subnode of I)

If I d [I] = sum [I] is not obtained,

If I is taken, e is the link with the lca as I, d [I] = max (d [I], e's weight + sigma (sum [k]) -sigma (d [k]) k is the point on the tree chain.

You can use the tree link splitting + tree loading array to calculate the value of the chain in the time complexity of nlogn.


Tree chain problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 430 Accepted Submission (s): 114


Problem DescriptionCoco has a tree, whose vertices are conveniently labeled by 1, 2 ,..., N.
There are m chain on the tree, Each chain has a certain weight. Coco wocould like to pick out some chains any two of which do not share common vertices.
Find out the maximum sum of the weight Coco can pick
 
InputThe input consists of several test cases. The first line of input gives the number of test cases T (T <= 10 ).
For each tests:
First line two positive integers n, m. (1 <= n, m <= 100000)
The following (n-1) lines contain 2 integers ai bi denoting an edge between vertices ai and bi (1 ≤ ai, bi ≤ n ),
Next m lines each three numbers u, v and val (1 ≤ u, v ≤ n, 0 <val <1000), represent the two end points and the weight of a tree chain.
 
OutputFor each tests:
A single integer, the maximum number of paths.
 
Sample Input
17 31 21 32 42 53 63 72 3 44 5 36 7 3
 
Sample Output
6Hint Stack expansion program: #pragma comment(linker, "/STACK:1024000000,1024000000")  
 
AuthorFZUACM
Source2015 Multi-University Training Contest 1
RecommendWe have carefully selected several similar problems for you: 5299 5298 5297 5295 5294

/*************************************** * ******** Author: CKbossCreated Time: Wednesday, June 16, July 22, 2015 File Name: HDOJ5293.cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> # pragma comment (linker, "/STACK: 102400 100100 ") using namespace std; const int maxn =; int n, m; /*********************** CHAIN **************** * *****/struct Chain {int from, to, weight;} chain [maxn]; // chain lca at pos ivector <int> vi [maxn]; /********************** EDGE **************** * *******/struct Edge {int, next;} edge [maxn * 2]; int Adj [maxn], Size; void init () {memset (Adj,-1, sizeof (Adj); Size = 0; for (int I = 0; I <= n; I ++) vi [I]. clear () ;} Void Add_Edge (int u, int v) {edge [Size]. to = v; edge [Size]. next = Adj [u]; Adj [u] = Size ++ ;} /********************** LCA **************** * ********/const int DEG = 22; int fa [maxn] [DEG]; // fa [I] [j] 2nd ^ j ancestor int deg [maxn] of node I; /// depth void BFS (int root) {queue <int> q; deg [root] = 0; fa [root] [0] = root; q. push (root); while (! Q. empty () {int u = q. front (); q. pop (); for (int I = 1; I <DEG; I ++) {fa [u] [I] = fa [fa [u] [I-1] [I-1];} for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v = fa [u] [0]) continue; deg [v] = deg [u] + 1; fa [v] [0] = u; q. push (v) ;}} int LCA (int u, int v) {if (deg [u]> deg [v]) swap (u, v ); int hu = deg [u], hv = deg [v]; int tu = u, TV = v; for (int det = hv-hu, I = 0; det; I ++, det = det/2) if (det & 1) TV = fa [TV] [I]; if (tu = TV) return tu; for (int I = DEG-1; I> = 0; I --) {if (fa [tu] [I] = fa [TV] [I]) continue; tu = fa [tu] [I]; TV = fa [TV] [I];} return fa [tu] [0];} /************************* tree chain Split into *****************/int Fa [maxn], deep [maxn], num [maxn], son [maxn]; int top [maxn], p [maxn], rp [maxn], pos; int tree1 [maxn], tree2 [maxn]; int ans; void INIT () {init (); pos = 1; ans = 0; memset (son,-1, sizeof (son); memset (tree1, 0, sizeof (tree1); memset (tree2, 0, sizeof (tree2);} void dfs1 (int u, int pre, int d) {num [u] = 1; fa [u] = pre; deep [u] = d; for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v = pre) continue; dfs1 (v, u, d + 1); num [u] + = num [v]; if (son [u] =-1) | (num [son [u] <num [v]) son [u] = v ;}} void getPOS (int u, int to) {top [u] = to; p [u] = pos ++; rp [p [u] = u; if (son [u]! =-1) getPOS (son [u], to); for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v! = Fa [u] & v! = Son [u]) getPOS (v, v) ;}// arrayinline int lowbit (int x) {return x & (-x );} void Add (int * tree, int p, int v) {for (int I = p; I <maxn; I + = lowbit (I )) tree [I] + = v;} int _ sum (int * tree, int p) {int sum = 0; for (int I = p; I; i-= lowbit (I) sum + = tree [I]; return sum;} int Query (int * tree, int L, int R) {return _ sum (tree, r)-_ sum (tree, L-1);} // query sum of node on the chainint get_chain_sum (int u, int v) {int f1 = top [u], f2 = top [v]; // r Et1: sum of sum tree1 // ret2: sum of d tree2int ret1 = 0, ret2 = 0; while (f1! = F2) {if (deep [f1] <deep [f2]) {swap (f1, f2); swap (u, v );} // ret + = ret1 + = Query (tree1, p [f1], p [u]); ret2 + = Query (tree2, p [f1], p [u]); u = Fa [f1]; f1 = top [u];} if (deep [u]> deep [v]) swap (u, v ); // ret + = ret1 + = Query (tree1, p [u], p [v]); ret2 + = Query (tree2, p [u], p [v]); return ret1-ret2;} // add one point on the node of the chainvoid update (int * tree, int x, int v) {Add (tree, p [x], v);} int d [maxn], sum [maxn]; void DFS (int u, int fa) {sum [u] = 0; for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v = fa) continue; DFS (v, u); sum [u] + = d [v];} // not choose u; d [u] = sum [u]; update (tree1, u, sum [u]); // choose ufor (int I = 0, sz = vi [u]. size (); I <sz; I ++) {int p = vi [u] [I]; int w = chain [p]. weight, from = chain [p]. from, to = chain [p]. to; int temp = w + get_chain_sum (from, to); d [u] = max (d [u], temp);} if (ans <d [u]) ans = d [u]; update (tree2, u, d [u]);} int main () {// freopen ("in.txt", "r", stdin ); // freopen ("out.txt", "w", stdout); int T_T; scanf ("% d", & T_T); while (T_T --) {scanf ("% d", & n, & m); INIT (); for (int I = 0, u, v; I <n-1; I ++) {scanf ("% d", & u, & v); Add_Edge (u, v); Add_Edge (v, u) ;} BFS (1 ); for (int I = 0, u, v, w; I <m; I ++) {scanf ("% d", & u, & v, & w); // chain [I] = (Chain) {u, v, w}; chain [I]. from = u; chain [I]. to = v; chain [I]. weight = w; int lca = LCA (u, v); vi [lca]. push_back (I);} dfs1 (, 0); getPOS (); DFS (); printf ("% d \ n", ans);} return 0 ;}



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.