Advertisement
Nik_Perepelov

Ярику прикол

Nov 3rd, 2021
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  zadacha
  4. //
  5. //  Created by Yaroslav Monastyrev on 24.10.2021.
  6. //
  7.  
  8. #include <iostream>
  9. #include <stack>
  10. #include <list>
  11. #include <vector>
  12.  
  13. using namespace std;
  14.  
  15. class Graph {
  16.     int V;
  17.     vector<int> order;
  18.     vector<vector<int>> adj;
  19.  
  20.     void DFS_1(int v, vector<bool> &used, stack<int> &Stack);
  21.     void DFS_2(int v, vector<bool> &used);
  22.  
  23. public:
  24.  
  25.     Graph(int V);
  26.     Graph reverse();
  27.     void addEdge(int v, int w);
  28.     void mainSolve();
  29.  
  30. };
  31.  
  32. Graph :: Graph(int V) {
  33.     this -> V = V;
  34.     adj = vector<vector<int>> (V);
  35. }
  36.  
  37. void Graph::addEdge(int v, int w) {
  38.     adj[v].push_back(w);
  39. }
  40.  
  41. void Graph :: DFS_1(int v, vector<bool> &used, stack<int> &Stack){
  42.     used[v] = true;
  43.     for (auto i: adj[v]){
  44.         if (!used[i]){
  45.             DFS_1(i, used, Stack);
  46.         }
  47.     }
  48.     Stack.push(v);
  49. }
  50.  
  51. void Graph :: DFS_2(int v, vector<bool> &used){
  52.     used[v] = true;
  53.     order.push_back(v);
  54.     for (auto i: adj[v]){
  55.         if (!used[i]){
  56.             DFS_2(i, used);
  57.         }
  58.     }
  59. }
  60. Graph Graph :: reverse(){
  61.     Graph Gr(V);
  62.     for(int i = 0; i < V; i++){
  63.         for(auto j: adj[i]){
  64.             Gr.adj[j].push_back(i);
  65.         }
  66.  
  67.     }
  68.     return Gr;
  69. }
  70.  
  71. void Graph :: mainSolve() {
  72.     vector<bool> used(V);
  73.     stack <int> Stack;
  74.     for(int i = 0; i < V;i++) {
  75.         if (!used[i]){
  76.             DFS_1(i, used, Stack);
  77.         }
  78.     }
  79.     Graph Gr1 = reverse();
  80.     for(int i = 0; i < V;i++) {
  81.         used[i] = false;
  82.     }
  83.     vector<int> answer(V);
  84.     int count = 0;
  85.     while (!Stack.empty()) {
  86.         int a = Stack.top();
  87.         Stack.pop();
  88.         if(!used[a]){
  89.             Gr1.DFS_2(a, used);
  90.             for (int i = 0; i < Gr1.order.size(); ++i) {
  91.                 answer[Gr1.order[i]] = count + 1;
  92.             }
  93.             count++;
  94.             Gr1.order.clear();
  95.         }
  96.     }
  97.     cout << count << endl;
  98.     for (int i = 0; i < answer.size(); ++i) {
  99.         cout << answer[i] << " ";
  100.     }
  101. }
  102.  
  103. int main(){
  104.     int n, m;
  105.     cin >> n >> m;
  106.     Graph graph(n);
  107.  
  108.     for (int i = 0; i < m; ++i) {
  109.         int a, b;
  110.         cin >> a >> b;
  111.         graph.addEdge(a - 1, b - 1);
  112.     }
  113.     graph.mainSolve();
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement