mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge branch 'fixgraph_fixlint' into fixgraph
This commit is contained in:
commit
edf7b4db6e
@ -91,7 +91,7 @@ std::vector<int> beadth_first_search(const std::vector<std::vector<int>> &adj,
|
||||
std::vector<int> result;
|
||||
|
||||
/// vector to keep track of visited vertices
|
||||
std::vector<bool> visited(vertices, 0);
|
||||
std::vector<bool> visited(vertices, false);
|
||||
|
||||
std::queue<int> tracker;
|
||||
/// marking the start vertex as visited
|
||||
@ -172,7 +172,7 @@ int main() {
|
||||
/// running predefined test cases
|
||||
tests();
|
||||
|
||||
size_t vertices, edges;
|
||||
size_t vertices = 0, edges = 0;
|
||||
std::cout << "Enter the number of vertices : ";
|
||||
std::cin >> vertices;
|
||||
std::cout << "Enter the number of edges : ";
|
||||
@ -185,7 +185,7 @@ int main() {
|
||||
std::cout << "Enter vertices in pair which have edges between them : "
|
||||
<< std::endl;
|
||||
while (edges--) {
|
||||
int u, v;
|
||||
int u = 0, v = 0;
|
||||
std::cin >> u >> v;
|
||||
graph::addEdge(&adj, u, v);
|
||||
}
|
||||
@ -193,4 +193,4 @@ int main() {
|
||||
/// running Breadth First Search Algorithm on the graph
|
||||
graph::beadth_first_search(adj, 0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ using std::vector;
|
||||
class Solution {
|
||||
vector<vector<int>> graph;
|
||||
vector<int> in_time, out_time;
|
||||
int timer;
|
||||
int timer = 0;
|
||||
vector<vector<int>> bridge;
|
||||
vector<bool> visited;
|
||||
void dfs(int current_node, int parent) {
|
||||
@ -51,7 +51,7 @@ class Solution {
|
||||
return bridge;
|
||||
}
|
||||
};
|
||||
int main(void) {
|
||||
int main() {
|
||||
Solution s1;
|
||||
int number_of_node = 5;
|
||||
vector<vector<int>> node;
|
||||
|
@ -1,39 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <cstdlib>
|
||||
using std::vector;
|
||||
using std::pair;
|
||||
|
||||
void explore(int i, vector<vector<int>> &adj, int *state)
|
||||
void explore(int i, const vector<vector<int>> &adj, std::vector<int> *state)
|
||||
{
|
||||
state[i] = 1;
|
||||
for(auto it2 : adj[i])
|
||||
(*state)[i] = 1;
|
||||
for (const auto it2 : adj[i])
|
||||
{
|
||||
if (state[it2] == 0)
|
||||
if ((*state)[it2] == 0)
|
||||
{
|
||||
explore(it2, adj,state);
|
||||
explore(it2, adj, state);
|
||||
}
|
||||
if (state[it2] == 1)
|
||||
if ((*state)[it2] == 1)
|
||||
{
|
||||
std::cout<<"1";
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
state[i] = 2;
|
||||
(*state)[i] = 2;
|
||||
};
|
||||
int acyclic(vector<vector<int> > &adj,size_t n) {
|
||||
int acyclic(const vector<vector<int> > &adj, size_t n) {
|
||||
//write your code here
|
||||
|
||||
int state[n]; // permitted states are 0 1 and 2
|
||||
// permitted states are 0 1 and 2
|
||||
std::vector<int> state(n, 0);
|
||||
|
||||
// mark the states of all vertices initially to 0
|
||||
for(int i=0;i<n;i++)
|
||||
for (size_t i = 0; i < n; i++)
|
||||
{
|
||||
state[i] = 0;
|
||||
}
|
||||
|
||||
for(auto it1 = 0; it1 != adj.size(); it1++)
|
||||
for (size_t it1 = 0; it1 != adj.size(); it1++)
|
||||
{
|
||||
if (state[it1] == 0)
|
||||
explore(it1,adj,state);
|
||||
{
|
||||
explore(it1, adj, &state);
|
||||
}
|
||||
if (state[it1] == 1)
|
||||
{
|
||||
std::cout<<"1";
|
||||
@ -45,11 +50,11 @@ int acyclic(vector<vector<int> > &adj,size_t n) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
size_t n, m;
|
||||
size_t n = 0, m = 0;
|
||||
std::cin >> n >> m;
|
||||
vector<vector<int> > adj(n, vector<int>());
|
||||
for (size_t i = 0; i < m; i++) {
|
||||
int x, y;
|
||||
int x = 0, y = 0;
|
||||
std::cin >> x >> y;
|
||||
adj[x - 1].push_back(y - 1);
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ void depth_first_search(const std::vector<std::vector<size_t>> &adj,
|
||||
|
||||
/** Main function */
|
||||
int main() {
|
||||
size_t vertices, edges;
|
||||
size_t vertices = 0, edges = 0;
|
||||
std::cout << "Enter the Vertices : ";
|
||||
std::cin >> vertices;
|
||||
std::cout << "Enter the Edges : ";
|
||||
@ -120,7 +120,7 @@ int main() {
|
||||
std::cout << "Enter the vertices which have edges between them : "
|
||||
<< std::endl;
|
||||
while (edges--) {
|
||||
size_t u, v;
|
||||
size_t u = 0, v = 0;
|
||||
std::cin >> u >> v;
|
||||
graph::addEdge(&adj, u, v);
|
||||
}
|
||||
@ -130,4 +130,4 @@ int main() {
|
||||
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,17 @@
|
||||
* @param V : vertices
|
||||
* @return void
|
||||
**/
|
||||
void print(std::vector<int> a[], int V) {
|
||||
void print(const std::vector< std::vector<int> > &a, int V) {
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (!a[i].empty())
|
||||
if (!a[i].empty()) {
|
||||
std::cout << "i=" << i << "-->";
|
||||
for (int j = 0; j < a[i].size(); j++) std::cout << a[i][j] << " ";
|
||||
if (!a[i].empty())
|
||||
}
|
||||
for (int j : a[i]) {
|
||||
std::cout << j << " ";
|
||||
}
|
||||
if (!a[i].empty()) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,32 +31,34 @@ void print(std::vector<int> a[], int V) {
|
||||
* //Recursive function/method to push vertices into stack passed as parameter:
|
||||
* @param v : vertices
|
||||
* @param &st : stack passed by reference
|
||||
* @param vis[] : array to keep track of visited nodes (boolean type)
|
||||
* @param &vis : array to keep track of visited nodes (boolean type)
|
||||
* @param adj[] : array of vectors to represent graph
|
||||
* @return void
|
||||
**/
|
||||
void push_vertex(int v, std::stack<int> &st, bool vis[], std::vector<int> adj[]) {
|
||||
vis[v] = true;
|
||||
void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis, const std::vector< std::vector<int> > &adj) {
|
||||
(*vis)[v] = true;
|
||||
for (auto i = adj[v].begin(); i != adj[v].end(); i++) {
|
||||
if (vis[*i] == false)
|
||||
if ((*vis)[*i] == false) {
|
||||
push_vertex(*i, st, vis, adj);
|
||||
}
|
||||
}
|
||||
st.push(v);
|
||||
st->push(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* //Recursive function/method to implement depth first traversal(dfs):
|
||||
* @param v : vertices
|
||||
* @param vis[] : array to keep track of visited nodes (boolean type)
|
||||
* @param vis : array to keep track of visited nodes (boolean type)
|
||||
* @param grev[] : graph with reversed edges
|
||||
* @return void
|
||||
**/
|
||||
void dfs(int v, bool vis[], std::vector<int> grev[]) {
|
||||
vis[v] = true;
|
||||
void dfs(int v, std::vector<bool> *vis, const std::vector< std::vector<int> > &grev) {
|
||||
(*vis)[v] = true;
|
||||
// cout<<v<<" ";
|
||||
for (auto i = grev[v].begin(); i != grev[v].end(); i++) {
|
||||
if (vis[*i] == false)
|
||||
if ((*vis)[*i] == false) {
|
||||
dfs(*i, vis, grev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,15 +72,16 @@ no SCCs i.e. none(0) or there will be x no. of SCCs (x>0)) i.e. it returns the
|
||||
count of (number of) strongly connected components (SCCs) in the graph.
|
||||
(variable 'count_scc' within function)
|
||||
**/
|
||||
int kosaraju(int V, std::vector<int> adj[]) {
|
||||
bool vis[V] = {};
|
||||
int kosaraju(int V, const std::vector< std::vector<int> > &adj) {
|
||||
std::vector<bool> vis(V, false);
|
||||
std::stack<int> st;
|
||||
for (int v = 0; v < V; v++) {
|
||||
if (vis[v] == false)
|
||||
push_vertex(v, st, vis, adj);
|
||||
if (vis[v] == false) {
|
||||
push_vertex(v, &st, &vis, adj);
|
||||
}
|
||||
}
|
||||
// making new graph (grev) with reverse edges as in adj[]:
|
||||
std::vector<int> grev[V];
|
||||
std::vector< std::vector<int> > grev(V);
|
||||
for (int i = 0; i < V + 1; i++) {
|
||||
for (auto j = adj[i].begin(); j != adj[i].end(); j++) {
|
||||
grev[*j].push_back(i);
|
||||
@ -89,7 +96,7 @@ int kosaraju(int V, std::vector<int> adj[]) {
|
||||
int t = st.top();
|
||||
st.pop();
|
||||
if (vis[t] == false) {
|
||||
dfs(t, vis, grev);
|
||||
dfs(t, &vis, grev);
|
||||
count_scc++;
|
||||
}
|
||||
}
|
||||
@ -101,13 +108,13 @@ int kosaraju(int V, std::vector<int> adj[]) {
|
||||
// All critical/corner cases have been taken care of.
|
||||
// Input your required values: (not hardcoded)
|
||||
int main() {
|
||||
int t;
|
||||
int t = 0;
|
||||
std::cin >> t;
|
||||
while (t--) {
|
||||
int a, b; // a->number of nodes, b->directed edges.
|
||||
int a = 0, b = 0; // a->number of nodes, b->directed edges.
|
||||
std::cin >> a >> b;
|
||||
int m, n;
|
||||
std::vector<int> adj[a + 1];
|
||||
int m = 0, n = 0;
|
||||
std::vector< std::vector<int> > adj(a + 1);
|
||||
for (int i = 0; i < b; i++) // take total b inputs of 2 vertices each
|
||||
// required to form an edge.
|
||||
{
|
||||
|
@ -1,75 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
//#include <boost/multiprecision/cpp_int.hpp>
|
||||
// using namespace boost::multiprecision;
|
||||
const int mx = 1e6 + 5;
|
||||
const long int inf = 2e9;
|
||||
typedef long long ll;
|
||||
#define rep(i, n) for (i = 0; i < n; i++)
|
||||
#define repp(i, a, b) for (i = a; i <= b; i++)
|
||||
#define pii pair<int, int>
|
||||
#define vpii vector<pii>
|
||||
#define vi vector<int>
|
||||
#define vll vector<ll>
|
||||
#define r(x) scanf("%d", &x)
|
||||
#define rs(s) scanf("%s", s)
|
||||
#define gc getchar_unlocked
|
||||
#define pc putchar_unlocked
|
||||
#define mp make_pair
|
||||
#define pb push_back
|
||||
#define lb lower_bound
|
||||
#define ub upper_bound
|
||||
#define endl "\n"
|
||||
#define fast \
|
||||
ios_base::sync_with_stdio(false); \
|
||||
cin.tie(NULL); \
|
||||
cout.tie(NULL);
|
||||
using namespace std;
|
||||
void in(int &x) {
|
||||
register int c = gc();
|
||||
x = 0;
|
||||
int neg = 0;
|
||||
for (; ((c < 48 || c > 57) && c != '-'); c = gc())
|
||||
;
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = gc();
|
||||
}
|
||||
for (; c > 47 && c < 58; c = gc()) {
|
||||
x = (x << 1) + (x << 3) + c - 48;
|
||||
}
|
||||
if (neg)
|
||||
x = -x;
|
||||
}
|
||||
void out(int n) {
|
||||
int N = n, rev, count = 0;
|
||||
rev = N;
|
||||
if (N == 0) {
|
||||
pc('0');
|
||||
return;
|
||||
}
|
||||
while ((rev % 10) == 0) {
|
||||
count++;
|
||||
rev /= 10;
|
||||
}
|
||||
rev = 0;
|
||||
while (N != 0) {
|
||||
rev = (rev << 3) + (rev << 1) + N % 10;
|
||||
N /= 10;
|
||||
}
|
||||
while (rev != 0) {
|
||||
pc(rev % 10 + '0');
|
||||
rev /= 10;
|
||||
}
|
||||
while (count--) pc('0');
|
||||
}
|
||||
ll parent[mx], arr[mx], node, edge;
|
||||
vector<pair<ll, pair<ll, ll>>> v;
|
||||
typedef int64_t ll;
|
||||
|
||||
std::array<ll, mx> parent, arr;
|
||||
ll node, edge;
|
||||
std::vector<std::pair<ll, std::pair<ll, ll>>> v;
|
||||
void initial() {
|
||||
int i;
|
||||
rep(i, node + edge) parent[i] = i;
|
||||
for (int i = 0; i < node + edge; ++i) {
|
||||
parent[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
int root(int i) {
|
||||
while (parent[i] != i) {
|
||||
parent[i] = parent[parent[i]];
|
||||
@ -77,16 +23,18 @@ int root(int i) {
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
void join(int x, int y) {
|
||||
int root_x = root(x); // Disjoint set union by rank
|
||||
int root_y = root(y);
|
||||
parent[root_x] = root_y;
|
||||
}
|
||||
|
||||
ll kruskal() {
|
||||
ll mincost = 0, i, x, y;
|
||||
rep(i, edge) {
|
||||
x = v[i].second.first;
|
||||
y = v[i].second.second;
|
||||
ll mincost = 0;
|
||||
for (int i = 0; i < edge; ++i) {
|
||||
ll x = v[i].second.first;
|
||||
ll y = v[i].second.second;
|
||||
if (root(x) != root(y)) {
|
||||
mincost += v[i].first;
|
||||
join(x, y);
|
||||
@ -94,23 +42,22 @@ ll kruskal() {
|
||||
}
|
||||
return mincost;
|
||||
}
|
||||
|
||||
int main() {
|
||||
fast;
|
||||
while (1) {
|
||||
int i, j, from, to, cost, totalcost = 0;
|
||||
cin >> node >> edge; // Enter the nodes and edges
|
||||
if (node == 0 && edge == 0)
|
||||
while (true) {
|
||||
int from = 0, to = 0, cost = 0, totalcost = 0;
|
||||
std::cin >> node >> edge; // Enter the nodes and edges
|
||||
if (node == 0 && edge == 0) {
|
||||
break; // Enter 0 0 to break out
|
||||
}
|
||||
initial(); // Initialise the parent array
|
||||
rep(i, edge) {
|
||||
cin >> from >> to >> cost;
|
||||
v.pb(mp(cost, mp(from, to)));
|
||||
for (int i = 0; i < edge; ++i) {
|
||||
std::cin >> from >> to >> cost;
|
||||
v.emplace_back(make_pair(cost, std::make_pair(from, to)));
|
||||
totalcost += cost;
|
||||
}
|
||||
sort(v.begin(), v.end());
|
||||
// rep(i,v.size())
|
||||
// cout<<v[i].first<<" ";
|
||||
cout << kruskal() << endl;
|
||||
std::cout << kruskal() << std::endl;
|
||||
v.clear();
|
||||
}
|
||||
return 0;
|
||||
|
@ -7,8 +7,8 @@
|
||||
// Find the lowest common ancestor using binary lifting in O(nlogn)
|
||||
// Zero based indexing
|
||||
// Resource : https://cp-algorithms.com/graph/lca_binary_lifting.html
|
||||
const int N = 1005;
|
||||
const int LG = log2(N) + 1;
|
||||
constexpr int N = 1005;
|
||||
constexpr int LG = 20;
|
||||
struct lca {
|
||||
int n;
|
||||
std::vector<int> adj[N]; // Graph
|
||||
|
@ -10,8 +10,9 @@ std::vector<int> ans;
|
||||
void dfs(int v) {
|
||||
visited[v] = true;
|
||||
for (int u : G[v]) {
|
||||
if (!visited[u])
|
||||
if (!visited[u]) {
|
||||
dfs(u);
|
||||
}
|
||||
}
|
||||
ans.push_back(v);
|
||||
}
|
||||
@ -20,15 +21,16 @@ void topological_sort() {
|
||||
visited.assign(n, false);
|
||||
ans.clear();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!visited[i])
|
||||
if (!visited[i]) {
|
||||
dfs(i);
|
||||
}
|
||||
}
|
||||
reverse(ans.begin(), ans.end());
|
||||
}
|
||||
int main() {
|
||||
std::cout << "Enter the number of vertices and the number of directed edges\n";
|
||||
std::cin >> n >> m;
|
||||
int x, y;
|
||||
int x = 0, y = 0;
|
||||
G.resize(n, std::vector<int>());
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::cin >> x >> y;
|
||||
|
Loading…
Reference in New Issue
Block a user