Advertisement
Guest User

govno

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