Advertisement
Guest User

memes

a guest
Feb 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <unordered_set>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cassert>
  5. #include <queue>
  6.  
  7. using std::unordered_set;
  8. using std::vector;
  9. using std::queue;
  10. using std::cout;
  11. using std::endl;
  12. using std::pair;
  13. using std::cin;
  14.  
  15.  
  16.  
  17. class SetGraph {
  18. public:
  19.     explicit SetGraph(unsigned _verticesCount);
  20.  
  21.     virtual void addEdge(int from, int to);
  22.  
  23.     virtual unsigned getVerticesCount() const { return verticesCount; }
  24.  
  25.     virtual void getNextVertices(int vertex, std::vector<int> &vertices) const;
  26.     virtual void getPrevVertices(int vertex, std::vector<int> &vertices) const;
  27.  
  28. private:
  29.     unsigned verticesCount;
  30.     vector<unordered_set<int>> table;
  31. };
  32.  
  33.  
  34.  
  35. // BFS-based function, returns the length of the first found cycle
  36. int searchCycle(SetGraph const &graph, int b)
  37. {
  38.     // "q" is a service queue for BFS
  39.     queue<int> q;
  40.    
  41.     // "used[i]" stores "true" if vertex "i" is "grey" or "black" now
  42.     vector<bool> used(graph.getVerticesCount(), false);
  43.    
  44.     // "depths[i]" stores the depth of first discovering for vertex "i"
  45.     vector<int> depths(graph.getVerticesCount(), 0);
  46.    
  47.     // configuring parameters for the start vertex
  48.     q.push(b);
  49.     used[b] = true;
  50.     depths[b] = 0;
  51.  
  52.     while(!q.empty()) {
  53.  
  54.         // taking front vertex from the queue
  55.         int cur = q.front();
  56.         int depth = depths[cur] + 1;
  57.         q.pop();
  58.  
  59.         vector<int> vertices;
  60.         graph.getNextVertices(cur, vertices);
  61.  
  62.         // iterating over the neighbours of the "cur" vertex
  63.         for (int i : vertices) {
  64.             if (used[i] == true && depths[i] >= depth - 1) {
  65.                 int len = depth + depths[i];
  66.                 return len ;
  67.             }
  68.             else if (!used[i]) {
  69.                 q.push(i);
  70.                 used[i] = true;
  71.                 depths[i] = depth;
  72.             }
  73.         }
  74.  
  75.  
  76.     }
  77.     return INFINITY;
  78. }
  79.  
  80.  
  81.  
  82. /*============================================================================*/
  83. int main()
  84. {
  85.     // input
  86.     int points, edges;
  87.     scanf("%d%d",&points,&edges);
  88.     SetGraph graph(points);
  89.  
  90.     for (int i = 0; i < edges; ++i) {
  91.         int b, e;
  92.         scanf("%d%d",&b,&e);
  93.         graph.addEdge(b, e);
  94.     }
  95.  
  96.     // setting "minLen" value higher than really possible
  97.     int minLen = points + 1;
  98.  
  99.     // running the search of cycles from every vertex
  100.     for (int i = 0; i < graph.getVerticesCount(); ++i) {
  101.         int len = searchCycle(graph, i);
  102.         if (minLen > len) minLen = len;
  103.     }
  104.  
  105.     // if "minLen" value hasn't changed there are no cycles in the graph
  106.     if (minLen == points + 1) minLen = -1;
  107.    
  108.     // printing of the result
  109.     cout  << minLen;
  110.  
  111.     return 0;
  112. }
  113.  
  114.  
  115. /*============================================================================*/
  116. SetGraph::SetGraph(unsigned _verticesCount) :
  117.         verticesCount(_verticesCount),
  118.         table(_verticesCount)
  119. {}
  120.  
  121.  
  122.  
  123. void SetGraph::addEdge(int from, int to)
  124. {
  125.     assert( from >= 0 && from < verticesCount );
  126.     assert( to >= 0 && to < verticesCount );
  127.     assert( from != to );
  128.  
  129.     assert( table[from].find(to) == table[from].end() );
  130.     assert( table[to].find(from) == table[to].end() );
  131.  
  132.  
  133.     table[from].insert(to);
  134.     table[to].insert(from);
  135. }
  136.  
  137.  
  138.  
  139. void SetGraph::getNextVertices(int vertex, std::vector<int> &vertices) const
  140. {
  141.     assert( vertex < verticesCount);
  142.  
  143.     for (unordered_set<int>::const_iterator it = table[vertex].begin();
  144.          it !=  table[vertex].end();
  145.          ++it)
  146.     {
  147.         vertices.push_back(*it);
  148.     }
  149. }
  150.  
  151.  
  152.  
  153. void SetGraph::getPrevVertices(int vertex, std::vector<int> &vertices) const
  154. {
  155.     assert( vertex < verticesCount);
  156.  
  157.     for (int i = 0; i < verticesCount; ++i) {
  158.         if (table[i].find(vertex) != table[i].end()) {
  159.             vertices.push_back(i);
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement