Advertisement
Guest User

Untitled

a guest
May 20th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <queue>
  6. #include <stdlib.h>
  7. #include <algorithm>
  8. #include <map>
  9.  
  10. using namespace std;
  11.  
  12. vector<string> getList(vector<string> names, vector<vector<bool>> relations, int startIndex) {
  13. vector<int> men;
  14. map<string, int> next;
  15. vector<string> output;
  16. next[names[startIndex]] = startIndex;
  17.  
  18. output.push_back(names[startIndex]);
  19.  
  20. int n = names.size();
  21. bool flag = true;
  22. while (flag)
  23. {
  24. flag = false;
  25. for (pair<string, int> var : next)
  26. {
  27. men.push_back(var.second);
  28. }
  29. next.clear();
  30. for (int j = 0; j < men.size(); j++)
  31. {
  32. for (int i = 0; i < n; i++)
  33. {
  34. if (relations[i][men[j]]) {
  35. bool f = true;
  36. for (int t = 0; t < men.size(); t++)
  37. {
  38. if (men[t] == i)
  39. f = false;
  40. }
  41. if (f) {
  42. flag = true;
  43. next[names[i]] = i;
  44. }
  45. relations[i][men[j]] = false;
  46. relations[men[j]][i] = false;
  47. }
  48. }
  49. }
  50. for (pair<string, int> var : next)
  51. {
  52. output.push_back(var.first);
  53. }
  54. }
  55. return output;
  56. }
  57.  
  58. int main()
  59. {
  60. vector<string> names = vector<string>();
  61. vector<vector<bool>> relations;
  62. int startIndex;
  63.  
  64. ifstream fin;
  65. fin.open("input.txt");
  66. if (fin.is_open())
  67. {
  68. string str = "";
  69. getline(fin, str);
  70.  
  71. while (str != "#")
  72. {
  73. names.emplace_back(str.substr(str.find(' ') + 1));
  74. getline(fin, str);
  75. }
  76.  
  77. relations = vector<vector<bool>>(names.size());
  78.  
  79. for (int i = 0; i < names.size(); i++)
  80. {
  81. relations[i] = vector<bool>(names.size());
  82. for (int j = 0; j < names.size(); j++)
  83. relations[i][j] = false;
  84. }
  85.  
  86. getline(fin, str);
  87.  
  88. while (str != "#")
  89. {
  90. int a = stoi(str.substr(0, str.find(' '))) - 1;
  91. int b = stoi(str.substr(str.find(' '))) - 1;
  92. relations[a][b] = true;
  93. relations[b][a] = true;
  94. getline(fin, str);
  95. }
  96.  
  97. fin >> startIndex;
  98.  
  99. fin.close();
  100. }
  101.  
  102. vector<string> res = getList(names, relations, startIndex - 1);
  103.  
  104. fstream fout;
  105. fout.open("output.txt", ios::out);
  106. for (int i = 0; i < res.size(); i++)
  107. fout << res[i] << "\n";
  108. fout.close();
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement