Advertisement
Guest User

Untitled

a guest
May 27th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 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.  
  10. using namespace std;
  11.  
  12. void go(int curr, const vector<string> &names, const vector<vector<bool>> &relations, vector<int> &used, bool *visited,
  13.         bool transposed) {
  14. //    printf("in %d\n", curr + 1);
  15.     visited[curr] = true;
  16.     vector<int> children;
  17.    
  18.     for (int i = 0; i < relations.size(); ++i) {
  19.         bool arrow = transposed ? relations[i][curr] : relations[curr][i];
  20.        
  21.         if (arrow && !visited[i])
  22.             children.push_back(i);
  23.     }
  24.    
  25.     // sort children by name
  26.     sort(children.begin(), children.end(), [names](int a, int b) {
  27.         return names[a] < names[b];
  28.     });
  29.    
  30.     for (int child : children) {
  31.         if (!visited[child])
  32.             go(child, names, relations, used, visited, transposed);
  33.     }
  34.     used.push_back(curr);
  35. //    printf("after %d\n", curr + 1);
  36. }
  37.  
  38.  
  39. vector<vector<string>> getList(vector<string> &names, vector<vector<bool>> &relations) {
  40. //    setlocale(LC_ALL, "rus");
  41.    
  42.     // find sources of graph
  43.     vector<int> sources;
  44.    
  45.     for (int i = 0; i < relations.size(); ++i) {
  46.         // has incoming edges
  47.         bool hasIns = false;
  48.        
  49.         for (int j = 0; j < relations.size(); ++j) {
  50.             // check column i
  51.             if (relations[j][i]) {
  52.                 hasIns = true;
  53.                 break;
  54.             }
  55.         }
  56.         if (!hasIns)
  57.             sources.push_back(i);
  58.     }
  59. //    printf("sources:\n");
  60. //    for (auto &&source : sources) {
  61. //        printf("%d %s\n",source + 1,  names[source].c_str());
  62. //    }
  63.    
  64.     // sort sources by name
  65.     sort(sources.begin(), sources.end(), [names](int a, int b) {
  66.         return names[a] < names[b];
  67.     });
  68.    
  69.     bool *visited = new bool[relations.size()];
  70.     for (int i = 0; i < relations.size(); ++i) {
  71.         visited[i] = false;
  72.     }
  73.     // nodes, sorted by post-value
  74.     vector<int> used;
  75.     for (int source : sources) {
  76.         go(source, names, relations, used, visited, false);
  77.     }
  78.    
  79.     reverse(used.begin(), used.end());
  80.    
  81.     // reset visited
  82.     for (int i = 0; i < relations.size(); ++i) {
  83.         visited[i] = false;
  84.     }
  85.     vector<vector<string>> ans;
  86.    
  87.     // iterate in reversed post-value order
  88.     for (int node : used) {
  89.         if (visited[node])
  90.             continue;
  91.        
  92.         vector<int> intGroup;
  93.         go(node, names, relations, intGroup, visited, true);
  94.        
  95.         vector<string> stringGroup;
  96.        
  97.         for (int elInGroup : intGroup) {
  98.             stringGroup.push_back(names[elInGroup]);
  99.         }
  100.        
  101.         // sort inside one group
  102.         sort(stringGroup.begin(), stringGroup.end());
  103.        
  104.         ans.push_back(stringGroup);
  105.     }
  106.     // sort groups by first name
  107.     sort(ans.begin(), ans.end(), [](vector<string>& a, vector<string>& b) {
  108.         return a.front() < b.front();
  109.     });
  110.    
  111.     delete[] visited;
  112.     return ans;
  113. }
  114.  
  115. int main() {
  116.     vector<string> names = vector<string>();
  117.     vector<vector<bool>> relations;
  118.     int startIndex;
  119.    
  120.     ifstream fin;
  121.     fin.open("input.txt");
  122.     if (fin.is_open()) {
  123.         string str = "";
  124.         getline(fin, str);
  125.        
  126.         while (str != "#") {
  127.             names.emplace_back(str.substr(str.find(' ') + 1));
  128.             getline(fin, str);
  129.         }
  130.        
  131.         relations = vector<vector<bool>>(names.size());
  132.        
  133.         for (int i = 0; i < names.size(); i++) {
  134.             relations[i] = vector<bool>(names.size());
  135.             for (int j = 0; j < names.size(); j++)
  136.                 relations[i][j] = false;
  137.         }
  138.        
  139.         getline(fin, str);
  140.        
  141.         while (fin) {
  142.             int a = stoi(str.substr(0, str.find(' '))) - 1;
  143.             int b = stoi(str.substr(str.find(' '))) - 1;
  144.             relations[a][b] = true;
  145.             getline(fin, str);
  146.         }
  147.        
  148.         fin.close();
  149.     }
  150.    
  151.     vector<vector<string>> res = getList(names, relations);
  152.    
  153.     fstream fout;
  154.     fout.open("output.txt", ios::out);
  155.     for (int i = 0; i < res.size(); i++) {
  156.         for (int j = 0; j < res[i].size(); j++)
  157.             fout << res[i][j] << "\n";
  158.         fout << "\n";
  159.     }
  160.     fout.close();
  161.    
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement