├── .gitignore ├── .vscode └── settings.json ├── Data_Structure ├── Disjoined_Set_Union.cpp ├── Fenwick_Tree.cpp ├── Fenwick_Tree2D.cpp ├── Inversion_Fenwick_Tree.cpp ├── Kth_Inversion.cpp ├── Lazy_Segment_Tree.cpp ├── Persistent_Segment_Tree.cpp ├── Segment_Tree.cpp └── queue_fifo.cpp ├── Divide_and_Conquer ├── Closest_Pair.cpp ├── Inversion_Merge_Sort.cpp ├── Mod_of_Power.cpp └── multiply_two_polynomials.cpp ├── Dynamic_Programming ├── Catalan_Number.cpp ├── Edit_Distance.cpp ├── Knapsack.cpp ├── Longest_Common_Subsequence.cpp ├── Longest_Common_Substring.cpp ├── Longest_Increasing_Subsequence.cpp ├── Longest_Palindromic_Substring.cpp ├── Matrix_Chain_Multiplication.cpp ├── Maximum_Subarray_Sum_Kadane.cpp ├── Pallindrome_Partitioning.cpp ├── Pascal_Triangle.cpp └── Weighted_Interval_Scheduling.cpp ├── General_Knowledge ├── Extended_Euclidean.cpp ├── Fermat_Little_Theorem.cpp └── Square_Root.cpp ├── Geometry └── Convex_Hull_Graham_Scan.cpp ├── Graph_and_Tree ├── Articulation_Points_and_Bridges.cpp ├── Bipartite_Check.cpp ├── Breadth_First_Search.cpp ├── Centroid_Decomposition.cpp ├── Depth_First_Search.cpp ├── Kahn_Algorithm.cpp ├── LCA_Binary_Lifting.cpp ├── Topological_Sort.cpp └── Tree_Traversal.cpp ├── Greedy ├── Job_sequencing.cpp └── Minimum_Coin_Change.cpp ├── MST ├── Kruskal.cpp ├── Prim.cpp └── Second_MST.cpp ├── Network_Flow └── Maximum_Flow_Ford_Fulkerson.cpp ├── Optimization_Technique ├── 2D_Range_Minimum_Query.cpp ├── MO's_alogorithm.cpp ├── Range_Minimum_Query.cpp └── Square_Root_Decomposition.cpp ├── README.md ├── Searching ├── Exponential_Search.cpp ├── Fibonacci_search.cpp └── jump_search.cpp ├── Shortest_Path ├── Bellman_Ford.cpp ├── Dijkstra.cpp └── Floyd_Warshall.cpp ├── Sorting ├── Bubble_sort.cpp ├── Counting_sort.cpp ├── Insertion_sort.cpp ├── Merge_Sort.cpp ├── Optimised_Bubble_Sort.cpp ├── Quick_Sort.cpp ├── Radix_sort.cpp ├── Random_Quick_Sort.cpp ├── Selection_sort.cpp └── heap_sort.cpp ├── String_Matching_Algorithm ├── KMP.cpp └── Rabin_Karp.cpp ├── Strongly_Connected_Component ├── Kosaraju.cpp └── Tarjan.cpp ├── Template.cpp ├── _config.yml └── index.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.app 2 | *.bat 3 | *.cgi 4 | *.com 5 | *.exe 6 | *.gadget 7 | *.jar 8 | *.pif 9 | *.vb 10 | *.wsf 11 | *.doc 12 | *.docx 13 | *.log 14 | *.msg 15 | *.pages 16 | *.rtf 17 | *.txt 18 | *.wpd 19 | *.wps 20 | *.in 21 | *.out 22 | *.rar 23 | *.zip 24 | *.o 25 | .vscode/* 26 | # !.vscode/settings.json 27 | # !.vscode/tasks.json 28 | # !.vscode/launch.json 29 | # !.vscode/extensions.json 30 | .history 31 | Thumbs.db 32 | Thumbs.db:encryptable 33 | ehthumbs.db 34 | ehthumbs_vista.db 35 | *.stackdump 36 | [Dd]esktop.ini 37 | $RECYCLE.BIN/ 38 | *.cab 39 | *.msi 40 | *.msix 41 | *.msm 42 | *.msp 43 | *.lnk 44 | *.pdf 45 | *.sol 46 | *.inp 47 | *.oac 48 | *.out 49 | *.txt 50 | Gen_case.cpp 51 | Gencase.cpp 52 | Sol.cpp 53 | Teach.cpp 54 | test.cpp 55 | *.tex 56 | _* -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.default.compilerPath": "C:\\MinGW\\bin\\g++.exe" 3 | } -------------------------------------------------------------------------------- /Data_Structure/Disjoined_Set_Union.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Disjoined Set Union 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language : C++ 6 | Created : 01 Oct 2021 [13:07] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 10010; 19 | 20 | int parent[N]; 21 | 22 | int find_root(int u){ 23 | if(u == parent[u]){ 24 | return u; 25 | } 26 | return parent[u] = find_root(parent[u]); 27 | } 28 | 29 | void merge(int u, int v){ 30 | int ru = find_root(ru), rv = find_root(rv); 31 | parent[ru] = rv; // change parent of U to parent of V 32 | return ; 33 | } 34 | 35 | // Must be called once 36 | void init(){ 37 | for(int i=1; i<=N; ++i){ 38 | parent[i] = i; 39 | } 40 | return ; 41 | } 42 | -------------------------------------------------------------------------------- /Data_Structure/Fenwick_Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Fenwick_Tree 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/ 7 | Visualgo : https://visualgo.net/en/fenwicktree 8 | Created : 03 May 2021 [12:04] 9 | */ 10 | #include 11 | #define all(x) begin(x),end(x) 12 | #define sz(x) (int)(x).size() 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100000; 21 | LL tree[N+10]; 22 | void upd(int idx,int v){ 23 | for(;idx<=N;idx+=idx&=idx) 24 | tree[idx]+=v; 25 | } 26 | LL read(int idx,LL sum=0){ 27 | for(;idx;idx-=idx&-idx) 28 | sum+=tree[idx]; 29 | return sum; 30 | } -------------------------------------------------------------------------------- /Data_Structure/Fenwick_Tree2D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Fenwick_Tree 2D 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language : C++ 6 | Created : 01 Oct 2021 [08:51] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 10010; 19 | LL tree[N][N]; 20 | void upd(int idxi, int idxj,int v){ 21 | for(; idxi<=N-10; idxi+=idxi&-idxi){ 22 | int now = idxj; 23 | for(; now<=N-10; now+=now&-now){ 24 | tree[idxi][now] += v; 25 | } 26 | } 27 | } 28 | LL read(int idxi, int idxj, LL sum=0){ 29 | for(;idxi;idxi-=idxi&-idxi){ 30 | int now = idxj; 31 | for(; now; now-=now&-now){ 32 | sum += tree[idxi][now]; 33 | } 34 | } 35 | return sum; 36 | } 37 | -------------------------------------------------------------------------------- /Data_Structure/Inversion_Fenwick_Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Inversion_Fenwick_Tree 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [20:06] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 100000; 19 | int tree[N+10]; 20 | map mapp; 21 | void upd(int idx){ 22 | for(;idx<=N;idx+=idx&-idx) 23 | tree[idx]++; 24 | } 25 | int read(int idx,int sum=0){ 26 | for(;idx;idx-=idx&-idx) 27 | sum+=tree[idx]; 28 | return sum; 29 | } 30 | int inversion_count(vector a){ 31 | vector b = a; 32 | sort(all(b)),b.resize(unique(all(b))-b.begin()); // compress number and remove duplicate numbers 33 | for(int i=0;i a; 45 | cin >> n; 46 | a.resize(n); 47 | for(int i=0;i> a[i]; 49 | cout << inversion_count(a); 50 | } -------------------------------------------------------------------------------- /Data_Structure/Kth_Inversion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Kth_Inversion 3 | Author : Phumipat C. [MAGCARI] 4 | Language : C++ 5 | Explanation : 6 | Created : 01 October 2021 [10:03] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; //N is number of element 20 | int tree[N]; 21 | 22 | void upd(int idx,int v){ 23 | for(;idx0;idx-=idx&-idx) 29 | sum+=tree[idx]; 30 | return sum; 31 | } 32 | 33 | vector comp_number(vector a){ 34 | vector b = a; 35 | sort(all(b)),b.resize(unique(all(b))-b.begin()); 36 | for(auto &x:a) 37 | x = lower_bound(all(b),x)-b.begin(); 38 | return a; 39 | } 40 | 41 | pair kth_inversion(vector a,LL k){ 42 | vector b = comp_number(a); 43 | for(auto x:b) 44 | upd(x,1); 45 | for(int i=0;inum){ 48 | k-=num; 49 | continue; 50 | } 51 | int idx = i+1; 52 | while(k){ 53 | if(a[idx] 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100000; 20 | LL tree[N*4],lazy[N+10]; 21 | void upd_range(int l,int r,int now,int ll,int rr,int v){ 22 | if(rrr) return ; 23 | if(ll<=l && r<=rr){ 24 | tree[now]+=v; 25 | if(l!=r) lazy[now*2]+=v,lazy[now*2+1]+=v; 26 | return ; 27 | } 28 | int mid = (l+r)/2; 29 | upd_range(l,mid,now*2,ll,rr,v),upd_range(mid+1,r,now*2+1,ll,rr,v); 30 | tree[now] = tree[now*2] + tree[now*2+1]; 31 | } 32 | LL read_point(int l,int r,int now,int idx){ 33 | if(lazy[now]){ 34 | tree[now]+=lazy[now]; 35 | if(l!=r) lazy[now*2]+=lazy[now],lazy[now*2+1]+=lazy[now]; 36 | lazy[now] = 0; 37 | } 38 | if(l == r) return tree[now]; 39 | int mid = (l+r)/2; 40 | if(idx<=mid) return read_point(l,mid,now*2,idx); 41 | else return read_point(mid+1,r,now*2+1,idx); 42 | } 43 | LL read_range(int l,int r,int now,int ll,int rr){ 44 | if(rrr) return 0; 45 | if(lazy[now]){ 46 | tree[now]+=lazy[now]; 47 | if(l!=r) lazy[now*2]+=lazy[now],lazy[now*2+1]+=lazy[now]; 48 | lazy[now] = 0; 49 | } 50 | if(ll<=l && r<=rr) return tree[now]; 51 | int mid = (l+r)/2; 52 | return read_range(l,mid,now*2,ll,rr) + read_range(mid+1,r,now*2+1,ll,rr); 53 | } 54 | // Function Calling 55 | // upd_range(1,N,1,ll,rr,v); 56 | // read_point(1,N,1,idx); 57 | // read_range(1,N,1,ll,rr); -------------------------------------------------------------------------------- /Data_Structure/Persistent_Segment_Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : _Persistent_Segment_Tree 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/persistent-segment-tree-set-1-introduction/ 7 | Created : 07 May 2021 [22:11] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | struct A{ 22 | int v; 23 | A *l,*r; 24 | }; 25 | A tree[4*N]; 26 | A* ver[N]; 27 | int id; 28 | A* build(int l,int r){ 29 | int now = ++id; 30 | if(l == r) return &tree[now]; 31 | int mid = (l+r)/2; 32 | tree[now] = {0,build(l,mid),build(mid+1,r)}; 33 | return &tree[now]; 34 | } 35 | A* upd(int l,int r,A* origin,int idx,int v){ 36 | int now = ++id; 37 | tree[now] = *origin; 38 | if(l == r){ 39 | tree[now].v = v; 40 | return &tree[now]; 41 | } 42 | int mid = (l+r)/2; 43 | if(idx<=mid) tree[now].l = upd(l,mid,origin->l,idx,v); 44 | else tree[now].r = upd(mid+1,r,origin->r,idx,v); 45 | tree[now].v = tree[now].l->v + tree[now].r->v; 46 | return &tree[now]; 47 | } 48 | int read(int l,int r,A* lo,A* ro,int ll,int rr){ 49 | if(ll>r || rrv - lo->v; 51 | int mid = (l+r)/2; 52 | return read(l,mid,lo->l,ro->l,ll,rr) + read(mid+1,r,lo->r,ro->r,ll,rr); 53 | } 54 | int main(){ 55 | int n,num; 56 | cin >> n; 57 | ver[0] = build(1,n); 58 | for(int i=1;i<=n;i++){ 59 | cin >> num; 60 | ver[i] = upd(1,n,ver[i-1],num,1); 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Data_Structure/Segment_Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Segment_Tree 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/ 7 | visualgo : https://visualgo.net/en/segmenttree 8 | Created : 03 May 2021 [12:06] 9 | */ 10 | #include 11 | #define all(x) begin(x),end(x) 12 | #define sz(x) (int)(x).size() 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100000; 21 | LL tree[N*4]; 22 | int value[N]; 23 | void build(int l,int r,int now){ 24 | if(l == r){ 25 | tree[now] = value[l]; 26 | return ; 27 | } 28 | int mid = (l+r)/2; 29 | build(l,mid,now*2); 30 | build(mid+1,r,now*2+1); 31 | tree[now] = tree[now*2] + tree[now*2+1]; 32 | } 33 | void upd_point(int l,int r,int now,int idx,int v){ 34 | if(l == r){ 35 | tree[now]+=v; 36 | return ; 37 | } 38 | int mid = (l+r)/2; 39 | if(idx<=mid) upd_point(l,mid,now*2,idx,v); 40 | else upd_point(mid+1,r,now*2+1,idx,v); 41 | tree[now]=tree[now*2] + tree[now*2+1]; 42 | } 43 | LL read_point(int l,int r,int now,int idx){ 44 | if(l == r) return tree[now]; 45 | int mid = (l+r)/2; 46 | if(idx<=mid) return read_point(l,mid,now*2,idx); 47 | else return read_point(mid+1,r,now*2+1,idx); 48 | } 49 | LL read_range(int l,int r,int now,int ll,int rr){ 50 | if(rrr) return 0; 51 | if(ll<=l && r<=rr) return tree[now]; 52 | int mid = (l+r)/2; 53 | return read_range(l,mid,now*2,ll,rr) + read_range(mid+1,r,now*2+1,ll,rr); 54 | } 55 | // Function Calling 56 | // build(1,N,1); 57 | // upd_point(1,N,1,idx,v); 58 | // read_point(1,N,1,idx); 59 | // read_range(1,N,1,ll,rr); -------------------------------------------------------------------------------- /Data_Structure/queue_fifo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Queue 3 | Author : Lalit Chaudhary 4 | Language : C++ 5 | Explanation : Implement a queue using a linked list and perform 5 enqueue operations, one dequeue operation, anmd display the elements 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | struct node { 11 | int data; 12 | wwxstruct node *next; 13 | }; 14 | struct node* front = NULL; 15 | struct node* rear = NULL; 16 | struct node* temp; 17 | void Insert() { 18 | int val; 19 | cout<<"Insert the element in queue : "<>val; 21 | if (rear == NULL) { 22 | rear = (struct node *)malloc(sizeof(struct node)); 23 | rear->next = NULL; 24 | rear->data = val; 25 | front = rear; 26 | } else { 27 | temp=(struct node *)malloc(sizeof(struct node)); 28 | rear->next = temp; 29 | temp->data = val; 30 | temp->next = NULL; 31 | rear = temp; 32 | } 33 | } 34 | void Delete() { 35 | temp = front; 36 | if (front == NULL) { 37 | cout<<"Underflow"<next != NULL) { 42 | temp = temp->next; 43 | cout<<"Element deleted from queue is : "<data<data<data<<" "; 62 | temp = temp->next; 63 | } 64 | cout<>ch; 75 | switch (ch) { 76 | case 1: Insert(); 77 | break; 78 | case 2: Delete(); 79 | break; 80 | case 3: Display(); 81 | break; 82 | case 4: cout<<"Exit"< 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | struct Point { 22 | double x,y; 23 | } p[N]; 24 | int n,tmp_p[N]; 25 | bool cmpx(Point a , Point b) { 26 | if (a.x < b.x) return true; 27 | else return false; 28 | } 29 | bool cmpy(int a, int b) { 30 | if(p[a].y < p[b].y) return true; 31 | else return false; 32 | } 33 | double dis(int i, int j) { 34 | return sqrt((p[i].x - p[j].x)*(p[i].x - p[j].x) + (p[i].y - p[j].y)*(p[i].y - p[j].y)); 35 | } 36 | double Closest_Pair(int l, int r) { 37 | if (l == r) return 1e18; 38 | if (l + 1 == r) return dis(l, r); 39 | 40 | int mid = (l + r)/2,k = 0; 41 | double mindis = min(Closest_Pair(l, mid),Closest_Pair(mid+1, r)); 42 | 43 | for (int i = l; i <= r; i++) { 44 | if (fabs(p[mid].x - p[i].x) <= mindis) 45 | tmp_p[k++] = i; 46 | } 47 | 48 | sort(tmp_p, tmp_p + k, cmpy); 49 | for (int i = 0; i < k; i++) { 50 | for (int j = i + 1; j < k && p[tmp_p[j]].y - p[tmp_p[i]].y < mindis;j++) { 51 | double now = dis(tmp_p[i], tmp_p[j]); 52 | if (mindis-now > 1e-9) mindis = now; 53 | } 54 | } 55 | return mindis; 56 | } 57 | int main(){ 58 | int n; 59 | cin >> n; 60 | for(int i=0;i> p[i].x >> p[i].y; 62 | sort(p,p+n,cmpx); 63 | cout << Closest_Pair(0,n-1); 64 | } -------------------------------------------------------------------------------- /Divide_and_Conquer/Inversion_Merge_Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Inversion_Merge_Sort 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/counting-inversions/ 7 | Created : 03 May 2021 [20:05] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | int a[N],b[N]; 21 | int cnt; 22 | // same code as Merge_Sort but "cnt" is used to count inversion pair 23 | void inversion_count(int l,int r){ 24 | if(l == r) return ; 25 | int mid = (l+r)/2; 26 | inversion_count(l,mid),inversion_count(mid+1,r); 27 | int i = l,j = mid+1,k=l; 28 | while(i<=mid && j<=r){ 29 | if(a[i]<=a[j]) b[k++] = a[i++]; 30 | else b[k++] = a[j++],cnt+=mid-i+1; 31 | } 32 | while(i<=mid) b[k++] = a[i++]; 33 | while(j<=r) b[k++] = a[j++]; 34 | for(int k=l;k<=r;k++) 35 | a[k] = b[k]; 36 | } -------------------------------------------------------------------------------- /Divide_and_Conquer/Mod_of_Power.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Mod_of_Power 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Explanation Link : https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/ 7 | Created : 03 May 2021 [15:15] 8 | */ 9 | #include 10 | #define MOD (LL )(1e9+7) 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | LL mod_of_power(LL a,LL b,LL c = MOD){ // pow(a,b)%c 16 | if(b == 0) return 1; 17 | if(b == 1) return a%c; 18 | LL now = mod_of_power(a,b/2,c); 19 | if(b&1) return (((now*now)%c)*(a%c))%c; 20 | else return (now*now)%c; 21 | } 22 | -------------------------------------------------------------------------------- /Divide_and_Conquer/multiply_two_polynomials.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phumipatc/Algorithm/f4fc74c40ffdec8af63f6abb3b9d27579e7e4563/Divide_and_Conquer/multiply_two_polynomials.cpp -------------------------------------------------------------------------------- /Dynamic_Programming/Catalan_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Catalan_Number 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [13:32] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 10010; 19 | int dp[N]; 20 | 21 | int catalan_topdown(int n){ 22 | if(dp[n] != -1){ 23 | return dp[n]; 24 | } 25 | if(n <= 1){ 26 | return dp[n] = 1; 27 | } 28 | int ret = 0; 29 | for(int i=0; i 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 1010; 20 | char a[N],b[N]; 21 | int dp[N][N]; 22 | 23 | int edit_distance(int n,int m){ 24 | for(int i=1;i<=n;i++){ 25 | for(int j=1;j<=m;j++){ 26 | if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1]; 27 | else dp[i][j] = min({dp[i][j-1],dp[i-1][j],dp[i-1][j-1]}) + 1; 28 | } 29 | } 30 | return dp[n][m]; 31 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Knapsack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Knapsack 3 | Author : Phumipat C. [MAGCARI] 4 | Language : C++ 5 | Explanation : 6 | Created : 22 August 2021 [11:15] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 1010; 20 | int dp[N]; //dp[i] means max value when weight = i 21 | pair items[N]; //first = value, second = weight 22 | 23 | int knapsack_no_repeated_item_allowed(int n,int max_weight){ 24 | fill(dp,dp+N,-1e9); 25 | dp[0] = 0; 26 | for(int i=1;i<=n;i++){ 27 | for(int j=max_weight;j>=items[i].second;j--){ 28 | if(dp[j-items[i].second] == -1e9) continue; 29 | dp[j] = max(dp[j],dp[j-items[i].second] + items[i].first); 30 | } 31 | } 32 | int maxx = -1e9; 33 | for(int i=0;i<=max_weight;i++) 34 | maxx = max(maxx,dp[i]); 35 | return maxx; 36 | } 37 | 38 | int knapsack_repeated_item_allowed(int n,int max_weight){ 39 | fill(dp,dp+N,-1e9); 40 | dp[0] = 0; 41 | for(int i=1;i<=n;i++){ 42 | for(int j=items[i].second;j<=max_weight;j++){ 43 | if(dp[j-items[i].second] == -1e9) continue; 44 | dp[j] = max(dp[j],dp[j-items[i].second] + items[i].first); 45 | } 46 | } 47 | int maxx = -1e9; 48 | for(int i=0;i<=max_weight;i++) 49 | maxx = max(maxx,dp[i]); 50 | return maxx; 51 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Longest_Common_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Longest_Common_Subsequence 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [12:47] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 1010; 19 | int dp[N][N]; 20 | char a[N],b[N]; 21 | int LCS(){ 22 | int n = strlen(a+1),m = strlen(b+1); 23 | for(int i=1;i<=n;i++){ 24 | for(int j=1;j<=m;j++){ 25 | if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1]+1; 26 | dp[i][j] = max({dp[i][j],dp[i-1][j],dp[i][j-1]}); 27 | } 28 | } 29 | return dp[n][m]; 30 | } 31 | int main(){ 32 | cin >> a+1 >> b+1; 33 | cout << LCS(); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Longest_Common_Substring.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Longest_Common_Substring 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [12:57] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 1010; 19 | int dp[N][N]; 20 | char a[N],b[N]; 21 | int LCS(){ 22 | int n = strlen(a+1),m = strlen(b+1); 23 | int maxx = 0; 24 | for(int i=1;i<=n;i++){ 25 | for(int j=1;j<=m;j++){ 26 | if(a[i] == b[j]) 27 | dp[i][j] = dp[i-1][j-1]+1; 28 | maxx = max(maxx,dp[i][j]); 29 | } 30 | } 31 | return maxx; 32 | } 33 | int main(){ 34 | cin >> a+1 >> b+1; 35 | cout << LCS(); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Longest_Increasing_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Longest_Increasing_Subsequence 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [12:43] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 100010; 19 | int dp[N],a[N]; 20 | int LIS(int n){ 21 | int maxx=0; 22 | for(int i=1;i<=n;i++){ 23 | int idx = lower_bound(dp,dp+maxx,a[i])-dp; 24 | if(idx == maxx) maxx++; 25 | dp[idx] = a[i]; 26 | } 27 | return maxx; 28 | } 29 | int main(){ 30 | int n; 31 | cin >> n; 32 | for(int i=1;i<=n;i++) 33 | cin >> a[i]; 34 | cout << LIS(n); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Longest_Palindromic_Substring.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Longest_palindromic_Substring 3 | Author : Chayanin K. [~Aphrodicez] 4 | School : RYW 5 | Lang : C++ 6 | Created : 21 Oct 2021 [18:36] 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | const int MaxN = 1e3 + 10; 13 | 14 | char s[MaxN]; 15 | bool palindrome[MaxN][MaxN]; 16 | 17 | char Longest_Palindrome[MaxN]; 18 | 19 | char* LPS() { 20 | int n = strlen(s + 1); 21 | int max_size = 1; 22 | int start = 1; 23 | for(int i = 1; i <= n; i++) 24 | palindrome[i][i] = true; 25 | for(int i = 1; i <= n - 1; i++) { 26 | palindrome[i][i + 1] = s[i] == s[i + 1]; 27 | if(palindrome[i][i + 1] && 2 > max_size) { 28 | max_size = 2; 29 | start = i; 30 | } 31 | } 32 | for(int sz = 3; sz <= n; sz++) { 33 | for(int l = 1; l + sz - 1 <= n; l++) { 34 | int r = l + sz - 1; 35 | palindrome[l][r] = palindrome[l + 1][r - 1] & (s[l] == s[r]); 36 | if(palindrome[l][r] && sz > max_size) { 37 | max_size = sz; 38 | start = l; 39 | } 40 | } 41 | } 42 | for(int i = 1, j = start; i <= max_size; i++, j++) { 43 | Longest_Palindrome[i] = s[j]; 44 | } 45 | return &Longest_Palindrome[1]; 46 | } 47 | 48 | int main() { 49 | ios_base :: sync_with_stdio(0); 50 | cin.tie(0); 51 | 52 | cin >> (s + 1); 53 | cout << LPS(); 54 | return 0; 55 | } 56 | 57 | /* 58 | banana 59 | abracadabra 60 | */ 61 | -------------------------------------------------------------------------------- /Dynamic_Programming/Matrix_Chain_Multiplication.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Matrix_Chain 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [13:32] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 1010; 19 | int dp[N][N]; 20 | //Top Down Approach 21 | int mcm_topdown(int l,int r){ 22 | if(l == r) return 0; 23 | if(dp[l][r]!=-1) return dp[l][r]; 24 | int ret = 1e9; 25 | for(int k=l;k 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 10010; 20 | int num[N]; 21 | int kadane(int n){ 22 | int sum = 0,maxx = 0; 23 | for(int i=1;i<=n;i++){ 24 | sum+=num[i]; 25 | maxx = max(maxx,sum); 26 | if(sum<0) sum = 0; 27 | } 28 | return maxx; 29 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Pallindrome_Partitioning.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Pallindrome Partitioning 3 | Author : Dharmik Govani [21Shadow10] 4 | Language : C++ 5 | Explanation : 6 | Created : 27 October 2021 [00:06] 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | 12 | class Solution{ 13 | public: 14 | 15 | 16 | bool isPallindrome(string str, int i, int j) { 17 | while(i<=j) { 18 | if(str[i]!=str[j]) 19 | return false ; 20 | 21 | i++ ; 22 | j-- ; 23 | } 24 | 25 | return true ; 26 | } 27 | 28 | int solve(int dp[501][501],string str, int i, int j) { 29 | if(i>=j) 30 | return 0; 31 | 32 | if(dp[i][j]!=-1) 33 | return dp[i][j]; 34 | 35 | if(isPallindrome(str,i,j)) 36 | return 0; 37 | 38 | int ans = INT_MAX ; 39 | 40 | for(int k=i;k>t; 70 | while(t--){ 71 | string str; 72 | cin>>str; 73 | 74 | Solution ob; 75 | cout< 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 1010; 20 | int nCr[N][N]; //nCr[i][j] means n = i, r = j [iCj] 21 | 22 | void init_triangle(int n){ 23 | for(int i=0;i<=n;i++){ 24 | nCr[i][0] = nCr[i][i] = 1; 25 | for(int j=1;j 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 100010; 19 | struct A{ 20 | int st,en; 21 | LL w; 22 | bool operator < (const A&o) const{ 23 | if(en!=o.en) return en 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | void extended_euclidean(int a,int b,int *x,int *y){ 21 | if(b==0){ 22 | *x=1; 23 | *y=0; 24 | return ; 25 | } 26 | int u,v; 27 | extended_euclidean(b,a%b,&u,&v); 28 | *x=v; 29 | *y=u-v*(a/b); 30 | } -------------------------------------------------------------------------------- /General_Knowledge/Fermat_Little_Theorem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Fermat's Little Theorem 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language : C++ 6 | Explanation : 7 | Created : 10 Aug 2021 [11:40] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | LL mod_of_power(LL a, LL b, LL c){ 21 | if(b == 1){ 22 | return a % c; 23 | } 24 | LL tmp = mod_of_power(a, b/2, c); 25 | if(b % 2 == 0){ 26 | return ((tmp % c) * (tmp % c)) %c; 27 | } 28 | return ((tmp % c) * (tmp % c) * (a % c)) % c; 29 | } 30 | 31 | // (a / b) mod c 32 | LL fermat_litte_theorem(LL a, LL b, LL c){ 33 | LL inverse_b = mod_of_power(b, c-2, c); 34 | LL ret = (inverse_b * (a % c))%c; 35 | return ret; 36 | } -------------------------------------------------------------------------------- /General_Knowledge/Square_Root.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Approximate Square Root 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language : C++ 6 | Explanation : 7 | Created : 10 Aug 2021 [23:33] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | LL square_root(LL n){ 21 | LL l, r; 22 | l = 1, r = n; 23 | while(l <= r){ 24 | LL mid = (l + r) / 2; 25 | if(mid * mid == n){ 26 | return mid; 27 | } 28 | if(mid <= n / mid){ 29 | l = mid + 1; 30 | } 31 | else{ 32 | r = mid - 1; 33 | } 34 | } 35 | return l - 1; 36 | } 37 | -------------------------------------------------------------------------------- /Geometry/Convex_Hull_Graham_Scan.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Convex_Hull_Graham_Scan 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : 7 | Created : 26 May 2021 [10:09] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | struct point{ 22 | long long x,y; 23 | }; 24 | point convex[N]; 25 | long long dist(point p1,point p2){ 26 | return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y); 27 | } 28 | int orientation(point p0,point p1,point p2){ 29 | long long res=(p1.y-p0.y)*(p2.x-p0.x)-(p2.y-p0.y)*(p1.x-p0.x); 30 | if(res<0) return 0;//left turn 31 | if(res>0) return 1;//right turn 32 | if(res==0) return 2;//int the same line 33 | } 34 | bool cmp(point p1,point p2){ 35 | int res=orientation(convex[1],p1,p2); 36 | if(res==0) return true; 37 | if(res==1) return false; 38 | if(res==2) return dist(convex[1],p1) graham_scan(int n){ 42 | int top = 0; 43 | for(int i=2;i<=n;i++){ 44 | if(convex[1].x>convex[i].x){ 45 | swap(convex[1],convex[i]); 46 | } 47 | else if(convex[1].x==convex[i].x){ 48 | if(convex[1].y1&&orientation(st[top-1],st[top],convex[i])!=0) top--; 58 | st[++top]=convex[i]; 59 | } 60 | while(orientation(st[top-1],st[top],convex[1])==2) top--; 61 | vector ret; 62 | for(int i=1;i<=top;i++) 63 | ret.push_back(st[i]); 64 | return ret; 65 | } -------------------------------------------------------------------------------- /Graph_and_Tree/Articulation_Points_and_Bridges.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : _Articulation_Points_and_Bridges 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : http://www.cs.kent.edu/~aleitert/iga/slides/04ArtPointsBridges.pdf 7 | visualgo : https://visualgo.net/en/dfsbfs 8 | Created : 08 May 2021 [22:45] 9 | */ 10 | #include 11 | #define all(x) begin(x),end(x) 12 | #define sz(x) (int)(x).size() 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | vector g[N],points; 22 | vector bridges; 23 | int idx[N],low[N]; 24 | int id,root = 1; 25 | void dfs(int u,int p){ 26 | idx[u] = low[u] = ++id; 27 | int cnt = 0; 28 | for(auto x:g[u]){ 29 | if(x == p) continue; 30 | if(!idx[x]) dfs(x,u),low[u] = min(low[u],low[x]); 31 | else low[u] = min(low[u],idx[x]); 32 | cnt++; 33 | if(u != root && low[x]>=idx[u]) points.push_back(u); 34 | if(low[x]>idx[u]) bridges.push_back({u,x}); 35 | } 36 | if(u == root && cnt>1) points.push_back(u); 37 | } -------------------------------------------------------------------------------- /Graph_and_Tree/Bipartite_Check.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Bipartite_Check 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Visualgo: https://visualgo.net/en/dfsbfs 7 | Created : 06 May 2021 [14:04] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | vector g[N]; 22 | int col[N]; 23 | bool bipartite_check(int u,int c){ 24 | if(col[u]){ 25 | if(col[u] == c) return true; 26 | else return false; 27 | } 28 | col[u] = c; 29 | for(auto x:g[u]){ 30 | if(!bipartite_check(x,3-c)) 31 | return false; 32 | } 33 | return true; 34 | } 35 | //call function with bipartite_check(1,1) = start at node 1 with color 1 -------------------------------------------------------------------------------- /Graph_and_Tree/Breadth_First_Search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Breadth First Search 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language: C++ 6 | Created : 10 Aug 2021 [12:30] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | vector adj[N]; 21 | bool visited[N]; 22 | 23 | void bfs(int source){ 24 | memset(visited, false, sizeof visited); 25 | queue q; 26 | q.push(source); 27 | visited[source] = true; 28 | while(!q.empty()){ 29 | int now = q.front(); 30 | q.pop(); 31 | cout << now << ' '; 32 | for(auto x: adj[now]){ 33 | if(!visited[x]){ 34 | visited[x] = true; 35 | q.push(x); 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Graph_and_Tree/Centroid_Decomposition.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Centroid_Decomposition 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/centroid-decomposition-of-tree/ 7 | Explanation : https://codeforces.com/blog/entry/81661 8 | Created : 24 May 2021 [19:41] 9 | */ 10 | #include 11 | #define all(x) begin(x),end(x) 12 | #define sz(x) (int)(x).size() 13 | #define MOD (LL )(1e9+7) 14 | using namespace std; 15 | using LL = long long; 16 | using PII = pair; 17 | using PLL = pair; 18 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 19 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 20 | 21 | const int N = 1e5+10; 22 | vector g[N]; 23 | int p[N],vis[N],sz[N]; 24 | int find_size(int u,int pa = 0){ 25 | if(vis[u]) return 0; 26 | sz[u] = 1; 27 | for(auto x:g[u]){ 28 | if(x == pa) continue; 29 | sz[u]+=find_size(x,u); 30 | } 31 | return sz[u]; 32 | } 33 | int find_centroid(int u,int pa,int n){ 34 | for(auto x:g[u]){ 35 | if(x == pa) continue; 36 | if(vis[x]) continue; 37 | if(sz[x]<=n/2) continue; 38 | return find_centroid(x,u,n); 39 | } 40 | return u; 41 | } 42 | int init_centroid(int u,int pa){ 43 | find_size(u); 44 | int c = find_centroid(u,pa,sz[u]); 45 | vis[c] = 1; 46 | p[c] = pa; 47 | for(auto x:g[c]){ 48 | if(vis[x]) continue; 49 | init_centroid(x,c); 50 | } 51 | } 52 | // init_centroid(1,0) -------------------------------------------------------------------------------- /Graph_and_Tree/Depth_First_Search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Depth First Search 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language: C++ 6 | Created : 10 Aug 2021 [12:30] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | vector adj[N]; 21 | bool visited[N]; 22 | 23 | // call dfs_rec(source, 0) 24 | void dfs_rec(int u, int p){ 25 | cout << u << " "; 26 | visited[u] = true; 27 | for(auto x: adj[u]){ 28 | if(!visited[x]){ 29 | dfs_rec(x, u); 30 | } 31 | } 32 | } 33 | 34 | // call dfs_stack(source) 35 | void dfs_stack(int source){ 36 | memset(visited, false, sizeof visited); 37 | stack st; 38 | st.push(source); 39 | while(!st.empty()){ 40 | int now = st.top(); 41 | st.pop(); 42 | cout << now << " "; 43 | for(auto x: adj[now]){ 44 | if(!visited[x]){ 45 | visited[x] = true; 46 | st.push(x); 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Graph_and_Tree/Kahn_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Kahn's Algorithm 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language: C++ 6 | Created : 11 Aug 2021 [14:17] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | vector adj[N]; 21 | int in[N]; 22 | 23 | // Kahn's Algorithm 24 | void kahn(int nodes){ 25 | queue q; 26 | for(int i=1; i<=nodes; ++i){ 27 | if(!in[i]){ 28 | q.push(i); 29 | } 30 | } 31 | while(!q.empty()){ 32 | int now = q.front(); 33 | q.pop(); 34 | cout << now << " " ; 35 | for(auto x: adj[now]){ 36 | in[x] -= 1; 37 | if(in[x] == 0){ 38 | q.push(x); 39 | } 40 | } 41 | } 42 | } 43 | 44 | int main(){ 45 | int n, m, u, v; 46 | cin >> n >> m; 47 | for(int i=1; i<=m; ++i){ 48 | cin >> u >> v; 49 | adj[u].push_back(v); 50 | in[v]++; 51 | } 52 | kahn(n); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Graph_and_Tree/LCA_Binary_Lifting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : LCA_Binary_Lifting 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 06 May 2021 [14:54] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | const int logN = 20; 21 | vector g[N]; 22 | int LCA[N][logN],lv[N]; 23 | void dfs(int u,int p,int lvl){ 24 | LCA[u][0] = p; 25 | lv[u] = lvl; 26 | for(int i=1;i<=18;i++) 27 | LCA[u][i] = LCA[LCA[u][i-1]][i-1]; 28 | for(auto x:g[u]){ 29 | if(x == p) continue; 30 | dfs(x,u,lvl+1); 31 | } 32 | } 33 | int find_LCA(int u,int v){ 34 | if(lv[u]=0;i--){ 36 | if(lv[LCA[u][i]]=0;i--){ 42 | if(LCA[u][i] == LCA[v][i]) continue; 43 | u = LCA[u][i],v = LCA[v][i]; 44 | } 45 | return LCA[u][0]; 46 | } -------------------------------------------------------------------------------- /Graph_and_Tree/Topological_Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Topological Sort (Depth First Search) 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language: C++ 6 | Created : 30 Sep 2021 [10:54] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | vector adj[N]; 21 | int in[N]; 22 | bool visited[N]; 23 | 24 | void dfs(int u){ 25 | if(visited[u]){ 26 | return ; 27 | } 28 | visited[u] = true; 29 | cout << u << " "; 30 | for(auto x: adj[u]){ 31 | if(--in[x] == 0){ 32 | dfs(x); 33 | } 34 | } 35 | } 36 | 37 | int main(){ 38 | int n, m, u, v; 39 | cin >> n >> m; 40 | for(int i=1; i<=m; ++i){ 41 | cin >> u >> v; 42 | adj[u].push_back(v); 43 | in[v]++; 44 | } 45 | for(int i=1; i<=n; ++i){ 46 | if(in[i] == 0){ 47 | dfs(i); 48 | } 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Graph_and_Tree/Tree_Traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Tree_Traversal 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 06 May 2021 [14:01] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | struct A{ 21 | int l,r; 22 | }; 23 | A a[N]; 24 | void prefix(int u){ 25 | cout << u << ' '; 26 | prefix(a[u].l); 27 | prefix(a[u].r); 28 | } 29 | void infix(int u){ 30 | cout << u << ' '; 31 | infix(a[u].l); 32 | infix(a[u].r); 33 | } 34 | void postfix(int u){ 35 | postfix(a[u].l); 36 | postfix(a[u].r); 37 | cout << u << ' '; 38 | } -------------------------------------------------------------------------------- /Greedy/Job_sequencing.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Job_sequencing 3 | Author : PunnyOz2 4 | Language : C++ 5 | Explanation : 6 | Created : 01 October 2022 [15:39] 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | // A structure to represent a job 12 | struct Job { 13 | 14 | char id; // Job Id 15 | int dead; // Deadline of job 16 | int profit; // Profit earned if job is completed before 17 | // deadline 18 | bool operator<(const Job &o) const{ 19 | return profit > o.profit; 20 | } 21 | }; 22 | 23 | vector jobSequencingResult(vector arr, int n) { 24 | sort(arr.begin(), arr.end(), [](Job a, Job b) { return a.dead < b.dead; }); 25 | priority_queue pq; 26 | vector ans; 27 | for (int i = n - 1; i >= 0; i--) { 28 | int slot_available; 29 | if (pq.empty()) { 30 | slot_available = arr[i].dead; 31 | } else { 32 | slot_available = arr[i].dead - arr[i-1].dead; 33 | } 34 | pq.push(arr[i]); 35 | while(!slot_available && !pq.empty()) { 36 | ans.push_back(pq.top()); 37 | pq.pop(); 38 | slot_available--; 39 | } 40 | } 41 | sort(ans.begin(), ans.end(), [&](Job a, Job b) { return a.dead < b.dead; }); 42 | return ans; 43 | } 44 | 45 | int jobSequencingProfitSum(vector arr, int n) { 46 | sort(arr.begin(), arr.end(), [](Job a, Job b) { return a.dead < b.dead; }); 47 | priority_queue pq; 48 | int ans = 0; 49 | for (int i = n - 1; i >= 0; i--) { 50 | int slot_available; 51 | if (pq.empty()) { 52 | slot_available = arr[i].dead; 53 | } else { 54 | slot_available = arr[i].dead - arr[i-1].dead; 55 | } 56 | pq.push(arr[i]); 57 | while(!slot_available && !pq.empty()) { 58 | ans += pq.top().profit; 59 | pq.pop(); 60 | slot_available--; 61 | } 62 | } 63 | return ans; 64 | } -------------------------------------------------------------------------------- /Greedy/Minimum_Coin_Change.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Minimum Coin Change 3 | Author : Naravit N. [ADNCRICH] 4 | School : Chulalongkorn University 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/quick-sort/ 7 | Created : 01 Oct 2022 [15:37] 8 | */ 9 | #include 10 | #define all(x) begin(x), end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL)(1e9 + 7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 18 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 19 | const int N = 1000000, M = 10000; 20 | 21 | int dp[N + 20], coins[M + 20]; 22 | 23 | void minimum_coin_change(int n) { 24 | for (int i = 0; i < n; i++) cin >> coins[i]; 25 | memset(dp, -1, sizeof(dp)); 26 | dp[0] = 0; 27 | for (int i = 1; i <= N; i++) { 28 | for (int j = 0; j < n; j++) { 29 | if (i - coins[j] >= 0 && dp[i - coins[j]] != -1) { 30 | if (dp[i] == -1) 31 | dp[i] = dp[i - coins[j]] + 1; 32 | else 33 | dp[i] = min(dp[i], dp[i - coins[j]] + 1); 34 | } 35 | } 36 | } 37 | } 38 | 39 | int main() { 40 | int n, q, i; 41 | cin >> n >> q; 42 | minimum_coin_change(n); 43 | while (q--) { 44 | int x; 45 | cin >> x; 46 | cout << dp[x] << "\n"; 47 | } 48 | } -------------------------------------------------------------------------------- /MST/Kruskal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Kruskal 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | visualgo: https://visualgo.net/en/mst 7 | Created : 03 May 2021 [11:38] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | 17 | const int N = 100010; 18 | struct A{ 19 | int u,v; 20 | LL w; 21 | A(int u,int v,LL w):u(u),v(v),w(w){} 22 | bool operator < (const A&o) const{ 23 | return w edges){ 32 | LL sum = 0; 33 | sort(all(edges)); 34 | for(auto x:edges){ 35 | int ru = fr(x.u),rv = fr(x.v); 36 | if(ru == rv) continue; 37 | p[ru] = rv; 38 | sum+=x.w; 39 | } 40 | return sum; 41 | } 42 | int main(){ 43 | int n,m,u,v,w; 44 | vector edges; 45 | cin >> n >> m; 46 | iota(p,p+n+1,0); 47 | while(m--){ 48 | cin >> u >> v >> w; 49 | edges.emplace_back(u,v,w); 50 | } 51 | cout << kruskal(edges); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /MST/Prim.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Prim 3 | Author : Borworntat D. [Hydrolyzed~] 4 | School : RYW 5 | Language: C++ 6 | visualgo: https://visualgo.net/en/mst 7 | Created : 10 Aug 2021 [11:29] 8 | */ 9 | #include 10 | #define all(x) begin(x), end(x) 11 | #define sz(x) (int)(x).size() 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | 17 | const int N = 100010; 18 | struct A 19 | { 20 | int v; 21 | LL w; 22 | bool operator < (const A& o) const{ 23 | return w > o.w; 24 | } 25 | }; 26 | vector> adj[N]; 27 | bool visited[N]; 28 | 29 | LL prim() 30 | { 31 | LL sum = 0; 32 | priority_queue pq; 33 | pq.push({1, 0}); 34 | while(!pq.empty()){ 35 | A now = pq.top(); 36 | pq.pop(); 37 | if(visited[now.v]){ 38 | continue; 39 | } 40 | visited[now.v] = true; 41 | sum += now.w; 42 | for(auto x: adj[now.v]){ 43 | pq.push({x.first, x.second}); 44 | } 45 | } 46 | return sum; 47 | } 48 | int main() 49 | { 50 | int n, m, u, v, w; 51 | cin >> n >> m; 52 | while (m--) 53 | { 54 | cin >> u >> v >> w; 55 | adj[u].emplace_back(v, w); 56 | adj[v].emplace_back(u, w); 57 | } 58 | cout << prim(); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /MST/Second_MST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Second_MST 3 | Author : Phumipat C. [MAGCARI] 4 | Language : C++ 5 | Explanation : 6 | Created : 09 December 2021 [22:16] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 1e5+10; 20 | struct A{ 21 | int u,v,w; 22 | bool operator < (const A&o) const{ 23 | return w g[N]; 27 | 28 | int LCA[N][20],weight[N][20],lv[N]; 29 | 30 | int p[N]; 31 | int fr(int n){ 32 | return (p[n] == n)?n:(p[n] = fr(p[n])); 33 | } 34 | 35 | void init_LCA(int u,int pa,int w,int lvl = 1){ 36 | LCA[u][0] = pa; 37 | weight[u][0] = w; 38 | lv[u] = lvl; 39 | for(int i=1;i<=18;i++){ 40 | LCA[u][i] = LCA[LCA[u][i-1]][i-1]; 41 | weight[u][i] = max(weight[u][i-1],weight[LCA[u][i-1]][i-1]); 42 | } 43 | for(auto x:g[u]){ 44 | if(x.first == pa) continue; 45 | init_LCA(x.first,u,x.second,lvl+1); 46 | } 47 | } 48 | 49 | PII find_LCA_weight(int u,int v){ 50 | if(u == v) return {u,0}; 51 | 52 | int max_weight = 0; 53 | 54 | if(lv[u] < lv[v]) swap(u,v); 55 | 56 | // lower level of u 57 | for(int i=18;i>=0;i--){ 58 | if(lv[LCA[u][i]]=0;i--){ 67 | if(LCA[u][i] == LCA[v][i]) continue; 68 | max_weight = max({max_weight,weight[u][i],weight[v][i]}); 69 | u = LCA[u][i],v = LCA[v][i]; 70 | } 71 | 72 | // last step up 73 | max_weight = max({max_weight,weight[u][0],weight[v][0]}); 74 | u = LCA[u][0],v = LCA[v][0]; 75 | return {u,max_weight}; 76 | } 77 | 78 | pair > init_MST(vector edges){ 79 | vector unused; 80 | 81 | iota(p,p+N,0); 82 | sort(all(edges)); 83 | 84 | int sum = 0; 85 | for(auto x:edges){ 86 | int ru = fr(x.u),rv = fr(x.v); 87 | if(ru == rv){ 88 | unused.push_back(x); 89 | continue; 90 | } 91 | p[ru] = rv; 92 | sum+=x.w; 93 | g[x.u].push_back({x.v,x.w}); 94 | g[x.v].push_back({x.u,x.w}); 95 | } 96 | return {sum,unused}; 97 | } 98 | 99 | int second_MST(vector edges){ 100 | auto temp = init_MST(edges); 101 | int sum = temp.first; 102 | vector unused = temp.second; 103 | init_LCA(1,0,0); 104 | 105 | int ans = 1e9; 106 | for(auto x:unused){ 107 | PII old = find_LCA_weight(x.u,x.v); 108 | ans = min(ans,sum - old.second + x.w); 109 | } 110 | return ans; 111 | } -------------------------------------------------------------------------------- /Network_Flow/Maximum_Flow_Ford_Fulkerson.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Maximum_Flow_Ford_Fulkerson 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm 7 | Visualgo 8 | Created : 09 May 2021 [13:18] 9 | */ 10 | #include 11 | #define all(x) begin(x),end(x) 12 | #define sz(x) (int)(x).size() 13 | #define MOD (LL )(1e9+7) 14 | using namespace std; 15 | using LL = long long; 16 | using PII = pair; 17 | using PLL = pair; 18 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 19 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 20 | 21 | const int N = 1010; 22 | vector g[N]; 23 | queue que; 24 | int res[N][N],dis[N],p[N],s,t; 25 | int augment(int u,int now){ 26 | if(u==s) return now; 27 | if(p[u]==-1) return 0; 28 | int cap=augment(p[u],min(now,res[p[u]][u])); 29 | res[p[u]][u]-=cap; 30 | res[u][p[u]]+=cap; 31 | return cap; 32 | } 33 | int max_flow(int n){ 34 | int flow = 1e9,maxx = 0; 35 | while(flow){ 36 | memset(p,-1,sizeof p); 37 | fill(dis+1,dis+n+1,1e9); 38 | dis[s] = 0; 39 | que.push(s); 40 | while(!que.empty()){ 41 | int u = que.front(); 42 | que.pop(); 43 | if(u == t) break; 44 | for(auto v:g[u]){ 45 | if(res[u][v]==0) continue; 46 | if(dis[v]>dis[u]+1){ 47 | dis[v] = dis[u]+1; 48 | p[v] = u; 49 | que.push(v); 50 | } 51 | } 52 | } 53 | flow = augment(t,1e9); 54 | maxx+=flow; 55 | } 56 | return maxx; 57 | } 58 | -------------------------------------------------------------------------------- /Optimization_Technique/2D_Range_Minimum_Query.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : 2D Range Minimum Query 3 | Author : Borworntat D. [Hydrolzed~] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://codeforces.com/blog/entry/45485 7 | Created : 01 October 2022 [12:31] 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | const int MxN = 1010; 14 | const int LgN = 16; 15 | int sparse_table[LgN][LgN][MxN][MxN], lg[MxN]; 16 | 17 | void init(std::vector> arr){ 18 | int n = arr.size(); 19 | for(int state=2; state> 1] + 1; 21 | } 22 | memset(sparse_table, 0x3f, sizeof sparse_table); 23 | // base case 24 | for(int i=0; i 2 | using namespace std; 3 | 4 | // put sum of subarrays of size sqrt(n) into block array 5 | void preprocess_blocks(vector&arr, vector& block) 6 | { 7 | int len=block.size(); 8 | 9 | for(int i=0;i>n; 21 | 22 | vector arr(n); 23 | 24 | //input array 25 | for(int i=0;i>arr[i]; 27 | 28 | //initialize size of blocks to be sqrt(n)+1 29 | int len= sqrt(n)+1; 30 | vector block( len ); 31 | 32 | // preprocess blocks with sum 33 | preprocess_blocks(arr, block); 34 | 35 | int querySize; 36 | //input total queries 37 | cin>>querySize; 38 | 39 | int l,r; 40 | //input all the queries 41 | while(querySize--) 42 | { 43 | //1 indexed 44 | cin>>l>>r; 45 | l--; 46 | r--; 47 | 48 | int sum=0; 49 | //traversing l to r efficiently by taking 50 | //already calculated sum from block array 51 | for(int i=l;i<=r;) 52 | { 53 | //if i is the starting index of a block and that block is in 54 | //the range l to r then take precalculated sum 55 | //else take arr[i] to sum 56 | if(i%len==0 && i+len-1<=r) 57 | { 58 | sum+= block[i/len]; 59 | i+=len; 60 | } 61 | else 62 | { 63 | sum+=arr[i]; 64 | i++; 65 | } 66 | } 67 | 68 | //output sum 69 | cout< 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | int maxx[20][N]; 21 | 22 | void init(int n,vector a){ 23 | for(int i=0;i 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | const int sqN = 320; 22 | int cluster[sqN]; 23 | int a[N]; 24 | int main(){ 25 | int n,m; 26 | // array starts from 0 -> n-1 27 | cin >> n >> m; 28 | while(m--){ 29 | int opr; 30 | cin >> opr; 31 | if(opr == 1){ 32 | // Change value 33 | int idx,v; 34 | cin >> idx >> v; 35 | a[idx] = v; 36 | // change current cluster value 37 | cluster[idx/sqN] = -1e9; 38 | for(int i=(idx/sqN)*sqN;i> l >> r; 44 | int maxx = -1e9; 45 | for(int i=l;i<=r;){ 46 | if(i%sqN || i+sqN-1>r){ 47 | maxx = max(maxx,a[i]); 48 | i++; 49 | }else{ 50 | maxx = max(maxx,cluster[i/sqN]); 51 | i+=sqN; 52 | } 53 | } 54 | cout << maxx << '\n'; 55 | } 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithm 2 | ## Gathering all "Need to know" algorithm in Competitive Programming (C++) 3 | 4 | Feel free to create new pull request 5 | ## Pull Request Rule 6 | 1. Use the provided ["Template.cpp" file](Template.cpp) 7 | 2. Implement the algorithm part in new function (main function is for i/o only) 8 | 3. return the answers from the function (not using an variable to store the answers or print them directly) 9 | 4. (Optional) If possible, please provide a explanation link (e.g. blog explained about that algorithm, geeksforgreeks etc.) 10 | -------------------------------------------------------------------------------- /Searching/Exponential_Search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Exponential_Search.cpp 3 | Author : sumit-awasthi 4 | Language : C++ 5 | Explanation : 6 | */ 7 | #include 8 | #define all(x) begin(x), end(x) 9 | #define sz(x) (int)(x).size() 10 | #define MOD (LL)(1e9 + 7) 11 | 12 | #include 13 | 14 | using namespace std; 15 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 16 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 17 | 18 | int binarySearch(int array[], int start, int end, int key) { 19 | if(start <= end) { 20 | int mid = (start + (end - start) /2); 21 | if(array[mid] == key) 22 | return mid; 23 | if(array[mid] > key) 24 | return binarySearch(array, start, mid-1, key); 25 | return binarySearch(array, mid+1, end, key); 26 | } 27 | return -1; 28 | } 29 | 30 | int exponentialSearch(int array[], int start, int end, int key){ 31 | if((end - start) <= 0) 32 | return -1; 33 | int i = 1; // as 2^0 = 1 34 | while(i < (end - start)){ 35 | if(array[i] < key) 36 | i *= 2; 37 | else 38 | break; 39 | } 40 | return binarySearch(array, i/2, i, key); 41 | } 42 | 43 | int main() { 44 | int n, searchKey, loc; 45 | cout << "Enter number of items: "; 46 | cin >> n; 47 | int arr[n]; 48 | cout << "Enter items: " << endl; 49 | for(int i = 0; i< n; i++) { 50 | cin >> arr[i]; 51 | } 52 | cout << "Enter search key to search in the list: "; 53 | cin >> searchKey; 54 | if((loc = exponentialSearch(arr, 0, n, searchKey)) >= 0) 55 | cout << "Item found at Index= " << loc << endl; 56 | else 57 | cout << "Item is not found in the list." << endl; 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Searching/Fibonacci_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | Task : Fibonacci_search.cpp 7 | Author : Saurabh-2809 8 | Language : C++ 9 | Explanation : 10 | */ 11 | 12 | #define all(x) begin(x), end(x) 13 | #define sz(x) (int)(x).size() 14 | #define MOD (LL)(1e9 + 7) 15 | 16 | using namespace std; 17 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 18 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 19 | 20 | using namespace std; 21 | 22 | int min(int x, int y) { return (x <= y) ? x : y; } 23 | 24 | int fibMonaccianSearch(int arr[], int x, int n) 25 | { 26 | 27 | int fibMMm2 = 0; // (m-2)'th Fibonacci No. 28 | int fibMMm1 = 1; // (m-1)'th Fibonacci No. 29 | int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci 30 | 31 | while (fibM < n) { 32 | fibMMm2 = fibMMm1; 33 | fibMMm1 = fibM; 34 | fibM = fibMMm2 + fibMMm1; 35 | } 36 | 37 | int offset = -1; 38 | 39 | while (fibM > 1) { 40 | 41 | int i = min(offset + fibMMm2, n - 1); 42 | 43 | if (arr[i] < x) { 44 | fibM = fibMMm1; 45 | fibMMm1 = fibMMm2; 46 | fibMMm2 = fibM - fibMMm1; 47 | offset = i; 48 | } 49 | 50 | else if (arr[i] > x) { 51 | fibM = fibMMm2; 52 | fibMMm1 = fibMMm1 - fibMMm2; 53 | fibMMm2 = fibM - fibMMm1; 54 | } 55 | 56 | else 57 | return i; 58 | } 59 | 60 | if (fibMMm1 && arr[offset + 1] == x) 61 | return offset + 1; 62 | 63 | return -1; 64 | } 65 | 66 | /* main function for I/O*/ 67 | int main(void) 68 | { 69 | int n,i; 70 | cout<<"\nEnter the total number of elements:"; 71 | cin>>n; 72 | int arr[n]; 73 | cout<<"Enter elements in array\n"; 74 | for( i=1 ; i>arr[i]; 77 | } 78 | int m = sizeof(arr)/sizeof(arr[0]); 79 | int x; 80 | cout<<"\nEnter element to be searched:"; 81 | cin>>x; 82 | int ind = fibMonaccianSearch(arr, x, n); 83 | if(ind >= 0) 84 | cout << "Found at index: " << ind; 85 | else 86 | cout << x << " isn't present in the array"; 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Searching/jump_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int jump_Search(int a[], int n, int item) { 6 | int i = 0; 7 | int m = sqrt(n); //initializing block size= √(n) 8 | 9 | while(a[m] <= item && m < n) { 10 | // the control will continue to jump the blocks 11 | i = m; // shift the block 12 | m += sqrt(n); 13 | if(m > n - 1) // if m exceeds the array size 14 | return -1; 15 | } 16 | 17 | for(int x = i; x> n; 28 | int arr[n]; //creating an array of size n 29 | cout << "\n Enter items: "; 30 | 31 | for(int i = 0; i< n; i++) { 32 | cin >> arr[i]; 33 | } 34 | 35 | cout << "\n Enter search key to be found in the array: "; 36 | cin >> item; 37 | loc = jump_Search(arr, n, item); 38 | if(loc>=0) 39 | cout << "\n Item found at location: " << loc; 40 | else 41 | cout << "\n Item is not found in the list."; 42 | } 43 | -------------------------------------------------------------------------------- /Shortest_Path/Bellman_Ford.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Bellman_Ford 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [14:57] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 100010; 19 | struct A{ 20 | int u,v,w; 21 | }; 22 | LL dis[N]; 23 | // Bellman Ford can be used to detect "Negative Weight Cycle" 24 | LL bellman_ford(int n,int st,int en,vector edges){ 25 | dis[st] = 0; 26 | for(int i=1;i edges; 41 | cin >> n >> m; 42 | fill(dis,dis+n+1,1e18); 43 | edges.resize(m); 44 | for(int i=0;i> edges[i].u >> edges[i].v >> edges[i].w; 46 | cout << bellman_ford(n,1,n,edges); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Shortest_Path/Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Dijkstra 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : 7 | Created : 09 August 2021 [22:13] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL )(1e9+7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 10010; 21 | struct A{ 22 | int current_node,total_distance; 23 | A(int n,int d):current_node(n),total_distance(d){} 24 | bool operator < (const A&o) const{ 25 | return total_distance>o.total_distance; 26 | } 27 | }; 28 | int dis[N]; 29 | vector > g[N]; 30 | int dijkstra(int source,int destination){ 31 | priority_queue heap; 32 | fill(dis,dis+N,2e9); 33 | dis[source] = 0; 34 | heap.push({source,0}); 35 | while(!heap.empty()){ 36 | auto now = heap.top(); 37 | heap.pop(); 38 | for(auto x:g[now.current_node]){ 39 | if(dis[x.first]<=now.total_distance+x.second) continue; 40 | dis[x.first] = now.total_distance + x.second; 41 | heap.push({x.first,dis[x.first]}); 42 | } 43 | } 44 | return dis[destination]; 45 | } 46 | int main(){ 47 | int n,m,source,destination; 48 | cin >> n >> m >> source >> destination; 49 | for(int i=1;i<=m;i++){ 50 | int u,v,w; 51 | cin >> u >> v >> w; 52 | g[u].emplace_back(v,w); 53 | g[v].emplace_back(u,w); 54 | } 55 | cout << dijkstra(source,destination); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Shortest_Path/Floyd_Warshall.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Floyd_Warshall 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [15:07] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 1010; 19 | LL dis[N][N]; 20 | // Floyd-Warshall is All-pairs Shortest Path 21 | void floyd_warshall(int n){ 22 | for(int k=1;k<=n;k++) 23 | for(int i=1;i<=n;i++) 24 | for(int j=1;j<=n;j++) 25 | dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]); 26 | } 27 | int main(){ 28 | int n,m,u,v; 29 | LL w; 30 | cin >> n >> m; 31 | for(int i=1;i<=n;i++) 32 | for(int j=1;j<=n;j++) 33 | dis[i][j] = 1e18; 34 | while(m--){ 35 | cin >> u >> v >> w; 36 | dis[u][v] = dis[v][u] = min({dis[u][v],dis[v][u],w}); 37 | } 38 | floyd_warshall(n); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Sorting/Bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Bubble_sort 3 | Author : Phumipat C. [MAGCARI] 4 | Language : C++ 5 | Explanation : 6 | Created : 01 October 2022 [13:44] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | const int N = 10010; 19 | int a[N]; 20 | void bubbleSort(int n){ 21 | bool swapped = true; 22 | while(swapped){ 23 | swapped = false; 24 | for(int i=1;i a[i+1]) 26 | swap(a[i],a[i+1]),swapped = true; 27 | } 28 | } -------------------------------------------------------------------------------- /Sorting/Counting_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Counting_sort 3 | Author : PunnyOz2 4 | Language : C++ 5 | Explanation : 6 | Created : 01 October 2022 [14:20] 7 | */ 8 | #include 9 | using namespace std; 10 | void countingSort(int* start, int* end){ 11 | int* temp = start; 12 | int Max = *max_element(start, end); 13 | int Min = *min_element(start, end); 14 | int range = Max - Min + 1; 15 | vector cnt(range); 16 | for(int* i = start; i != end; i++){ 17 | cnt[*i - Min]++; 18 | } 19 | for(int i = 0; i < range; i++){ 20 | while(cnt[i]--){ 21 | *temp = i + Min; 22 | temp++; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Sorting/Insertion_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Insertion Sort 3 | Author : Nikhil Gautam 4 | Language : C++ 5 | Explanation : https://www.geeksforgeeks.org/insertion-sort/ 6 | */ 7 | #include 8 | #define all(x) begin(x), end(x) 9 | #define sz(x) (int)(x).size() 10 | #define MOD (LL)(1e9 + 7) 11 | using namespace std; 12 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 13 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 14 | 15 | 16 | void display(int a[],int n){ 17 | for(int i=0;itemp && j>=0){ 27 | a[j+1]=a[j]; 28 | j--; 29 | } 30 | a[j+1]=temp; 31 | } 32 | } 33 | 34 | int main(){ 35 | int n; 36 | cin>>n; 37 | int a[n]; 38 | for(int i=0;i>a[i]; 40 | } 41 | insertsort(a,n); 42 | display(a,n); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Sorting/Merge_Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Merge_Sort 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/merge-sort/ 7 | visualgo : https://visualgo.net/en/sorting 8 | Created : 03 May 2021 [19:55] 9 | */ 10 | #include 11 | #define all(x) begin(x),end(x) 12 | #define sz(x) (int)(x).size() 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 18 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 19 | 20 | const int N = 100010; 21 | int a[N],b[N]; 22 | void merge_sort(int l,int r){ 23 | if(l == r) return ; 24 | int mid = (l+r)/2; 25 | merge_sort(l,mid),merge_sort(mid+1,r); 26 | int i = l,j = mid+1,k=l; 27 | while(i<=mid && j<=r){ 28 | if(a[i]<=a[j]) b[k++] = a[i++]; 29 | else b[k++] = a[j++]; 30 | } 31 | while(i<=mid) b[k++] = a[i++]; 32 | while(j<=r) b[k++] = a[j++]; 33 | for(int k=l;k<=r;k++) 34 | a[k] = b[k]; 35 | } -------------------------------------------------------------------------------- /Sorting/Optimised_Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | //This is an optimised Bubble Sort where the function exits as soon as the array is sorted. 2 | 3 | #include 4 | using namespace std; 5 | void display(int a[],int n){ //display function to display the sorted array 6 | cout<<"Sorted Array"<a[j+1]){ 17 | temp = a[j]; 18 | a[j] = a[j+1]; //swapping the values 19 | a[j+1] = temp; 20 | flag = 1; // flag being updated 21 | } 22 | } 23 | if(flag == 0){ 24 | display(a,n); 25 | return; // exiting the function 26 | } 27 | } 28 | display(a,n); 29 | } 30 | 31 | //main function to take the input and call the bubbleSort method. 32 | int main(){ 33 | int n,a[100]; 34 | cout<<"Enter the size of the array : "; 35 | cin>>n; 36 | a[n]; 37 | cout<<"Enter the elements in the array"<>a[i]; 40 | } 41 | bubbleSort(a,n); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Sorting/Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Quick Sort 3 | Author : Naravit N. [ADNCRICH] 4 | School : Chulalongkorn University 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/quick-sort/ 7 | Created : 01 Oct 2022 [13:49] 8 | */ 9 | #include 10 | #define all(x) begin(x), end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL)(1e9 + 7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 18 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 19 | const int N = 1000010; 20 | 21 | int a[N]; 22 | 23 | int find_pos(int begin, int end) { 24 | int pivot = a[end]; 25 | int pos = begin - 1; 26 | for (int i = begin; i <= end; i++) { 27 | if (a[i] < pivot) { 28 | pos++; 29 | swap(a[i], a[pos]); 30 | } 31 | } 32 | swap(a[pos + 1], a[end]); 33 | return pos + 1; 34 | } 35 | void quick_sort(int begin, int end) { 36 | if (begin < end) { 37 | int pos = find_pos(begin, end); 38 | quick_sort(begin, pos - 1); 39 | quick_sort(pos + 1, end); 40 | } 41 | } 42 | int main() { 43 | int n, i; 44 | cin >> n; 45 | for (i = 0; i < n; i++) cin >> a[i]; 46 | quick_sort(0, n - 1); 47 | for (i = 0; i < n; i++) cout << a[i] << " "; 48 | } -------------------------------------------------------------------------------- /Sorting/Radix_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Radix_sort 3 | Author : PunnyOz2 4 | Language : C++ 5 | Explanation : 6 | Created : 01 October 2022 [13:56] 7 | */ 8 | #include 9 | using namespace std; 10 | void radixSort(int* start, int* end){ 11 | int* temp = start; 12 | int Max = *max_element(start, end); 13 | queue cnt[10]; 14 | for(int i = 1; Max/i > 0; i *= 10){ 15 | for(int* j = start; j != end; j++){ 16 | cnt[(*j/i)%10].push(*j); 17 | } 18 | temp = start; 19 | for(int j = 0; j < 10; j++){ 20 | while(!cnt[j].empty()){ 21 | *temp = cnt[j].front(); 22 | cnt[j].pop(); 23 | temp++; 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Sorting/Random_Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Quick Sort 3 | Author : Naravit N. [ADNCRICH] 4 | School : Chulalongkorn University 5 | Language : C++ 6 | Explanation : https://www.geeksforgeeks.org/quick-sort/ 7 | Created : 01 Oct 2022 [13:49] 8 | */ 9 | #include 10 | #define all(x) begin(x), end(x) 11 | #define sz(x) (int)(x).size() 12 | #define MOD (LL)(1e9 + 7) 13 | using namespace std; 14 | using LL = long long; 15 | using PII = pair; 16 | using PLL = pair; 17 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 18 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 19 | const int N = 1000010; 20 | 21 | int a[N]; 22 | 23 | int find_pos(int begin, int end) { 24 | srand(time(NULL)); 25 | int random = begin + rand() % (end - begin); 26 | swap(a[random], a[end]); 27 | int pivot = a[end]; 28 | int pos = begin - 1; 29 | for (int i = begin; i <= end; i++) { 30 | if (a[i] < pivot) { 31 | pos++; 32 | swap(a[i], a[pos]); 33 | } 34 | } 35 | swap(a[pos + 1], a[end]); 36 | return pos + 1; 37 | } 38 | void random_quick_sort(int begin, int end) { 39 | if (begin < end) { 40 | int pos = find_pos(begin, end); 41 | random_quick_sort(begin, pos - 1); 42 | random_quick_sort(pos + 1, end); 43 | } 44 | } 45 | int main() { 46 | int n, i; 47 | cin >> n; 48 | for (i = 0; i < n; i++) cin >> a[i]; 49 | random_quick_sort(0, n - 1); 50 | for (i = 0; i < n; i++) cout << a[i] << " "; 51 | } -------------------------------------------------------------------------------- /Sorting/Selection_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Selection_sort 3 | Author : Phumipat C. [MAGCARI] 4 | Language : C++ 5 | Explanation : 6 | Created : 01 October 2022 [13:55] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | const int N = 10010; 19 | int a[N]; 20 | void selectionSort(int n){ 21 | for(int i=1;i<=n;i++){ 22 | int idx=i; 23 | for(int j=i;j<=n;j++) 24 | if(a[j] < a[idx]) 25 | idx = j; 26 | swap(a[i],a[idx]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sorting/heap_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Heap Sort 3 | Author : Gulamali S. [Night] 4 | Language : C++ 5 | Explanation : 6 | Created : 04 October 2022 [21:19] 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | void heap(int arr[], int n, int i) 13 | { 14 | int largest = i; 15 | int l = 2 * i + 1; 16 | int r = 2 * i + 2; 17 | 18 | if (l < n && arr[l] > arr[largest]) 19 | largest = l; 20 | 21 | if (r < n && arr[r] > arr[largest]) 22 | largest = r; 23 | 24 | if (largest != i) { 25 | swap(arr[i], arr[largest]); 26 | heap(arr, n, largest); 27 | } 28 | } 29 | 30 | 31 | void heapSort(int arr[], int n) 32 | { 33 | 34 | for (int i = n / 2 - 1; i >= 0; i--) 35 | heap(arr, n, i); 36 | 37 | for (int i = n - 1; i >= 0; i--) { 38 | // Move current root to end 39 | swap(arr[0], arr[i]); 40 | 41 | heap(arr, i, 0); 42 | } 43 | } 44 | 45 | void printArray(int arr[], int n) 46 | { 47 | for (int i = 0; i < n; ++i) 48 | cout << arr[i] << " "; 49 | cout << "\n"; 50 | } 51 | 52 | int main() 53 | { 54 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 55 | int n = sizeof(arr) / sizeof(arr[0]); 56 | 57 | heapSort(arr, n); 58 | 59 | cout << "Sorted array is \n"; 60 | printArray(arr, n); 61 | } 62 | -------------------------------------------------------------------------------- /String_Matching_Algorithm/KMP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : KMP [Knuth Morris Pratt] 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 03 May 2021 [15:19] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | using namespace std; 12 | using LL = long long; 13 | using PII = pair; 14 | using PLL = pair; 15 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 16 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 17 | 18 | const int N = 100010; 19 | char A[N],B[N]; 20 | int pos[N]; 21 | int KMP(){ // Check if String B found in String A 22 | int n = strlen(A+1),m = strlen(B+1),j = 0; 23 | for(int i=2;i<=m;i++){ 24 | while(j>0 && B[j+1]!=B[i]) j = pos[j]; 25 | if(B[j+1] == B[i]) j++; 26 | pos[i] = j; 27 | } 28 | j = 0; 29 | for(int i=1;i<=n;i++){ 30 | while(j>0 && B[j+1]!=A[i]) j = pos[j]; 31 | if(B[j+1] == A[i]) j++; 32 | if(j == m) 33 | return 1; // Found! 34 | } 35 | return 0; // Not found! 36 | } -------------------------------------------------------------------------------- /String_Matching_Algorithm/Rabin_Karp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : _Rabin_Karp 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | Created : 04 May 2021 [16:45] 7 | */ 8 | #include 9 | #define all(x) begin(x),end(x) 10 | #define sz(x) (int)(x).size() 11 | #define MOD (LL )(1e9+7) 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | char a[N],b[N]; 21 | // Check if String b is existed in String a 22 | int rabin_karp(){ 23 | int n = strlen(a),m = strlen(b); 24 | if(m>n) return 0; // Not found! 25 | int ha = 0,hb = 0,v = 1; 26 | for(int i=0;i> a >> b; 47 | cout << rabin_karp(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Strongly_Connected_Component/Kosaraju.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Kosaraju 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | visualgo: https://visualgo.net/en/dfsbfs 7 | Created : 03 May 2021 [22:23] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | vector g[N],rg[N]; 21 | int comp[N]; 22 | bool mark[N]; 23 | stack st; 24 | void dfs(int u){ 25 | mark[u] = true; 26 | for(auto x:g[u]){ 27 | if(mark[x]) continue; 28 | dfs(x); 29 | } 30 | st.push(u); 31 | } 32 | void dfs2(int u,int id){ 33 | comp[u] = id; 34 | mark[u] = true; 35 | for(auto x:rg[u]){ 36 | if(mark[x]) continue; 37 | dfs2(x,id); 38 | } 39 | } 40 | int main(){ 41 | int n,m,u,v; 42 | cin >> n >> m; 43 | while(m--){ 44 | cin >> u >> v; 45 | g[u].push_back(v); 46 | rg[v].push_back(u); 47 | } 48 | for(int i=1;i<=n;i++){ 49 | if(mark[i]) continue; 50 | dfs(i); 51 | } 52 | memset(mark,0,sizeof mark); 53 | int id = 0; 54 | while(!st.empty()){ 55 | int now = st.top(); 56 | st.pop(); 57 | id++; 58 | if(mark[now]) continue; 59 | dfs2(now,id); 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Strongly_Connected_Component/Tarjan.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Tarjan 3 | Author : Phumipat C. [MAGCARI] 4 | School : RYW 5 | Language: C++ 6 | visualgo: https://visualgo.net/en/dfsbfs 7 | Created : 03 May 2021 [12:26] 8 | */ 9 | #include 10 | #define all(x) begin(x),end(x) 11 | #define sz(x) (int)(x).size() 12 | using namespace std; 13 | using LL = long long; 14 | using PII = pair; 15 | using PLL = pair; 16 | const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}}; 17 | const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}}; 18 | 19 | const int N = 100010; 20 | vector g[N]; 21 | int idx[N],low[N],onstack[N],comp[N]; 22 | stack st; 23 | int id,comp_num; 24 | void dfs(int u,int p){ 25 | idx[u] = low[u] = ++id; 26 | onstack[u] = 1; 27 | st.push(u); 28 | for(auto x:g[u]){ 29 | if(!idx[x]) dfs(x,u),low[u] = min(low[u],low[x]); 30 | else if(onstack[x]) low[u] = min(low[u],idx[x]); 31 | } 32 | if(idx[u] == low[u]){ 33 | ++comp_num; 34 | while(!comp[u]){ 35 | comp[st.top()] = comp_num; 36 | onstack[st.top()] = 0; 37 | st.pop(); 38 | } 39 | } 40 | } 41 | //comp[i] = ID of SCC that i-node relies on -------------------------------------------------------------------------------- /Template.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task : Template 3 | Author : 4 | Language : C++ 5 | Explanation : 6 | */ 7 | #include 8 | #define all(x) begin(x), end(x) 9 | #define sz(x) (int)(x).size() 10 | #define MOD (LL)(1e9 + 7) 11 | using namespace std; 12 | const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; 13 | const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; 14 | 15 | int main() 16 | { 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | --- 4 | 5 | # Data Structure 6 | * [Fenwick Tree](https://github.com/phumipatc/Algorithm/blob/master/Data_Structure/Fenwick_Tree.cpp) 7 | ```cpp 8 | void upd(int idx,int v){ 9 | for(;idx<=N;idx+=idx&=idx) 10 | tree[idx]+=v; 11 | } 12 | LL read(int idx,LL sum=0){ 13 | for(;idx;idx-=idx&-idx) 14 | sum+=tree[idx]; 15 | return sum; 16 | } 17 | ``` 18 | * [Segment Tree](https://github.com/phumipatc/Algorithm/blob/master/Data_Structure/Segment_Tree.cpp) 19 | ```cpp 20 | void upd_point(int l,int r,int now,int idx,int v){ 21 | if(l == r){ 22 | tree[now]+=v; 23 | return ; 24 | } 25 | int mid = (l+r)/2; 26 | if(idx<=mid) upd_point(l,mid,now*2,idx,v); 27 | else upd_point(mid+1,r,now*2+1,idx,v); 28 | tree[now]+=tree[now*2] + tree[now*2+1]; 29 | } 30 | LL read_point(int l,int r,int now,int idx){ 31 | if(l == r) return tree[now]; 32 | int mid = (l+r)/2; 33 | if(idx<=mid) return read_point(l,mid,now*2,idx); 34 | else return read_point(mid+1,r,now*2+1,idx); 35 | } 36 | ``` 37 | 38 | 39 | 40 | Text can be **bold**, _italic_, or ~~strikethrough~~. 41 | 42 | [Link to another page](./another-page.html). 43 | 44 | There should be whitespace between paragraphs. 45 | 46 | There should be whitespace between paragraphs. We recommend including a README, or a file with information about your project. 47 | 48 | # Header 1 49 | 50 | This is a normal paragraph following a header. GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere. 51 | 52 | ## Header 2 53 | 54 | > This is a blockquote following a header. 55 | > 56 | > When something is important enough, you do it even if the odds are not in your favor. 57 | 58 | ### Header 3 59 | 60 | ```js 61 | // Javascript code with syntax highlighting. 62 | var fun = function lang(l) { 63 | dateformat.i18n = require('./lang/' + l) 64 | return true; 65 | } 66 | ``` 67 | 68 | 69 | #### Header 4 70 | 71 | * This is an unordered list following a header. 72 | * This is an unordered list following a header. 73 | * This is an unordered list following a header. 74 | 75 | ##### Header 5 76 | 77 | 1. This is an ordered list following a header. 78 | 2. This is an ordered list following a header. 79 | 3. This is an ordered list following a header. 80 | 81 | ###### Header 6 82 | 83 | | head1 | head two | three | 84 | |:-------------|:------------------|:------| 85 | | ok | good swedish fish | nice | 86 | | out of stock | good and plenty | nice | 87 | | ok | good `oreos` | hmm | 88 | | ok | good `zoute` drop | yumm | 89 | 90 | ### There's a horizontal rule below this. 91 | 92 | * * * 93 | 94 | ### Here is an unordered list: 95 | 96 | * Item foo 97 | * Item bar 98 | * Item baz 99 | * Item zip 100 | 101 | ### And an ordered list: 102 | 103 | 1. Item one 104 | 1. Item two 105 | 1. Item three 106 | 1. Item four 107 | 108 | ### And a nested list: 109 | 110 | - level 1 item 111 | - level 2 item 112 | - level 2 item 113 | - level 3 item 114 | - level 3 item 115 | - level 1 item 116 | - level 2 item 117 | - level 2 item 118 | - level 2 item 119 | - level 1 item 120 | - level 2 item 121 | - level 2 item 122 | - level 1 item 123 | 124 | ### Small image 125 | 126 | ![Octocat](https://github.githubassets.com/images/icons/emoji/octocat.png) 127 | 128 | ### Large image 129 | 130 | ![Branching](https://guides.github.com/activities/hello-world/branching.png) 131 | 132 | 133 | ### Definition lists can be used with HTML syntax. 134 | 135 |
136 |
Name
137 |
Godzilla
138 |
Born
139 |
1952
140 |
Birthplace
141 |
Japan
142 |
Color
143 |
Green
144 |
145 | 146 | ``` 147 | Long, single-line code blocks should not wrap. They should horizontally scroll if they are too long. This line should be long enough to demonstrate this. 148 | ``` 149 | 150 | ``` 151 | The final element. 152 | ``` 153 | --------------------------------------------------------------------------------