Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Wave.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <vector>
- #include <queue>
- #include <iostream>
- #include <fstream>
- #include <map>
- #include <string>
- using namespace std;
- using graph = vector <vector<int>>;
- using graph2 = map<string, vector<string>>;
- void BFS(graph &G, int s, vector<int> &d, vector<int> &p, bool reset_data=true) {
- int N = G.size();
- if (reset_data){
- d = vector<int>(N, INT_MAX);
- p = vector<int>(N, -1);
- d[s] = 0; }
- queue<int> Q;
- Q.push(s);
- while (Q.size())
- {
- auto u = Q.front(); Q.pop();
- for(auto v:G[u])
- if (d[v]==INT_MAX)
- {
- d[v] = d[u] + 1;
- p[v] = u;
- Q.push(v);
- }
- }
- }
- void BFS2(graph2 &G, string s, map<string,int> &d) {
- bool isenFind = false;
- for (auto &i : G) {
- d[i.first] = INT_MAX;
- if()
- }
- d[s] = 0;
- queue<string> Q;
- Q.push(s);
- while (Q.size())
- {
- auto u = Q.front(); Q.pop();
- for (auto v : G[u])
- if (d[v] == INT_MAX)
- {
- d[v] = d[u] + 1;
- Q.push(v);
- }
- }
- }
- graph GetGraphFromStream(ifstream inp) {
- int N;
- inp >> N;
- graph G(N);
- while (!inp.eof()) {
- int a, b = -1;
- inp >> a >> b;
- if (b == -1)continue;
- G[a].push_back(b);
- G[b].push_back(a);
- }
- return G;
- }
- graph2 GetGraphFromStream2(istream inp) {
- int N;
- inp >> N;
- graph2 G;
- for (int i = 0; i < N;i++) {
- string a, b, c;
- inp >> a >> b>>c;
- G[a].push_back(b);
- G[b].push_back(c);
- G[a].push_back(a);
- G[b].push_back(c);
- G[a].push_back(a);
- G[b].push_back(b);
- }
- return G;
- }
- vector<int> GetPathTo(int dest, vector<int> &p) {
- vector<int> path;
- int u = p[dest];
- while (u!= -1) {
- path.push_back(u);
- u = p[u];
- }
- if (path.size())
- {
- reverse(path.begin(), path.end());
- path.push_back(dest);
- }
- return path;
- }
- bool IsconnectedGraph(graph &G) {
- vector<int> d, p;
- BFS (G, 0, d, p);
- return find(d.begin(), d.end(), INT_MAX) == d.end();
- }
- int CountConnectedParts(graph &G) {
- int count = 0;
- vector<int> d, p;
- int N = G.size();
- d= vector<int>(N, INT_MAX);
- p = vector<int>(N, -1);
- for (size_t i = 0; i < d.size(); i++) {
- if (d[i] == INT_MAX) {
- BFS(G, i, d, p, 0);
- count++;
- }
- }
- return count;
- }
- bool IsBiGraph(graph &G) {;
- vector<int> d, p;
- int N = G.size();
- d = vector<int>(N, INT_MAX);
- p = vector<int>(N, -1);
- for (size_t i = 0; i < d.size(); i++) {
- if (d[i] == INT_MAX) {
- d[i] = 0;
- queue<int> Q;
- Q.push(i);
- while (Q.size())
- {
- auto u = Q.front(); Q.pop();
- for (auto v : G[u])
- if (d[v] == INT_MAX)
- {
- d[v] = d[u] + 1;
- p[v] = u;
- Q.push(v);
- }
- else {
- if ((d[v] + d[u]) % 2 == 0)
- return false;
- }
- }
- }
- }
- return true;
- }
- int main()
- {
- auto G = GetGraphFromStream2(cin);
- map<string, int> d;
- BFS2(G, "Isenbaev", d);
- for (auto i : d) {
- if(i.second ==INT_MAX)
- cout << i.first << " " << "undefined" << endl;
- else
- cout << i.first <<" "<<i.second<< endl;
- }
- /*auto G = GetGraphFromStream(ifstream("graph.txt"));
- cout << IsconnectedGraph(G) << endl;
- vector<int> d, p;
- BFS(G, 0, d, p);
- for (size_t i = 0; i < d.size();i++) {
- cout << i << " "<<d[i]<<" " << p[i] << endl;;
- }
- auto path = GetPathTo(14, p);
- for (auto u : path)
- cout << u << " ";
- cout << endl;
- cout << CountConnectedParts(G) << endl;
- cout << IsBiGraph(G) << endl;*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement