Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <queue>
- #include <stdlib.h>
- #include <algorithm>
- #include <map>
- using namespace std;
- vector<string> getList(vector<string> names, vector<vector<bool>> relations, int startIndex) {
- vector<int> men;
- map<string, int> next;
- vector<string> output;
- next[names[startIndex]] = startIndex;
- output.push_back(names[startIndex]);
- int n = names.size();
- bool flag = true;
- while (flag)
- {
- flag = false;
- for (pair<string, int> var : next)
- {
- men.push_back(var.second);
- }
- next.clear();
- for (int j = 0; j < men.size(); j++)
- {
- for (int i = 0; i < n; i++)
- {
- if (relations[i][men[j]]) {
- bool f = true;
- for (int t = 0; t < men.size(); t++)
- {
- if (men[t] == i)
- f = false;
- }
- if (f) {
- flag = true;
- next[names[i]] = i;
- }
- relations[i][men[j]] = false;
- relations[men[j]][i] = false;
- }
- }
- }
- for (pair<string, int> var : next)
- {
- output.push_back(var.first);
- }
- }
- return output;
- }
- int main()
- {
- vector<string> names = vector<string>();
- vector<vector<bool>> relations;
- int startIndex;
- ifstream fin;
- fin.open("input.txt");
- if (fin.is_open())
- {
- string str = "";
- getline(fin, str);
- while (str != "#")
- {
- names.emplace_back(str.substr(str.find(' ') + 1));
- getline(fin, str);
- }
- relations = vector<vector<bool>>(names.size());
- for (int i = 0; i < names.size(); i++)
- {
- relations[i] = vector<bool>(names.size());
- for (int j = 0; j < names.size(); j++)
- relations[i][j] = false;
- }
- getline(fin, str);
- while (str != "#")
- {
- int a = stoi(str.substr(0, str.find(' '))) - 1;
- int b = stoi(str.substr(str.find(' '))) - 1;
- relations[a][b] = true;
- relations[b][a] = true;
- getline(fin, str);
- }
- fin >> startIndex;
- fin.close();
- }
- vector<string> res = getList(names, relations, startIndex - 1);
- fstream fout;
- fout.open("output.txt", ios::out);
- for (int i = 0; i < res.size(); i++)
- fout << res[i] << "\n";
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement