Advertisement
tron24

SCC

May 25th, 2021
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. // { Driver Code Starts
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  // } Driver Code Ends
  6.  
  7.  
  8.  
  9. class Solution
  10. {
  11.     private:
  12.     vector<bool> visited;
  13.     stack<int> st;
  14.    
  15.     void dfs(int u, vector<int> adj[])
  16.     {  
  17.         visited[u] = 1;
  18.         for (auto v : adj[u])
  19.         {
  20.             if (!visited[v])
  21.             {
  22.                 dfs(v, adj);
  23.             }
  24.         }
  25.         st.push(u);
  26.     }
  27.    
  28.     void dfs2(int u, vector<int> adj[]){
  29.         visited[u]=1;
  30.        
  31.         for(auto v:adj[u]){
  32.             if(!visited[v]){
  33.                 dfs2(v, adj);
  34.             }
  35.         }
  36.     }
  37.    
  38.     // vector<vector<int>> reverseGraph(vector<int> adj[], int n)
  39.     // {
  40.        // vector<vector<int>> rev(n);
  41.        // for (int i = 0; i < n; i++)
  42.        // {
  43.           //  for (auto v : adj[i])
  44.           //  {
  45.              //   rev[v].push_back(i);
  46.           //  }
  47.        // }
  48.        // return rev;
  49.     // }
  50.    
  51.    
  52.    
  53.     public:
  54.     //Function to find number of strongly connected components in the graph.
  55.     int kosaraju(int V, vector<int> adj[])
  56.     {
  57.         //code here
  58.         int n = V, ans = 0;
  59.         visited.assign(n, false);
  60.         for (int i = 0; i < n; i++)
  61.         {
  62.             if (!visited[i])
  63.             {
  64.                 dfs(i, adj);
  65.             }
  66.         }
  67.        
  68.         visited.assign(n, false);
  69.        // auto revAdj = reverseGraph(adj, n);
  70.        
  71.        vector<int> revAdj[n];
  72.        
  73.        for(int i=0;i<n;i++){
  74.            for(auto v: adj[i]){
  75.                revAdj[v].push_back(i);
  76.            }
  77.        }
  78.        
  79.         while(!st.empty()){
  80.             int s = st.top();
  81.             st.pop();
  82.            
  83.             if(!visited[s]){
  84.                 ans++;
  85.                 for(auto v: revAdj[s]){
  86.                     if(!visited[v]){
  87.                         dfs2(v, revAdj);
  88.                     }
  89.                 }
  90.             }
  91.            
  92.         }
  93.        
  94.         return ans;
  95.     }
  96. };
  97.  
  98. // { Driver Code Starts.
  99.  
  100.  
  101. int main()
  102. {
  103.    
  104.     int t;
  105.     cin >> t;
  106.     while(t--)
  107.     {
  108.         int V, E;
  109.         cin >> V >> E;
  110.  
  111.         vector<int> adj[V];
  112.  
  113.         for(int i = 0; i < E; i++)
  114.         {
  115.             int u, v;
  116.             cin >> u >> v;
  117.             adj[u].push_back(v);
  118.         }
  119.  
  120.         Solution obj;
  121.         cout << obj.kosaraju(V, adj) << "\n";
  122.     }
  123.  
  124.     return 0;
  125. }
  126.  
  127.   // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement