Advertisement
Guest User

Untitled

a guest
May 27th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stack>
  6. #include <set>
  7. #include <algorithm>
  8. #include <list>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. vector<string> names;
  14. vector<vector<string>> result;
  15.  
  16. bool compare_groups(const vector<string> &a, const vector<string> &b) {
  17.     return a.front() < b.front();
  18. }
  19.  
  20.  
  21. void strong_connect(int curr, vector<vector<bool>> &relations, int *pre, int *low, bool *inStack, stack<int> &st,
  22.                     int &index) {
  23.     pre[curr] = index;
  24.     low[curr] = index;
  25.    
  26.     index++;
  27.    
  28.     st.push(curr);
  29.     inStack[curr] = true;
  30.    
  31.     for (int child = 0; child < relations.size(); ++child) {
  32.         if (!relations[curr][child])
  33.             continue;
  34.        
  35.         if (pre[child] == -1) {
  36.             strong_connect(child, relations, pre, low, inStack, st, index);
  37.             low[curr] = min(low[curr], low[child]);
  38.         } else {
  39.             if (inStack[child]) {
  40.                 low[curr] = min(low[curr], pre[child]);
  41.             }
  42.         }
  43.     }
  44.    
  45.     if (low[curr] == pre[curr]) {
  46.         vector<string> group;
  47.         int node;
  48.         do {
  49.             node = st.top();
  50.             inStack[node] = false;
  51.             st.pop();
  52.             group.push_back(names[node]);
  53.         } while (node != curr);
  54.        
  55.         sort(group.begin(), group.end());
  56.         result.push_back(group);
  57.     }
  58. }
  59.  
  60. // алгоритм тарьяна
  61. void getList(vector<vector<bool>> &relations) {
  62.    
  63.     int *pre = new int[relations.size()];
  64.     int *low = new int[relations.size()];
  65.     bool *inStack = new bool[relations.size()];
  66.    
  67.     for (int i = 0; i < relations.size(); ++i) {
  68.         // -1 значит непосещённая
  69.         pre[i] = -1;
  70.         low[i] = -1;
  71.         inStack[i] = false;
  72.     }
  73.    
  74.     int index = 0;
  75.     stack<int> st;
  76.    
  77.     for (int i = 0; i < relations.size(); ++i) {
  78.         if (pre[i] == -1) {
  79.             strong_connect(i, relations, pre, low, inStack, st, index);
  80.         }
  81.     }
  82.    
  83.     sort(result.begin(), result.end(), compare_groups);
  84.    
  85.     delete[] pre;
  86.     delete[] low;
  87.     delete[] inStack;
  88. }
  89.  
  90.  
  91. int main() {
  92. //    vector<string> names = vector<string>();
  93.     vector<vector<bool>> relations;
  94.     int startIndex;
  95.    
  96.     ifstream fin;
  97.     fin.open("input.txt");
  98.     if (fin.is_open()) {
  99.         string str = "";
  100.         getline(fin, str);
  101.        
  102.         while (str != "#") {
  103.             names.emplace_back(str.substr(str.find(' ') + 1));
  104.             getline(fin, str);
  105.         }
  106.        
  107.         relations = vector<vector<bool>>(names.size());
  108.        
  109.         for (int i = 0; i < names.size(); i++) {
  110.             relations[i] = vector<bool>(names.size());
  111.             for (int j = 0; j < names.size(); j++)
  112.                 relations[i][j] = false;
  113.         }
  114.        
  115.         getline(fin, str);
  116.        
  117.         while (fin) {
  118.             int a = stoi(str.substr(0, str.find(' '))) - 1;
  119.             int b = stoi(str.substr(str.find(' '))) - 1;
  120.             relations[a][b] = true;
  121.             getline(fin, str);
  122.         }
  123.        
  124.         fin.close();
  125.     }
  126.    
  127.     result = vector<vector<string>>();
  128.     getList(relations);
  129.    
  130.     fstream fout;
  131.     fout.open("output.txt", ios::out);
  132.     for (int i = 0; i < result.size(); i++) {
  133.         for (int j = 0; j < result[i].size(); j++)
  134.             fout << result[i][j] << "\n";
  135.         fout << "\n";
  136.     }
  137.     fout.close();
  138.    
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement