Skip to content

Commit

Permalink
Update Data Structures Class Template.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
zscoder authored Oct 7, 2016
1 parent d620db1 commit 3faf195
Showing 1 changed file with 229 additions and 7 deletions.
236 changes: 229 additions & 7 deletions Data Structures Class Template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,33 @@ struct update {
//Segment Tree end

//DSU start
struct node2
{
int p; ll sum;
};

struct DSU
{
int S;
vector<node2> dsu;

struct node
{
int p; ll sum;
};
vector<node> 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);
}
Expand Down Expand Up @@ -623,6 +634,19 @@ template<int N> 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();
Expand Down Expand Up @@ -1034,6 +1058,203 @@ struct ConvexHull
};
//End Convex Hull Trick

//Graph (Basic Algos)
struct Graph
{
struct edge
{
int v; ll weight;
};
vector<vector<edge> > 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<int> 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<int> 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<ii, vector<ii>, greater<ii> > 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<vector<ll> > 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<pair<ll, ii> > 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
Expand All @@ -1049,6 +1270,7 @@ struct ConvexHull
17. KMP
18. Trie
19. Suffix Tree (?)
20. Treap
*/
int main() //Testing Zone
{
Expand Down

0 comments on commit 3faf195

Please sign in to comment.