Advertisement
1WaKa_WaKa1

Task_N

May 21st, 2022 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. vector<short> colors;
  8. vector<short> colorsInProcess;
  9. vector<vector<short>> graph;
  10. short answer;
  11.  
  12. void deep_search(short top){
  13.     colorsInProcess[top] = 1;
  14.     for (auto i : graph[top]){
  15.         if (colors[i] == 2){
  16.             continue;
  17.         }
  18.         if (colorsInProcess[i] == 1){
  19.             answer+=1;
  20.             continue;
  21.         }
  22.         deep_search(i);
  23.     }
  24.     colorsInProcess[top] = 0;
  25.     colors[top] = 2;
  26. }
  27.  
  28. int main(){
  29.     //Ввод данных
  30.     short n;
  31.     cin >> n;
  32.     colors.assign(n, 0);
  33.     graph.assign(n, {});
  34.     for (short i = 0; i < n; i++){
  35.         short temp;
  36.         cin >> temp;
  37.         graph[temp-1].push_back(i);
  38.     }
  39.  
  40.     //Тело программы
  41.     for (short i = 0; i < n; i++){
  42.         if (colors[i] != 2) {
  43.             colorsInProcess.assign(n, 0);
  44.             deep_search(i);
  45.         }
  46.     }
  47.  
  48.     //Вывод данных
  49.     cout << answer;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement