From 3faf1951da75a99c26ecb9e5315b05a7e2c1e08c Mon Sep 17 00:00:00 2001 From: zscoder Date: Fri, 7 Oct 2016 15:26:17 +0800 Subject: [PATCH] Update Data Structures Class Template.cpp --- Data Structures Class Template.cpp | 236 ++++++++++++++++++++++++++++- 1 file changed, 229 insertions(+), 7 deletions(-) diff --git a/Data Structures Class Template.cpp b/Data Structures Class Template.cpp index 44a4c7f..d322a24 100644 --- a/Data Structures Class Template.cpp +++ b/Data Structures Class Template.cpp @@ -270,22 +270,33 @@ struct update { //Segment Tree end //DSU start -struct node2 -{ - int p; ll sum; -}; - struct DSU { int S; - vector dsu; + + struct node + { + int p; ll sum; + }; + vector dsu; DSU(int n) { S = n; for(int i = 0; i < n; i++) { - node2 tmp; + node tmp; + tmp.p = i; tmp.sum = 0; + dsu.pb(tmp); + } + } + + void reset(int n) + { + S = n; + for(int i = 0; i < n; i++) + { + node tmp; tmp.p = i; tmp.sum = 0; dsu.pb(tmp); } @@ -623,6 +634,19 @@ template struct Matrix return R; } + Matrix operator + (const Matrix &A) const + { + Matrix R = Matrix(); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + R.a[i][j] = a[i][j] + A.a[i][j]; + } + } + return R; + } + Matrix binpow(Matrix A, ll p) { if(p == 0) return Matrix(); @@ -1034,6 +1058,203 @@ struct ConvexHull }; //End Convex Hull Trick +//Graph (Basic Algos) +struct Graph +{ + struct edge + { + int v; ll weight; + }; + vector > adj; + int n; + + Graph(int _n) + { + adj.resize(_n); + n = _n; + } + + void addedge(int u, int v, ll c) + { + edge tmp; + tmp.v = v; tmp.weight = c; + adj[u].pb(tmp); + tmp.v = u; + adj[v].pb(tmp); + } + + vi dist; + vi par; + + void bfs(int s) + { + ll INFI = ll(1e18); + dist.assign(n, INFI); + par.assign(n, -1); + dist[s] = 0; par[s] = -1; + queue q; q.push(s); + while(!q.empty()) + { + int u = q.front(); q.pop(); + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; + if(dist[v] >= INFI) + { + dist[v] = dist[u] + 1; + par[v] = u; + q.push(v); + } + } + } + } + + void bfs01(int s) + { + ll INFI = ll(1e18); + dist.assign(n, INFI); + par.assign(n, -1); + dist[s] = 0; par[s] = -1; + deque q; q.pb(s); + while(!q.empty()) + { + int u = q.front(); q.pop_front(); + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; ll w = adj[u][i].weight; + if(dist[v] >= INFI) + { + if(w == 1) + { + dist[v] = dist[u] + 1; + par[v] = u; + q.push_back(v); + } + else + { + dist[v] = dist[u]; + par[v] = u; + q.push_front(v); + } + } + } + } + } + + void dijkstra(int s) + { + ll INFI = ll(1e18); + dist.assign(n, INFI); + par.assign(n, -1); + dist[s] = 0; par[s] = -1; + priority_queue, greater > pq; + pq.push(ii(0, s)); + while(!pq.empty()) + { + int u = pq.top().se; ll d = pq.top().fi; pq.pop(); + for(int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].v; ll w = adj[u][i].weight; + if(d + w < dist[v]) + { + dist[v] = d + w; + par[v] = u; + pq.push(ii(dist[v], v)); + } + } + } + } + + vector > d; + + void Floyd() + { + ll INFIN = ll(1e18); + d.resize(n); + for(int i = 0; i < n; i++) + { + d[i].assign(n, INFIN); + } + for(int i = 0; i < n; i++) + { + for(int j = 0; j < adj[i].size(); j++) + { + d[i][adj[i][j].v] = adj[i][j].weight; + } + d[i][i] = 0; + } + for(int k = 0; k < n; k++) + { + for(int i = 0; i < n; i++) + { + for(int j = 0; j < n; j++) + { + d[i][j] = min(d[i][j], d[i][k] + d[k][j]); + } + } + } + } + + bool BellmanFord(int s) //returns true if negative weight cycle exists + { + ll INFI = ll(1e18); + dist.assign(n, INFI); + par.assign(n, -1); + dist[s] = 0; + for(int step = 1; step <= n; step++) + { + for(int i = 0; i < n; i++) + { + for(int j = 0; j < adj[i].size(); j++) + { + int u = i; int v = adj[i][j].v; ll w = adj[i][j].weight; + if(dist[v] > dist[u] + w) + { + if(step == n) + { + return true; + } + dist[v] = dist[u] + w; + } + } + } + } + return false; + } + + ll shortest(int s, int e) //returns the distance by Dijkstra + { + return dist[e]; + } + + vector > edges; + + ll Kruskal() + { + DSU dsu(n); + for(int i = 0; i < n; i++) + { + for(int j = 0; j < adj[i].size(); j++) + { + int u = i; int v = adj[i][j].v; ll w = adj[i][j].weight; + edges.pb(mp(w, mp(u, v))); + } + } + sort(edges.begin(), edges.end()); + ll ans = 0; int cnt = 0; + for(int i = 0; i < edges.size(); i++) + { + int u = edges[i].se.fi; int v = edges[i].se.se; + if(dsu.sameset(u, v)) continue; + dsu.merge(u, v); + cnt++; ans += edges[i].fi; + if(cnt >= n - 1) break; + } + return ans; + } +}; +//End Graph + //Tree //End Tree @@ -1049,6 +1270,7 @@ struct ConvexHull 17. KMP 18. Trie 19. Suffix Tree (?) +20. Treap */ int main() //Testing Zone {