Advertisement
purxiz

Untitled

Nov 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. void DFS(int ** G, int v, int vertices);
  8. void DFS2(int ** G, int v, int vertices, vector<int> *);
  9. bool * vfound;
  10. int ** graph;
  11. stack<int> F;
  12. vector<vector<int> *> scc;
  13. int main () {
  14.   int vertices, edges;
  15.  
  16.   cin >> vertices;
  17.   cin >> edges;
  18.   vfound = new bool[vertices];
  19.   for (int i = 0; i < vertices; i++) {
  20.     vfound[i] = false;
  21.   }
  22.   graph = new int *[vertices];
  23.   for (int i = 0; i < vertices; i++) {
  24.     graph[i] = new int[vertices];
  25.   }
  26.   for (int i = 0; i < vertices; i++) {
  27.     for (int j = 0; j < vertices; j++) {
  28.       graph[i][j] = 0;
  29.     }
  30.   }
  31.   int input1, input2;
  32.   for (int i = 0; i < edges; i++) {
  33.     //cout << "Accepting input " << i << "th edge" << endl;
  34.     cin >> input1;
  35.     cin >> input2;
  36.     graph[input1][input2] = 1;
  37.   }
  38.   for(int i = 0; i < vertices; i++){
  39.     for(int j = 0; j < vertices; j++){
  40.       //cout << graph[i][j] << " ";
  41.     }
  42.     //cout << endl;
  43.   }
  44.   while(true){
  45.     int allfound = -1;
  46.     for(int i = vertices - 1; i > -1; i--){
  47.       if(!vfound[i]){
  48.         allfound = i;
  49.       }
  50.     }
  51.     //cout << "got here!" << allfound << endl;
  52.     if(allfound == -1){
  53.       break;
  54.     }
  55.     //cout << "calling DFS1 for " << allfound << endl;
  56.     DFS(graph, allfound, vertices);
  57.   }
  58.   int ** graphT = new int *[vertices];
  59.   for (int i = 0; i < vertices; i++) {
  60.     graphT[i] = new int[vertices];
  61.   }
  62.   for (int i = 0; i < vertices; i++) {
  63.     for (int j = 0; j < vertices; j++) {
  64.       graphT[i][j] = graph[j][i];
  65.     }
  66.   }
  67.   for (int i = 0; i < vertices; i++) {
  68.     vfound[i] = false;
  69.   }
  70.   int i = 0;
  71.   while (!F.empty()) {
  72.     //cout << "calling DFS for " << F.top() << endl;
  73.     if (!vfound[F.top()]) {
  74.       //cout << "It was not already found" << endl;
  75.       scc.push_back(new vector<int>);
  76.       DFS2(graphT, F.top(), vertices, scc[i]);
  77.       i++;
  78.     }
  79.     F.pop();
  80.   }
  81.   //cout << "scc list: ";
  82.   for(int k = 0; k < scc.size(); k++){
  83.     for(int l = 0; l < scc[k]->size(); l++){
  84.       //cout << scc[k]->at(l) << " ";
  85.     }
  86.   }
  87.   //cout << endl;
  88.   for (int j = 0; j < vertices; j++) {
  89.     //cout << "j is " << j << endl;
  90.     for (int k = 0; k < scc.size(); k++) {
  91.       if (find(scc[k]->begin(), scc[k]->end(), j) != scc[k]->end()) {
  92.         int min = 40000;
  93.         for(int l = 0; l < scc[k]->size(); l++){
  94.           if(scc[k]->at(l) < min){
  95.             min = scc[k]->at(l);
  96.           }
  97.         }
  98.         cout << min << endl;
  99.       }
  100.     }
  101.   }
  102. }
  103.  
  104. void DFS (int ** G, int v, int vertices) {
  105.   if (!vfound[v]) {
  106.     vfound[v] = true;
  107.     for (int i = 0; i < vertices; i++) {
  108.       if (graph[v][i] == 1) {
  109.         DFS(G, i, vertices);
  110.       }
  111.     }
  112.     F.push(v);
  113.   }
  114. }
  115.  
  116. void DFS2 (int ** G, int v, int vertices, vector<int> * scc) {
  117.   if (!vfound[v]) {
  118.     vfound[v] = true;
  119.     for (int i = 0; i < vertices; i++) {
  120.       if (G[v][i] == 1) {
  121.         DFS2(G, i, vertices, scc);
  122.       }
  123.     }
  124.     //cout << "adding " << v << " to scc" << endl;
  125.     scc->push_back(v);
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement