This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int N = 2e5 + 5; | |
int arr[N]; | |
void initialze(int n){ | |
for (int i = 0; i < n; i++) arr[i] = -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
/* | |
Using DFS to find a cycle in directed graph: | |
============================================ | |
- DFS can be used to detect a cycle in a directed graph. DFS for a connected graph produces a tree. There is a cycle in a graph only | |
if there is a back edge present in the graph. A back edge is an edge that is from a node to itself (self-loop) or one of its | |
ancestor in the tree produced by DFS. | |
- Simple visited approach will not work here: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
/* Using DFS to find a cycle in undirected graph */ | |
int N; | |
vector<vector<int>> gph; | |
vector<bool> vis(N, false); | |
bool dfs(int src, int par, vector<bool>& vis){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int n; | |
vector<vector<int>> graph; | |
vector<int> euler_walk, in, out; | |
void dfs(int u, int &i, vector<bool> &vis){ | |
vis[u] = true; | |
euler_walk[i++] = u; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int N = 2e5 + 5; // Max num of vertices | |
const int maxHeight = 20 + 1; // ceil(log2(N)) = 20 for N = 5e5+5 | |
int depth[N], parent[N], dp[N][maxHeight]; | |
vector<int> gph[N]; | |
int n; // Num of vertices (1-indexing) | |
void dfs (int u, int par, int dep) { // Calculate parent and depth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int N; | |
vector<vector<int>> graph; | |
vector<int> bfs(){ | |
vector<int> indeg(N, 0); | |
vector<int> ans; | |
for (auto i : graph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define pp pair<int, pair<int, int>> | |
const int N = 2e5 + 5; | |
int arr[N]; | |
int _find(int key); | |
void _union(int a, int b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
/* Works for Connected and undirected graph */ | |
// https://www.geeksforgeeks.org/prims-algorithm-using-priority_queue-stl/ | |
#define pp pair<int, int> | |
int N, MX = INT32_MAX; | |
vector<vector<pair<int,int>>> graph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<vector<int>> adj; // adjacency list representation | |
int n; // number of nodes | |
int src; // source vertex | |
queue<int> q; | |
vector<bool> vis(n); | |
vector<int> dist(n), par(n); | |
q.push(src); | |
vis[src] = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
public: | |
vector<Node*> children; | |
bool isLeaf; | |
Node() { | |
children.assign(26, NULL); | |
isLeaf = false; | |
} | |
}; |
OlderNewer