From 94f67584c8161cb6eeaa30976f61ce430c561847 Mon Sep 17 00:00:00 2001 From: zscoder Date: Fri, 7 Oct 2016 16:35:26 +0800 Subject: [PATCH] Added Tree Class --- Data Structures Class Template.cpp | 277 ++++++++++++++++++++++++++++- 1 file changed, 276 insertions(+), 1 deletion(-) diff --git a/Data Structures Class Template.cpp b/Data Structures Class Template.cpp index d322a24..1c37b0b 100644 --- a/Data Structures Class Template.cpp +++ b/Data Structures Class Template.cpp @@ -1083,6 +1083,11 @@ struct Graph adj[v].pb(tmp); } + void reset() + { + adj.clear(); + } + vi dist; vi par; @@ -1256,14 +1261,284 @@ struct Graph //End Graph //Tree +struct Tree +{ + struct data + { + ll w; + }; + + struct node + { + int p; //parent + ll w; //modify for different problems + }; + + struct edge + { + int v; data dat; + }; + + vector > adj; + int n; + + Tree(int _n) + { + adj.resize(_n); + n = _n; + } + + vi level; + vi depth; + vi h; + vi euler; + vi firstocc; + vector rmqtable; + vi subsize; + vi start; vi en; + vector > st; + + void addedge(int u, int v) + { + edge tmp; tmp.v = v; + adj[u].pb(tmp); + tmp.v = u; + adj[v].pb(tmp); + } + + void reset(int _n) + { + adj.clear(); + level.clear(); + depth.clear(); + euler.clear(); + rmqtable.clear(); + subsize.clear(); + start.clear(); + en.clear(); + st.clear(); + firstocc.clear(); + adj.resize(_n); + n = _n; + } + + void dfssub(int u, int p) + { + subsize[u] = 1; + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; + if(v == p) continue; + dfssub(v, u); + subsize[v] += subsize[u]; + } + } + + void calcsub() + { + subsize.resize(n); + dfssub(0, -1); + } + + int timer; + + void dfsstartend(int u, int p) + { + start[u] = ++timer; + if(p == -1) h[u] = 0; + else h[u] = h[p] + 1; + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; + dfsstartend(v, u); + } + en[u] = ++timer; + } + + void calcstartend() + { + timer = 0; + start.resize(n); en.resize(n); h.resize(n); + dfsstartend(0, -1); + } + + int eulercnt; + + void dfseuler(int u, int p) + { + euler[eulercnt] = u; eulercnt++; + if(p == -1) {depth[u] = 0;} + else {depth[u] = depth[p] + 1;} + firstocc[u] = eulercnt-1; + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; + if(v == p) continue ; + dfseuler(v, u); + euler[eulercnt] = u; eulercnt++; + } + } + + void calceuler() + { + eulercnt = 0; + level.assign(2*n, 0); + euler.assign(2*n, 0); + depth.assign(n, 0); + firstocc.resize(n); + dfseuler(0, -1); + } + void filllevel() + { + int LG = 0; + while((1< firstocc[v]) swap(u, v); + //cout << firstocc[u] << ' ' << firstocc[v] << ' ' << rmq(firstocc[u], firstocc[v]) << ' ' << euler[rmq(firstocc[u], firstocc[v])] << endl; + return euler[rmq(firstocc[u], firstocc[v])]; + } + + bool insub(int u, int v) //is u in the subtree of v? + { + if(start[v] <= start[u] && en[u] <= en[v]) return true; + return false; + } + + void dfspar(int u, int p) + { + //cerr << u << ' ' << p << '\n'; + st[0][u].p = p; + if(p == -1) h[u] = 0; + else h[u] = h[p] + 1; + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; + if(v == p) continue; + dfspar(v, u); + } + } + + int LOG; + + void calcpar() + { + h.resize(n); + int LG = 0; LOG = 0; + while((1<= 0; i--) + { + if(k&(1< h[v]) swap(u, v); + for(int i = LOG - 1; i >= 0; i--) + { + if(st[i][v].p != -1 && h[st[i][v].p] >= h[u]) + { + v = st[i][v].p; + } + } + if(u == v) return u; + for(int i = LOG - 1; i >= 0; i--) + { + if(st[i][v].p != -1 && st[i][v].p != st[i][u].p) + { + u = st[i][u].p; + v = st[i][v].p; + } + } + return st[0][u].p; + } + + int distance(int u, int v) + { + int lc = lca(u, v); + return (h[u]+h[v]-2*h[lc]); + } +}; //End Tree /* TO-DO LIST : 1. SQRT DECOMP (MO) 2. SQRT DECOMP (REAL) 3. TREE (LCA, HLD) -12. OTHER STRING STRUCTS SUCH AS PALINDROMIC TREE +12. OTHER STRING STRUCTS SUCH AS PALINDROMIC TREE, MANACHAR, Z? 14. FFT 15. Karatsuba 16. Other Flow Algo