mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-28 15:06:38 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -13,10 +13,8 @@ using namespace std;
|
||||
* @param V : vertices
|
||||
* @return void
|
||||
**/
|
||||
void print(vector<int> a[], int V)
|
||||
{
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
void print(vector<int> a[], int V) {
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (!a[i].empty())
|
||||
cout << "i=" << i << "-->";
|
||||
for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << " ";
|
||||
@@ -33,11 +31,9 @@ void print(vector<int> a[], int V)
|
||||
* @param adj[] : array of vectors to represent graph
|
||||
* @return void
|
||||
**/
|
||||
void push_vertex(int v, stack<int> &st, bool vis[], vector<int> adj[])
|
||||
{
|
||||
void push_vertex(int v, stack<int> &st, bool vis[], vector<int> adj[]) {
|
||||
vis[v] = true;
|
||||
for (auto i = adj[v].begin(); i != adj[v].end(); i++)
|
||||
{
|
||||
for (auto i = adj[v].begin(); i != adj[v].end(); i++) {
|
||||
if (vis[*i] == false)
|
||||
push_vertex(*i, st, vis, adj);
|
||||
}
|
||||
@@ -51,12 +47,10 @@ void push_vertex(int v, stack<int> &st, bool vis[], vector<int> adj[])
|
||||
* @param grev[] : graph with reversed edges
|
||||
* @return void
|
||||
**/
|
||||
void dfs(int v, bool vis[], vector<int> grev[])
|
||||
{
|
||||
void dfs(int v, bool vis[], vector<int> grev[]) {
|
||||
vis[v] = true;
|
||||
// cout<<v<<" ";
|
||||
for (auto i = grev[v].begin(); i != grev[v].end(); i++)
|
||||
{
|
||||
for (auto i = grev[v].begin(); i != grev[v].end(); i++) {
|
||||
if (vis[*i] == false)
|
||||
dfs(*i, vis, grev);
|
||||
}
|
||||
@@ -72,21 +66,17 @@ no SCCs i.e. none(0) or there will be x no. of SCCs (x>0)) i.e. it returns the
|
||||
count of (number of) strongly connected components (SCCs) in the graph.
|
||||
(variable 'count_scc' within function)
|
||||
**/
|
||||
int kosaraju(int V, vector<int> adj[])
|
||||
{
|
||||
int kosaraju(int V, vector<int> adj[]) {
|
||||
bool vis[V] = {};
|
||||
stack<int> st;
|
||||
for (int v = 0; v < V; v++)
|
||||
{
|
||||
for (int v = 0; v < V; v++) {
|
||||
if (vis[v] == false)
|
||||
push_vertex(v, st, vis, adj);
|
||||
}
|
||||
// making new graph (grev) with reverse edges as in adj[]:
|
||||
vector<int> grev[V];
|
||||
for (int i = 0; i < V + 1; i++)
|
||||
{
|
||||
for (auto j = adj[i].begin(); j != adj[i].end(); j++)
|
||||
{
|
||||
for (int i = 0; i < V + 1; i++) {
|
||||
for (auto j = adj[i].begin(); j != adj[i].end(); j++) {
|
||||
grev[*j].push_back(i);
|
||||
}
|
||||
}
|
||||
@@ -95,12 +85,10 @@ int kosaraju(int V, vector<int> adj[])
|
||||
// reinitialise visited to 0
|
||||
for (int i = 0; i < V; i++) vis[i] = false;
|
||||
int count_scc = 0;
|
||||
while (!st.empty())
|
||||
{
|
||||
while (!st.empty()) {
|
||||
int t = st.top();
|
||||
st.pop();
|
||||
if (vis[t] == false)
|
||||
{
|
||||
if (vis[t] == false) {
|
||||
dfs(t, vis, grev);
|
||||
count_scc++;
|
||||
}
|
||||
@@ -112,12 +100,10 @@ int kosaraju(int V, vector<int> adj[])
|
||||
|
||||
// All critical/corner cases have been taken care of.
|
||||
// Input your required values: (not hardcoded)
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--)
|
||||
{
|
||||
while (t--) {
|
||||
int a, b; // a->number of nodes, b->directed edges.
|
||||
cin >> a >> b;
|
||||
int m, n;
|
||||
|
||||
Reference in New Issue
Block a user