Advertisement
Zipton

Untitled

Mar 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. // Wave.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <queue>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <map>
  10. #include <string>
  11.  
  12. using namespace std;
  13. using graph = vector <vector<int>>;
  14. using graph2 = map<string, vector<string>>;
  15.  
  16. void BFS(graph &G, int s, vector<int> &d, vector<int> &p, bool reset_data=true) {
  17.     int N = G.size();
  18.     if (reset_data){
  19.     d = vector<int>(N, INT_MAX);
  20.     p = vector<int>(N, -1);
  21.     d[s] = 0; }
  22.     queue<int> Q;
  23.     Q.push(s);
  24.     while (Q.size())
  25.     {
  26.         auto u = Q.front(); Q.pop();
  27.         for(auto v:G[u])
  28.             if (d[v]==INT_MAX)
  29.             {
  30.                 d[v] = d[u] + 1;
  31.                 p[v] = u;
  32.                 Q.push(v);
  33.             }
  34.     }
  35. }
  36.  
  37. void BFS2(graph2 &G, string s, map<string,int> &d) {
  38.     bool isenFind = false;
  39.     for (auto &i : G) {
  40.             d[i.first] = INT_MAX;
  41.             if()
  42.         }
  43.         d[s] = 0;
  44.     queue<string> Q;
  45.     Q.push(s);
  46.     while (Q.size())
  47.     {
  48.         auto u = Q.front(); Q.pop();
  49.         for (auto v : G[u])
  50.             if (d[v] == INT_MAX)
  51.             {
  52.                 d[v] = d[u] + 1;
  53.                 Q.push(v);
  54.             }
  55.     }
  56. }
  57.  
  58. graph GetGraphFromStream(ifstream inp) {
  59.     int N;
  60.     inp >> N;
  61.     graph G(N);
  62.     while (!inp.eof()) {
  63.         int a, b = -1;
  64.         inp >> a >> b;
  65.         if (b == -1)continue;
  66.         G[a].push_back(b);
  67.         G[b].push_back(a);
  68.     }
  69.     return G;
  70. }
  71.  
  72. graph2 GetGraphFromStream2(istream inp) {
  73.     int N;
  74.     inp >> N;
  75.     graph2 G;
  76.     for (int i = 0; i < N;i++) {
  77.         string a, b, c;
  78.         inp >> a >> b>>c;
  79.         G[a].push_back(b);
  80.         G[b].push_back(c);
  81.         G[a].push_back(a);
  82.         G[b].push_back(c);
  83.         G[a].push_back(a);
  84.         G[b].push_back(b);
  85.  
  86.     }
  87.     return G;
  88. }
  89.  
  90. vector<int> GetPathTo(int dest, vector<int> &p) {
  91.     vector<int> path;
  92.     int u = p[dest];
  93.     while (u!= -1) {
  94.         path.push_back(u);
  95.         u = p[u];
  96.     }
  97.     if (path.size())
  98.     {
  99.         reverse(path.begin(), path.end());
  100.         path.push_back(dest);
  101.     }
  102.     return path;
  103. }
  104.  
  105. bool IsconnectedGraph(graph &G) {
  106.     vector<int> d, p;
  107.     BFS (G, 0, d, p);
  108.     return find(d.begin(), d.end(), INT_MAX) == d.end();
  109.  
  110. }
  111.  
  112. int CountConnectedParts(graph &G) {
  113.     int count = 0;
  114.     vector<int> d, p;
  115.     int N = G.size();
  116.     d= vector<int>(N, INT_MAX);
  117.     p = vector<int>(N, -1);
  118.     for (size_t i = 0; i < d.size(); i++) {
  119.         if (d[i] == INT_MAX) {
  120.             BFS(G, i, d, p, 0);
  121.             count++;
  122.         }
  123.     }
  124.     return count;
  125. }
  126.  
  127. bool IsBiGraph(graph &G) {;
  128.     vector<int> d, p;
  129.     int N = G.size();
  130.     d = vector<int>(N, INT_MAX);
  131.     p = vector<int>(N, -1);
  132.     for (size_t i = 0; i < d.size(); i++) {
  133.         if (d[i] == INT_MAX) {
  134.             d[i] = 0;
  135.             queue<int> Q;
  136.             Q.push(i);
  137.             while (Q.size())
  138.             {
  139.                 auto u = Q.front(); Q.pop();
  140.                 for (auto v : G[u])
  141.                     if (d[v] == INT_MAX)
  142.                     {
  143.                         d[v] = d[u] + 1;
  144.                         p[v] = u;
  145.                         Q.push(v);
  146.                     }
  147.                     else {
  148.                         if ((d[v] + d[u]) % 2 == 0)
  149.                             return false;
  150.                     }
  151.             }
  152.            
  153.         }
  154.     }
  155.     return true;
  156. }
  157.  
  158. int main()
  159. {
  160.     auto G = GetGraphFromStream2(cin);
  161.     map<string, int> d;
  162.     BFS2(G, "Isenbaev", d);
  163.     for (auto i : d) {
  164.         if(i.second ==INT_MAX)
  165.             cout << i.first << " " << "undefined" << endl;
  166.         else
  167.         cout << i.first <<" "<<i.second<< endl;
  168.         }
  169.  
  170.  
  171.     /*auto G = GetGraphFromStream(ifstream("graph.txt"));
  172.     cout << IsconnectedGraph(G) << endl;
  173.     vector<int> d, p;
  174.     BFS(G, 0, d, p);
  175.     for (size_t i = 0; i < d.size();i++) {
  176.         cout << i << " "<<d[i]<<" " << p[i] << endl;;
  177.     }  
  178.     auto path = GetPathTo(14, p);
  179.     for (auto u : path)
  180.         cout << u << " ";
  181.     cout << endl;
  182.     cout << CountConnectedParts(G) << endl;
  183.     cout << IsBiGraph(G) << endl;*/
  184.  
  185.  
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement