-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
775 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Strongly Connected Components (Kosaraju's Algo) | ||
Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, Find the number of strongly connected components in the graph. | ||
reverse graph | ||
order vector - dfs graph when reaches a visited node then backsrtrack and put in order vector | ||
dfs in reverse graph each time increase counter | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
class Solution | ||
{ | ||
public: | ||
vector<int> order; //global order vector | ||
void rdfs(int s, vector<int> &vis1, vector<int> rev[]) //reverse dfs | ||
{ | ||
vis1[s] = 1; | ||
for (auto i : rev[s]) | ||
{ | ||
if (!vis1[i]) | ||
{ | ||
rdfs(i, vis1, rev); | ||
} | ||
} | ||
} | ||
void dfs(int s, vector<int> &vis, vector<int> g[]) | ||
{ | ||
vis[s] = 1; | ||
for (auto i : g[s]) | ||
{ | ||
if (!vis[i]) | ||
{ | ||
dfs(i, vis, g); | ||
} | ||
} | ||
order.push_back(s); //when backstracking we are inserting to order vector | ||
} | ||
//Function to find the number of islands. | ||
int kosaraju(int V, vector<int> adj[]) | ||
{ | ||
//code here | ||
order.clear(); | ||
vector<int> rev[V]; | ||
for (int y = 0; y < V; y++) | ||
{ | ||
for (auto x : adj[y]) | ||
{ | ||
rev[x].push_back(y); //rev graph input | ||
} | ||
} | ||
vector<int> vis(V, 0); //visited vector | ||
for (int i = 0; i < V; i++) | ||
{ | ||
if (!vis[i]) | ||
{ | ||
dfs(i, vis, adj); //if not visited then dfs | ||
} | ||
} | ||
vector<int> vis1(V, 0); //for second dfs | ||
int count = 0; //number of components | ||
for (int i = V - 1; i >= 0; i--) | ||
{ //reverse loop of order vector | ||
if (!vis1[order[i]]) | ||
{ | ||
rdfs(order[i], vis1, rev); | ||
count++; //counter increases on each rdfs call | ||
} | ||
} | ||
return count; | ||
} | ||
}; | ||
int main() | ||
{ | ||
int v; //no of node/vertex | ||
int e; // no of edges | ||
|
||
cin >> v >> e; | ||
vector<int> g[v]; | ||
for (int i = 0; i < e; i++) | ||
{ //taking e no. of edges input | ||
int x, y; | ||
cin >> x >> y; | ||
g[x].push_back(y); //1->5 | ||
// g[y].push_back(x); //5->1 (undirected) | ||
} | ||
Solution ob; | ||
int ans = ob.kosaraju(v, g); | ||
cout << ans << '\n'; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Find maximum sum possible equal sum of three stacks | ||
Difficulty Level : Easy | ||
Last Updated : 25 Mar, 2021 | ||
Given three stacks of the positive numbers, the task is to find the possible equal maximum sum of the stacks with the removal of top elements allowed. Stacks are represented as an array, and the first index of the array represent the top element of the stack. | ||
Examples: | ||
Input : stack1[] = { 3, 10} | ||
stack2[] = { 4, 5 } | ||
stack3[] = { 2, 1 } | ||
Output : 0 | ||
Sum can only be equal after removing all elements | ||
from all stacks. | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define ff first | ||
#define ss second | ||
// #define int long long | ||
#define pb push_back | ||
#define mp make_pair | ||
#define pii pair<int, int> | ||
#define vi vector<int> | ||
#define mii map<int, int> | ||
#define pqb priority_queue<int> //max heap | ||
#define pqs priority_queue<int, vi, greater<int>> //min heap | ||
#define setbits(x) __builtin_popcountll(x) //gives number of setbits | ||
#define zrobits(x) __builtin_ctzll(x) //gives number of 0 bits | ||
#define mod 1000000007 // 1e9+7 ans%mod | ||
#define inf 1e18 | ||
#define ps(x, y) fixed << setprecision(y) << x | ||
#define mk(arr, n, type) type *arr = new type[n]; //dynamic array | ||
#define w(x) \ | ||
int x; \ | ||
cin >> x; \ | ||
while (x--) //test case | ||
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //random shuffle | ||
|
||
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //policy based ds | ||
|
||
#define deb(x) cout << #x << " " << x << endl; //for debugging | ||
#define fo(i, n) for (i = 0; i < n; i++) | ||
#define fo1(i, n) for (i = 1; i <= n; i++) | ||
#define Fo(i, k, n) for (i = k; i < n; i++) | ||
#define vii vector<pii> | ||
#define rep(i, a, b) for (int i = a; i < b; i++) | ||
|
||
#define vi vector<int> | ||
#define vvi vector<vi> | ||
|
||
/* | ||
struct Node | ||
{ | ||
int data; | ||
struct Node *left; //pointer pointing towards left child | ||
struct Node *right; //pointer for right child | ||
Node(int val) //constructor taking data | ||
{ | ||
data = val; | ||
left = NULL; //initially | ||
right = NULL; | ||
} | ||
}; | ||
*/ | ||
|
||
int main() | ||
{ | ||
int n1, n2, n3, i; | ||
cin >> n1 >> n2 >> n3; | ||
int s1[n1], s2[n2], s3[n3]; | ||
fo(i, n1) cin >> s1[i]; //taking input in 3 stacks | ||
fo(i, n2) cin >> s2[i]; | ||
fo(i, n3) cin >> s3[i]; | ||
|
||
int sum1 = 0, sum2 = 0, sum3 = 0; //finding sum of 3 stacks | ||
fo(i, n1) sum1 += s1[i]; | ||
fo(i, n2) sum2 += s2[i]; | ||
fo(i, n3) sum3 += s3[i]; | ||
|
||
int t1 = 0, t2 = 0, t3 = 0; //top as 0 (first element) (iterator) | ||
int ans = 0; | ||
|
||
while (true) | ||
{ | ||
if (t1 == n1 or t2 == n2 or t3 == n3) | ||
{ //base case if any of them reaches end | ||
ans = 0; //answer can't be formed | ||
break; | ||
} | ||
if (sum1 == sum2 and sum2 == sum3) | ||
{ //base case if sums are equal | ||
ans = sum1; | ||
break; | ||
} | ||
/* | ||
we are removing the top element from the stack with biggest sum | ||
*/ | ||
if (sum1 >= sum2 and sum1 >= sum3) //if sum 1 is the largest value | ||
|
||
sum1 -= s1[t1++]; //we will remove top element from stack(sum) | ||
|
||
else if (sum2 >= sum1 and sum2 >= sum3) | ||
|
||
sum2 -= s2[t2++]; | ||
|
||
else if (sum3 >= sum2 and sum3 >= sum1) | ||
|
||
sum3 -= s3[t3++]; | ||
} | ||
cout << ans; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.