Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Исходный код
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <map>
- #include <string>
- #include <cstring>
- using namespace std;
- queue <int> q;
- bool mas[4000][4000];
- bool used[4000];
- map<int, string> f;
- map<string, int> of;
- vector<pair<string, string>> inVertex;
- vector <string> s;
- int n, v2, v1, i, j;
- void bfs(int v1) {
- cout << f.find(v1)->second << endl;
- q.push(v1);
- used[v1] = true;
- while(!q.empty())
- {
- int v = q.front();
- q.pop();
- for(int i = 1; i < s.size() + 1; ++i)
- if(!used[i] && mas[v][i])
- {
- used[i] = true;
- q.push(i);
- cout << f.find(i)->second << endl;
- }
- }
- }
- void dfs(int v1) {
- used[v1] = true;
- cout << f.find(v1)->second << endl;
- for(int i = 1; i <= s.size(); i++)
- if(mas[v1][i] && !used[i]) {
- dfs(i);
- }
- }
- int main() {
- char graph_type, search_type;
- string start;
- //ifstream in;
- //in.open("C:\\Users\\DNSPC\\Desktop\\input.txt");
- cin >> graph_type >> start >> search_type;
- string v1, v2;
- while (cin>>v1) {
- cin >> v2;
- inVertex.push_back(make_pair(v1,v2));
- if (find(s.begin(), s.end(), v1) == s.end()) s.push_back(v1);
- if (find(s.begin(), s.end(), v2) == s.end()) s.push_back(v2);
- }
- sort(s.begin(), s.end());
- for (i = 1; i <= s.size(); i++) {
- f.insert(make_pair(i, s[i - 1]));
- of.insert(make_pair(s[i - 1], i));
- }
- for (i = 0; i < inVertex.size(); i++) {
- int ind1, ind2;
- ind1 = of.find(inVertex[i].first)->second;
- ind2 = of.find(inVertex[i].second)->second;
- mas[ind1][ind2] = 1;
- if (graph_type == 'u') {
- mas[ind2][ind1] = 1;
- }
- }
- if (search_type == 'd')
- dfs(of.find(start)->second);
- else bfs(of.find(start)->second);
- //in.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement