Skip to content

Instantly share code, notes, and snippets.

@NamPE286
NamPE286 / longest_path_DAG.cpp
Created May 17, 2023 09:40
Longest path in DAG
// https://cses.fi/problemset/task/1680
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void dfs(vector<vector<ll>> &graph, vector<bool> &visited, vector<ll> &topsort, ll u) {
if (visited[u]) return;
visited[u] = true;
for (ll i : graph[u]) {
@NamPE286
NamPE286 / connectedEdgeVertexCnt.cpp
Last active May 19, 2023 03:11
Find number of edge and vertex of connected component in an undirected graph
// https://atcoder.jp/contests/abc292/tasks/abc292_d
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void dfs(vector<vector<ll>> &graph, set<ll> &visited, ll &edgeCnt, ll u){
if(visited.count(u)) return;
visited.insert(u);
edgeCnt += graph[u].size();
@NamPE286
NamPE286 / findArtsAndBridge.cpp
Last active May 20, 2023 09:39
Find all articulation points and bridges in a graph
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void dfs(vector<vector<ll>> &graph,
vector<pair<ll, ll>> &bridges,
vector<bool> &isArt,
vector<bool> &visited,
vector<ll> &ids,
@NamPE286
NamPE286 / CSES1130.cpp
Last active May 25, 2023 06:19
CSES 1130 - DP on Tree solution
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// dp[i][0] = max matching if we don't take edge (i, i_children)
// dp[i][1] = max matching if we take edge (i, i_children)
ll dp[(size_t)2e5 + 1][2];
void dfs(vector<vector<ll>> &tree, ll root, ll parent){
@NamPE286
NamPE286 / segtree_it_index.cpp
Created May 29, 2023 06:43
Segment tree iterative with index
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
class segment_tree {
private:
vector<pair<T, T>> tree; // 0-indexed
T n;
@NamPE286
NamPE286 / powMod.cpp
Last active September 17, 2023 13:51
Modular binary exponentation
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll powMod(ll a, ll b, ll m) {
if (b == 0) {
return 1;
}
@NamPE286
NamPE286 / invMod.cpp
Last active July 1, 2023 13:23
Inverse modulo in C++ (a^-1 % m (m is prime)) (fermat's little theorem)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll powMod(ll a, ll b, ll m) {
ll res = 1;
while (b) {
if (b % 2) res = (res * a) % m;
a = (a * a) % m;
@NamPE286
NamPE286 / dfsTreeCnt.cpp
Last active November 16, 2023 12:16
Count bridge and articulation point in graph with DFS tree
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class dfsTree {
private:
vector<vector<ll>> tree;
vector<ll> nums, tail, low, parent;
ll time = 1;
@NamPE286
NamPE286 / pointerSegTree.cpp
Last active October 31, 2024 00:33
Segment tree implementation with pointer in C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class SegmentTree {
private:
struct Node {
Node* _left;
Node* _right;
@NamPE286
NamPE286 / pointerSegTreeLazy.cpp
Last active October 31, 2024 00:34
Segment tree with lazy propagation implementation with pointer in C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class SegmentTree {
private:
struct Node {
Node* _left;
Node* _right;