Tree link partitioning

Source: Internet
Author: User
Tags cmath

Http://blog.sina.com.cn/s/blog_7a1746820100wp67.html others turn to explain, read this learning.

It doesn't feel necessary. Other methods can also be used. This algorithm is generally required to maintain the highest value of the tree path.

Spoj375

Edge Weight path Division

# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # include <cmath> using namespace STD; const int maxn = 10000 + 123; int T [maxn <2]; int m; void initt (int x) {memset (T, 0, sizeof (t); M = 1 <(INT) ceil (log (x + 1.0)/log (2.0);} void Update (int x, int v) {for (T [x + = m] = V, x> = 1; X; x> = 1) {T [x] = max (T [x <1], T [x <1 | 1]) ;}} int query (int l, int R) {int res =-1; for (L + = M-1, R + = m + 1; L ^ r ^ 1; L> >=1, r >>=1) {If (~ L & 1) RES = max (T [L ^ 1], Res); If (R & 1) RES = max (T [R ^ 1], Res );} return res;} struct edge {int V, next, W;} edge [maxn <1]; int head [maxn], CNT; void addedge (int u, int V, int W) {edge [CNT]. V = V; edge [CNT]. W = W; edge [CNT]. next = head [u]; head [u] = CNT ++;} int Dep [maxn], POS [maxn], Fa [maxn], top [maxn], son [maxn], siz [maxn]; int idx; // POS the position of Father edge int seg-treevoid DFS (int u) {siz [u] = 1; son [u] =-1; f Or (int p = head [u]; ~ P; P = edge [p]. next) {const Int & V = edge [p]. v; If (V = Fa [u]) continue; Fa [v] = u; Dep [v] = Dep [u] + 1; DFS (v ); if (siz [v]> siz [son [u]) Son [u] = V; siz [u] + = siz [v];} void DFS _ (int u, int TP) {pos [u] = ++ idx; top [u] = TP; If (~ Son [u]) DFS _ (son [u], top [u]); // non-leaf node traversal heavy son for (int p = head [u]; ~ P; P = edge [p]. Next) {If (edge [p]. V! = Son [u] & edge [p]. V! = Fa [u]) DFS _ (edge [p]. v, edge [p]. v); // light edge} int find (int u, int v) {int Fu = top [u], FV = top [v], TMP =-1; while (Fu! = FV) {If (DEP [Fu] <Dep [FV]) {swap (Fu, Fv); swap (u, v);} TMP = max (TMP, query (Pos [Fu], POS [u]); U = Fa [Fu]; fu = top [u];} If (u = V) return TMP; if (DEP [u]> Dep [v]) Swap (u, v); // u v is different, but return max (TMP, query (Pos [son [u], POS [v]);} void Init () {memset (Head,-1, sizeof (head )); CNT = 0 ;}int main () {int CAS; scanf ("% d", & CAS); While (CAS --) {int N; scanf ("% d ", & N); Init (); idx = Dep [1] = 0; Fa [1] =-1; for (int I = 1; I <n; ++ I) {int A, B, C; scanf ("% d", & A, & B, & C); addedge (A, B, C); addedge (B, A, C);} DFS (1); DFS _ (1, 1 ); initt (idx); // printf ("idx = % d \ n", idx, m); For (INT u = 1; U <= N; ++ U) {for (int p = head [u]; ~ P; P = edge [p]. next) {const int v = edge [p]. v; If (DEP [v]> Dep [u]) Update (Pos [v], edge [p]. w) ;}} char op [20]; for (scanf ("% s", & OP); OP [0]! = 'D'; scanf ("% s", & OP) {int A, B; scanf ("% d", & A, & B ); if (OP [0] = 'q') printf ("% d \ n", find (A, B); else {--; int V1 = edge [A <1]. v, V2 = edge [A <1 | 1]. v; If (DEP [V1] <Dep [V2]) V1 = V2; Update (Pos [V1], B) ;}} return 0 ;} /* 231 2 1002 3 200 query 1 2 Change 1 3 query 1 2done41 2 12 4 23 4 3 query 1 3q 1 2 Change 1 3 query 3 4C 1 3q 1 2done */

BUPT 649 Gao on a tree II

Path partitioning of point Weights

# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # include <cmath> # include <map> using namespace STD; const int maxn = 200000 + 123; // int T [maxn <2]; // Map <int, int> MP [maxn <2]; multimap <int, int> mm; struct edge {int V, next, W;} edge [maxn <1]; int head [maxn], CNT; void addedge (int u, int V, int W = 0) {edge [CNT]. V = V; edge [CNT]. W = W; edge [CNT]. next = head [u]; head [u] = CNT ++;} int Dep [maxn], Pos [maxn], Fa [maxn], top [maxn], son [maxn], siz [maxn]; int idx; /// POS the position of Father edge int seg-tree // The Tree chain is split into chains, void DFS (int u) {siz [u] = 1; son [u] =-1; for (int p = head [u]; ~ P; P = edge [p]. next) {const Int & V = edge [p]. v; If (V = Fa [u]) continue; Fa [v] = u; Dep [v] = Dep [u] + 1; DFS (v ); if (siz [v]> siz [son [u]) Son [u] = V; siz [u] + = siz [v];} void DFS _ (int u, int TP) {pos [u] = ++ idx; top [u] = TP; If (~ Son [u]) DFS _ (son [u], top [u]); // non-leaf node traversal heavy son for (int p = head [u]; ~ P; P = edge [p]. Next) {If (edge [p]. V! = Son [u] & edge [p]. V! = Fa [u]) DFS _ (edge [p]. v, edge [p]. v); // light edge} bool find (int u, int V, int X) {int Fu = top [u], FV = top [v]; multimap <int, int >:: iterator ITU = mm. upper_bound (x), it; multimap <int, int >:: iterator ITL = mm. lower_bound (x); int L, R; If (ITL = mm. end () | ITL-> first! = X) return false; while (Fu! = FV) {If (DEP [Fu] <Dep [FV]) {swap (Fu, Fv); swap (u, v );} /// TMP = max (TMP, query (Pos [Fu], POS [u]); // If (query (Pos [Fu], POS [u]) return true; // If (Pos [Fu] <= L & Pos [u]) // printf ("% d \ n ", pos [Fu], POS [u]); For (IT = ITL; it! = ITU; ++ it) if (Pos [Fu] <= it-> Second & Pos [u]> = it-> second) return true; U = Fa [Fu]; fu = top [u]; // printf ("F % d \ n", Fu, Fv );} if (u = V) {for (IT = ITL; it! = ITU; ++ it) if (Pos [u] = It-> second) return true;} If (DEP [u]> Dep [v]) Swap (u, v); // u v, but on the same heavy chain // return max (TMP, query (Pos [son [u], POS [v]); /// printf ("P % d p % d \ n", POS [u], POS [v]); For (IT = ITL; it! = ITU; ++ it) if (Pos [u] <= it-> Second & Pos [v]> = it-> second) return true; return false ;} void Init () {memset (Head,-1, sizeof (head); CNT = 0;} void initr (int rt) {idx = 0; dep [RT] = 0; Fa [RT] = 0;} int W [maxn]; int main () {int CAS; scanf ("% d", & CAS ); while (CAS --) {int n, m; scanf ("% d", & N, & M); Init (); initr (1 ); for (INT I = 1; I <= N; ++ I) {scanf ("% d", W + I) ;}for (INT I = 1; I <n; ++ I) {int A, B; scanf ("% d", & A, & B); addedge (, B); addedge (B, A);} DFS (1); DFS _ (1, 1); // initt (idx); mm. clear (); For (INT I = 1; I <= N; ++ I) // Update (Pos [I], W [I]); mm. insert (make_pair (W [I], POS [I]); // For (INT I = 0; I <= N; ++ I) printf ("% d \ n", I, POS [I]); For (INT I = 0; I <m; ++ I) {char op [5]; scanf ("% s", OP); If (OP [0] = 'q') {int A, B, C; scanf ("% d", & A, & B, & C); If (find (a, B, c) puts ("find "); else puts ("notfind");} else {int A, B; scanf ("% d", & A, & B); Multimap <int, int >:: iterator ITU = mm. upper_bound (W [a]), it; multimap <int, int >:: iterator ITL = mm. lower_bound (W [a]); For (IT = ITL; it! = ITU; ++ it) {If (IT-> second = POS [a]) {mm. erase (it); break ;}} mm. insert (make_pair (B, POS [a]); W [a] = B ;}} puts ("");}}/**/

Related Article

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.