From ca1e614f103847f9d54e20635172d21e3352213c Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 5 Oct 2023 01:40:11 +0530 Subject: [PATCH 1/3] lca added --- c++/lca.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 c++/lca.cpp diff --git a/c++/lca.cpp b/c++/lca.cpp new file mode 100644 index 0000000..c402bc2 --- /dev/null +++ b/c++/lca.cpp @@ -0,0 +1,61 @@ +vector> g(N), up(N, vector (100, -1)); +vector lvl(N); + +void dfs(ll node, ll par, ll level){ + lvl[node] = level; + + for(auto it: g[node]){ + if(it != par){ + dfs(it, node, level + 1); + } + } +} + + +void binary_lift(ll src, ll par){ + up[src][0] = par; + for(ll i = 1; i < 20; i++){ + if(up[src][i-1] != -1){ + up[src][i] = up[up[src][i-1]][i-1]; + } + else up[src][i] = -1; + } + + for(auto it: g[src]){ + if(it != par){ + binary_lift(it, src); + } + } +} + +ll lift_node(ll node, ll jump_req){ + + for(ll i = 19; i>=0; i--){ + if(node == -1 or jump_req == 0){ + break; + } + if(jump_req >= (1<=0; i--){ + if(up[u][i] != up[v][i]){ + u = up[u][i]; v = up[v][i]; + } + } + return lift_node(u,1); +} \ No newline at end of file From 7fbaec6a67737242cf89132f1bd2c02a2bb4ceaa Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 5 Oct 2023 14:36:42 +0530 Subject: [PATCH 2/3] Added Segment Tree Library --- c++/segment_tree.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 c++/segment_tree.cpp diff --git a/c++/segment_tree.cpp b/c++/segment_tree.cpp new file mode 100644 index 0000000..eb6a3a3 --- /dev/null +++ b/c++/segment_tree.cpp @@ -0,0 +1,116 @@ +#include // ॐ +#include // Har Har Mahadev +#include + +using namespace std; +using namespace __gnu_pbds; + +template +using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; + + +#define rep(i, zz, n) for (int i = zz; i < (n); ++i) +#define all(x) x.begin(), x.end() +#define pb push_back +#define F first +#define S second +#define ll long long +#define ld long double +#define pll pair +#define PI 3.14159265358979323846 +#define el '\n' +#define sp " " +#define vl vector +#define dbg(x) cout << #x << ": " << x << " "; + +const ll mod = 1e9 + 7; +const ll inf = 1e18; +const ll N = 1e6 + 5; + +#define debug(x) cout << #x << " "; _print(x); cout << '\n'; +#define _debug(a,b) cout << #a << " [ "; array_debug(a,b); cout << "]\n"; +template void array_debug(T *a, V n){ rep(i,0,n) cout << a[i] << ' ';} +template void _print(T t) {cout << t;} +template void _print(pair p) {cout << "{"; _print(p.ff); cout << ","; _print(p.ss); cout << "}";} +template void _print(vector v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(set v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(multiset v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(map v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";} + +//greedy/brute force/DP/Graph/binary search/? + +template +struct SegmentTree{ + vector st; + void assign(vector &v1) { + int n = (v1.size()); + st = vector(4 * n + 1); + build(v1, 1, 0, n - 1); + } + T combine(T x, T y) { + // T z; + // return z; + return { x.sum + y.sum }; // check which opeartion is to be performed + } + void build(vector& v1, int v, int tl, int tr) { + if (tl == tr) { + st[v].sum = v1[tl]; // check + } + else{ + int mid = (tl + tr) / 2; + build(v1, 2 * v, tl, mid); + build(v1, 2 * v + 1, mid + 1, tr); + st[v] = combine(st[2 * v], st[2 * v + 1]); + } + } + // tl, tr : the current segment/range whose node/vertex is v + // l, r : the segment/range on which the 'range query' is to be performed + T query(int v, int tl, int tr, int l, int r) { + if (tr < l || r < tl) { + return { 0 }; // check which opeartion is to be performed + } + else if (tl == l && tr == r) { + return st[v]; + } + int mid = (tl + tr) / 2; + return combine(query(2 * v, tl, mid, l, min(r, mid)), query(2 * v + 1, mid + 1, tr, max(l, mid + 1), r)); + } + void update_one(int v, int tl, int tr, int pos, int newVal) { + if (tl == tr) { + st[v].sum = newVal; + // check whether the newVal is added or updated + return; + } + int mid = (tl + tr) / 2; + if (pos <= mid) { + update_one(2 * v, tl, mid, pos, newVal); + } + else{ + update_one(2 * v + 1, mid + 1, tr, pos, newVal); + } + st[v] = combine(st[2 * v], st[2 * v + 1]); + } +}; +struct Node{ + int sum; +}; +SegmentTree seg; + + +void __roOTs(){ + // main code here + +} +signed main(){ + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + int tt = 1; cin >> tt; + for (int i = 1; i <= tt; i++){ + // cout << "Case #" << i << ": "; + __roOTs(); cout << '\n'; + + } + return 0; +} \ No newline at end of file From 0a3e1175cb3349aafae1268df96a26a22cbc7a21 Mon Sep 17 00:00:00 2001 From: Aditya Date: Thu, 12 Oct 2023 16:35:52 +0530 Subject: [PATCH 3/3] Added Centroid Decomposition Library --- c++/centroid_decomposition.cpp | 105 +++++++++++++++++++++++++++++ c++/lca.cpp | 61 ----------------- c++/segment_tree.cpp | 116 --------------------------------- 3 files changed, 105 insertions(+), 177 deletions(-) create mode 100644 c++/centroid_decomposition.cpp delete mode 100644 c++/lca.cpp delete mode 100644 c++/segment_tree.cpp diff --git a/c++/centroid_decomposition.cpp b/c++/centroid_decomposition.cpp new file mode 100644 index 0000000..074d3a8 --- /dev/null +++ b/c++/centroid_decomposition.cpp @@ -0,0 +1,105 @@ +#include +using namespace std; + +#include +#include +using namespace __gnu_pbds; +template using ordered_set = tree, rb_tree_tag,tree_order_statistics_node_update>; +// orderded_set -> find_by_order(x); order_of_key(x) + +typedef long long ll; +typedef long double lld; +typedef pair pii; +typedef pair pll; +#define loop(n) for(ll i = 0; i < (n); ++i) +#define rep(i,zz,n) for(ll i = zz; i < (n); ++i) +#define repi(i,zz,n) for(ll i = zz; i >= (n); --i) +#define all(v) v.begin(),v.end() +#define pb push_back +#define ff first +#define ss second +#define sz(x) (int)x.size() +#define nline cout << '\n' + +#define debug(x) cout << #x << " "; _print(x); cout << '\n'; +#define _debug(a,b) cout << #a << " [ "; array_debug(a,b); cout << "]\n"; +template void array_debug(T *a, V n){ rep(i,0,n) cout << a[i] << ' ';} +template void _print(T t) {cout << t;} +template void _print(pair p) {cout << "{"; _print(p.ff); cout << ","; _print(p.ss); cout << "}";} +template void _print(vector v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(set v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(multiset v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(map v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";} + +const lld eps = 1e-9; +const int mod = 1e9 + 7; +const ll inf = 1e18; +const int N1 = 2e5 + 10; + +// vector> adj; + +template +struct CentroidDecomposition { + vector subtr, par_cd; + vector del; + + void assign(int n) { + subtr = vector(n); + par_cd = vector(n); + del = vector(n); + + decompose_cd(0, 0); + } + + int get_subtree_size(int i, int j) { + subtr[i] = 1; + for (int x : adj[i]) { + if (x == j || del[x]) continue; + subtr[i] += get_subtree_size(x, i); + } + return subtr[i]; + } + + int find_centroid(int i, int j, int n) { + for (int x : adj[i]) { + if (x != j && !del[x] && subtr[x] * 2 > n) { + return find_centroid(x, i, n); + } + } + return i; + } + + void decompose_cd(int i, int j) { + int n = get_subtree_size(i, j); + int ct = find_centroid(i, j, n); + par_cd[ct] = j; // parent of centrid-node in centroid-tree + + del[ct] = 1; + for (int x : adj[ct]) { + if (del[x]) continue; + decompose_cd(x, ct); + } + } +}; +CentroidDecomposition cd; + +int code() { + + // enter main code + + return 0; +} + +int main() { + ios::sync_with_stdio(0); cin.tie(0); + // freopen("input.txt","r",stdin); + // freopen("output.txt","w",stdout); + + int tc = 1; + cin >> tc; + + loop(tc) code(); + // rep(i, 1, tc + 1) cout << "Case #" << i << ": ", code(); + + return 0; +} \ No newline at end of file diff --git a/c++/lca.cpp b/c++/lca.cpp deleted file mode 100644 index c402bc2..0000000 --- a/c++/lca.cpp +++ /dev/null @@ -1,61 +0,0 @@ -vector> g(N), up(N, vector (100, -1)); -vector lvl(N); - -void dfs(ll node, ll par, ll level){ - lvl[node] = level; - - for(auto it: g[node]){ - if(it != par){ - dfs(it, node, level + 1); - } - } -} - - -void binary_lift(ll src, ll par){ - up[src][0] = par; - for(ll i = 1; i < 20; i++){ - if(up[src][i-1] != -1){ - up[src][i] = up[up[src][i-1]][i-1]; - } - else up[src][i] = -1; - } - - for(auto it: g[src]){ - if(it != par){ - binary_lift(it, src); - } - } -} - -ll lift_node(ll node, ll jump_req){ - - for(ll i = 19; i>=0; i--){ - if(node == -1 or jump_req == 0){ - break; - } - if(jump_req >= (1<=0; i--){ - if(up[u][i] != up[v][i]){ - u = up[u][i]; v = up[v][i]; - } - } - return lift_node(u,1); -} \ No newline at end of file diff --git a/c++/segment_tree.cpp b/c++/segment_tree.cpp deleted file mode 100644 index eb6a3a3..0000000 --- a/c++/segment_tree.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include // ॐ -#include // Har Har Mahadev -#include - -using namespace std; -using namespace __gnu_pbds; - -template -using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; - - -#define rep(i, zz, n) for (int i = zz; i < (n); ++i) -#define all(x) x.begin(), x.end() -#define pb push_back -#define F first -#define S second -#define ll long long -#define ld long double -#define pll pair -#define PI 3.14159265358979323846 -#define el '\n' -#define sp " " -#define vl vector -#define dbg(x) cout << #x << ": " << x << " "; - -const ll mod = 1e9 + 7; -const ll inf = 1e18; -const ll N = 1e6 + 5; - -#define debug(x) cout << #x << " "; _print(x); cout << '\n'; -#define _debug(a,b) cout << #a << " [ "; array_debug(a,b); cout << "]\n"; -template void array_debug(T *a, V n){ rep(i,0,n) cout << a[i] << ' ';} -template void _print(T t) {cout << t;} -template void _print(pair p) {cout << "{"; _print(p.ff); cout << ","; _print(p.ss); cout << "}";} -template void _print(vector v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} -template void _print(set v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} -template void _print(multiset v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} -template void _print(map v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";} - -//greedy/brute force/DP/Graph/binary search/? - -template -struct SegmentTree{ - vector st; - void assign(vector &v1) { - int n = (v1.size()); - st = vector(4 * n + 1); - build(v1, 1, 0, n - 1); - } - T combine(T x, T y) { - // T z; - // return z; - return { x.sum + y.sum }; // check which opeartion is to be performed - } - void build(vector& v1, int v, int tl, int tr) { - if (tl == tr) { - st[v].sum = v1[tl]; // check - } - else{ - int mid = (tl + tr) / 2; - build(v1, 2 * v, tl, mid); - build(v1, 2 * v + 1, mid + 1, tr); - st[v] = combine(st[2 * v], st[2 * v + 1]); - } - } - // tl, tr : the current segment/range whose node/vertex is v - // l, r : the segment/range on which the 'range query' is to be performed - T query(int v, int tl, int tr, int l, int r) { - if (tr < l || r < tl) { - return { 0 }; // check which opeartion is to be performed - } - else if (tl == l && tr == r) { - return st[v]; - } - int mid = (tl + tr) / 2; - return combine(query(2 * v, tl, mid, l, min(r, mid)), query(2 * v + 1, mid + 1, tr, max(l, mid + 1), r)); - } - void update_one(int v, int tl, int tr, int pos, int newVal) { - if (tl == tr) { - st[v].sum = newVal; - // check whether the newVal is added or updated - return; - } - int mid = (tl + tr) / 2; - if (pos <= mid) { - update_one(2 * v, tl, mid, pos, newVal); - } - else{ - update_one(2 * v + 1, mid + 1, tr, pos, newVal); - } - st[v] = combine(st[2 * v], st[2 * v + 1]); - } -}; -struct Node{ - int sum; -}; -SegmentTree seg; - - -void __roOTs(){ - // main code here - -} -signed main(){ - ios::sync_with_stdio(false); - cin.tie(0); - cout.tie(0); - - int tt = 1; cin >> tt; - for (int i = 1; i <= tt; i++){ - // cout << "Case #" << i << ": "; - __roOTs(); cout << '\n'; - - } - return 0; -} \ No newline at end of file