Advertisement
Little_hobbit

Цветной лабиринт

Jul 13th, 2020
175
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 <map>
  4.  
  5. using namespace std;
  6.  
  7. vector<map<int, int>> g;
  8. vector<int> k_array;
  9. int ans = 0;
  10.  
  11. void DFS(int &v, int i)
  12. {
  13.     if (ans || i == (k_array.size())) return;
  14.  
  15.     bool found = false;
  16.     for (auto v_i : g[v])
  17.     {
  18.         if (v_i.second == k_array[i])
  19.         {
  20.             v = v_i.first;
  21.             DFS(v, i + 1);
  22.             found = true;
  23.         }
  24.  
  25.         if (ans) break;
  26.     }
  27.     if (!found)
  28.         ans = 1;
  29. }
  30.  
  31. int main()
  32. {
  33.     int n, m;
  34.     cin >> n >> m;
  35.  
  36.     g.resize(n);
  37.  
  38.     for (int i = 0; i < m; ++i)
  39.     {
  40.         int v1, v2, colour;
  41.         cin >> v1 >> v2 >> colour;
  42.  
  43.         g[--v1].insert(make_pair(--v2, colour));
  44.         g[v2].insert(make_pair(v1, colour));
  45.     }
  46.  
  47.     int k;
  48.     cin >> k;
  49.     k_array.resize(k);
  50.     for (int j = 0; j < k; ++j)
  51.     {
  52.         cin >> k_array[j];
  53.     }
  54.  
  55.     int v = 0;
  56.     DFS(v, 0);
  57.  
  58.     if (ans)
  59.         cout << "INCORRECT";
  60.     else
  61.         cout << v + 1;
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement