Advertisement
smatskevich

Seminar6

Mar 17th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. // Graph.hpp
  2. #pragma once
  3.  
  4. #include <vector>
  5.  
  6. struct IGraph {
  7.   virtual ~IGraph() {}
  8.  
  9.   // Добавление ребра от from к to.
  10.   virtual void AddEdge(int from, int to) = 0;
  11.  
  12.   virtual size_t VerticesCount() const  = 0;
  13.  
  14.   virtual std::vector<int> FindAllAdjacentIn(int vertex) const = 0;
  15.   virtual std::vector<int> FindAllAdjacentOut(int vertex) const = 0;
  16. };
  17.  
  18.  
  19. // ListGraph.hpp
  20. #pragma once
  21.  
  22. #include "Graph.hpp"
  23.  
  24. class ListGraph : public IGraph {
  25.  public:
  26.   explicit ListGraph(size_t vertices_count);
  27.  
  28.   // Добавление ребра от from к to.
  29.   void AddEdge(int from, int to) override;
  30.  
  31.   size_t VerticesCount() const override { return childs_.size(); }
  32.  
  33.   std::vector<int> FindAllAdjacentIn(int vertex) const override;
  34.   std::vector<int> FindAllAdjacentOut(int vertex) const override;
  35.  
  36.  private:
  37.   // Списки смежности
  38.   std::vector<std::vector<int>> childs_;  // Исходящие ребра
  39.   std::vector<std::vector<int>> parents_;  // Входящие
  40. };
  41.  
  42.  
  43. // ListGraph.cpp
  44. #include "ListGraph.hpp"
  45.  
  46. ListGraph::ListGraph(size_t vertices_count) : childs_(vertices_count), parents_(vertices_count) {}
  47.  
  48. void ListGraph::AddEdge(int from, int to) {
  49.   childs_[from].push_back(to);
  50.   parents_[to].push_back(from);
  51. }
  52.  
  53. std::vector<int> ListGraph::FindAllAdjacentIn(int vertex) const {
  54.   return parents_[vertex];
  55. }
  56.  
  57. std::vector<int> ListGraph::FindAllAdjacentOut(int vertex) const {
  58.   return childs_[vertex];
  59. }
  60.  
  61.  
  62. // main.cpp
  63. #include <iostream>
  64. #include <vector>
  65.  
  66. #include "ListGraph.hpp"
  67.  
  68. class Furniture {
  69.  public:
  70.   virtual int Calories() const = 0;
  71. };
  72.  
  73. class Sofa : public Furniture {
  74.  public:
  75.   int Calories() const final { return 1000000; }
  76. };
  77.  
  78. class IronChair : public Furniture {
  79.  public:
  80.   int Calories() const final { return 0; }
  81. };
  82.  
  83. void Burn(Furniture& f) {
  84.   std::cout << "It's fire. Your room was heated on " << f.Calories() << " cals." << std::endl;
  85. }
  86.  
  87. int main1() {
  88.   std::vector<Furniture*> furnitures;
  89.   Sofa s;
  90.   furnitures.push_back(&s);
  91.   IronChair i;
  92.   furnitures.push_back(&i);
  93.   for (auto& f : furnitures) Burn(*f);
  94.  
  95.   IronChair x;
  96.   Burn(x);
  97.   return 0;
  98. }
  99.  
  100. void DFS(int v, const IGraph& graph, std::vector<bool>& visited,
  101.          std::vector<std::pair<int, int>>& times, int& time) {
  102.   times[v].first = time;
  103.  
  104.   visited[v] = true;
  105.   std::cout << v << " ";
  106.   for (int x : graph.FindAllAdjacentOut(v)) {
  107.     if (!visited[x]) {
  108.       DFS(x, graph, visited, times, time);
  109.     }
  110.   }
  111.  
  112.   times[v].second = ++time;
  113. }
  114.  
  115. std::vector<std::pair<int, int>> DFS(const IGraph& g) {
  116.   int time = 0;
  117.   std::vector<std::pair<int, int>> times(g.VerticesCount());
  118.   std::vector<bool> visited(g.VerticesCount(), false);
  119. /*  auto l = [&visited, &g] (int v, auto& l) {
  120.     visited[v] = true;
  121.     std::cout << v << " ";
  122.     for (int x : g.FindAllAdjacentOut(v)) {
  123.       if (!visited[x]) l(x, l);
  124.     }
  125.   };*/
  126.   for (int i = 0; i < visited.size(); ++i)
  127.     if (!visited[i]) {
  128.       // l(i, l);
  129.       DFS(i, g, visited, times, time);
  130.     }
  131.   return times;
  132. }
  133.  
  134. int main() {
  135.   ListGraph lg(9);
  136.   lg.AddEdge(6, 0);
  137.   lg.AddEdge(3, 4);
  138.   lg.AddEdge(8, 4);
  139.   lg.AddEdge(7, 8);
  140.   lg.AddEdge(4, 7);
  141.   lg.AddEdge(3, 2);
  142.   lg.AddEdge(2, 1);
  143.   lg.AddEdge(2, 5);
  144.   lg.AddEdge(1, 5);
  145.   lg.AddEdge(5, 7);
  146.  
  147.   std::vector<std::pair<int, int>> times = DFS(lg);
  148.   for (int i = 0; i < lg.VerticesCount(); ++i) {
  149.     std::cout << i << ": " << times[i].first << " " << times[i].second << std::endl;
  150.   }
  151.   return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement