Output of the HDU 5044 tree interval operation/LCA + DFS

Source: Internet
Author: User

A tree. Two operations: 1. Add VI to all vertices in the path, 2. Add VI to all edges.

First offline to find all the questions of the LCA, and then traverse the query once, point + VI, lca-2 * VI, finally DFS from the leaves sweep up once, and finally the ancestor points to make up. Input mounting is used. Complexity (n + M ).

# Pragma comment (linker, "/Stack: 102410000000000,10240000000000") # include <iostream> # include <cstdio> # include <cstdlib> using namespace STD; const int maxv = 100005, maxE = 200015; int head [maxv]; int nume = 0; long e [MaxE] [3]; void inline Adde (int I, Int J) // edge {e [nume] [0] = J; E [nume] [1] = head [I]; head [I] = nume; E [nume ++] [2] = 0; E [nume] [0] = I; E [nume] [1] = head [J]; head [J] = nume; E [nume ++] [2] = 0;} int headq [maxv]; int numq = 0; L Ong Long Q [MaxE] [5]; void inline addq (int x, int y, long Vals, int flag) // ask {q [numq] [0] = y; Q [numq] [1] = headq [X]; headq [x] = numq; Q [numq] [2] =-1; Q [numq] [3] = Vals; Q [numq ++] [4] = flag; Q [numq] [0] = x; Q [numq] [1] = headq [y]; headq [y] = numq; Q [numq] [2] =-1; Q [numq] [3] = Vals; Q [numq ++] [4] = flag;} int Fa [maxv]; int find (int x) {If (X! = Fa [x]) return Fa [x] = find (Fa [x]); Return Fa [X];} int ve [maxv]; int n, m; int vis [maxv]; int vis2 [maxv]; long Val [maxv]; int ise [MaxE]; void Tarjan (int u, int father) // offline find the LCA {for (Int J = head [u]; J! =-1; j = E [J] [1]) {int v = E [J] [0]; If (! Vis [v] & V! = Father) {ve [v] = J; ise [J] = 1; Tarjan (v, U); Fa [v] = u ;}} vis [u] = 1; for (Int J = headq [u]; J! =-1; j = Q [J] [1]) {If (vis [Q [J] [0]) {q [J ^ 1] [2] = Q [J] [2] = find (Q [J] [0]) ;}} void DFS (int u) // obtain all vertices and edges {for (Int J = head [u]; J! =-1; j = E [J] [1]) {int v = E [J] [0]; If (! Vis2 [v]) {vis2 [v] = 1; DFS (V); Val [u] + = Val [v]; E [ve [u] [2] + = E [ve [v] [2] ;}} int vis_e [MaxE]; long anster [maxv]; void Init () {nume = numq = 0; For (INT I = 0; I <maxv; I ++) {head [I] = headq [I] =-1; fa [I] = I; Val [I] = vis2 [I] = vis [I] = 0; ve [I] = 2 * N; anster [I] = 0 ;}for (INT I = 0; I <MaxE; I ++) {ise [I] = 0; vis_e [I] = 0 ;}} template <class T> // enter the inline bool scan (T & RET) {char C; int SGN; If (C = getchar (), c = EOF) return 0; // EO Fwhile (C! = '-' & (C <'0' | C> '9') c = getchar (); SGN = (C = '-')? -1:1; ret = (C = '-')? 0 :( C-'0'); While (C = getchar (), c> = '0' & C <= '9 ') ret = RET * 10 + (c-'0'); RET * = SGN; return 1;}/* inline void prints (long x) {If (x> 9) prints (X/10); putchar (X % 10 + '0');} */INT main () {int t; int CNT = 1; scan (t ); // scanf ("% d", & T); While (t --) {// scanf ("% d", & N, & M ); scan (n); scan (m); Init (); int AA, BB, CC; For (INT I = 0; I <n-1; I ++) {// scanf ("% d", & aa, & bb); scan (AA); scan (bb); Adde (AA, BB) ;}int TX; for (INT I = 0; I <m; I + +) {Getchar (); // scanf ("add"); scan (TX); scan (AA ); scan (bb); scan (CC); // scanf ("% d", & Tx, & aa, & BB, & CC ); if (Tx = 1) addq (AA, BB, CC, 1); else addq (AA, BB, CC, 0);} Tarjan (n/2 + 1, -1); For (INT I = 1; I <= N; I ++) for (Int J = headq [I]; J! =-1; j = Q [J] [1]) {If (! Vis_e [J]) {vis_e [J] = vis_e [J ^ 1] = 1; if (Q [J] [4]) {Val [I] + = Q [J] [3]; Val [Q [J] [0] + = Q [J] [3]; val [Q [J] [2]-= Q [J] [3] * 2; anster [Q [J] [2] + = Q [J] [3];} else {e [ve [I] [2] + = Q [J] [3]; E [ve [Q [J] [0] [2] + = Q [J] [3]; E [ve [Q [J] [2] [2]-= 2 * Q [J] [3] ;}} vis2 [n/2 + 1] = 1; DFS (n/2 + 1); printf ("case # % d: \ n", CNT ++ ); for (INT I = 1; I <n; I ++) printf ("% i64d", Val [I] + anster [I]); printf ("% i64d \ n", Val [N] + anster [N]); For (Int J = 0; j <nume; j ++) if (ISE [J]) {If (j = nume-2 | j = nume-1) printf ("% i64d", E [J] [2]); else printf ("% i64d", E [J] [2]);} puts ("") ;}return 0 ;}


Output of the HDU 5044 tree interval operation/LCA + DFS

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.