Advertisement
TAImatem

Пауки

Jan 10th, 2023
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. int main() {
  2. #ifdef DEBUG
  3.     freopen("input.txt", "r", stdin);
  4.     //freopen("output.txt", "w", stdout);
  5. #endif
  6.     ios::sync_with_stdio(false);
  7.     cin.tie(0);
  8.     int n;
  9.     cin >> n;
  10.     vector<vector<int>> facts(n);
  11.     vector<vector<int>> neigh(300000, vector<int>());
  12.     vector<bool> passed_f(300000, 0);
  13.     vector<bool> passed(n, 0);
  14.     vector<int> prev(n, -1);
  15.     int a;
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         cin >> a;
  19.         for (int f = 2; f * f <= a; f++)
  20.         {
  21.             if (!(a % f))
  22.                 facts[i].push_back(f);
  23.             while (!(a % f))
  24.                 a /= f;
  25.         }
  26.         if (a != 1)
  27.             facts[i].push_back(a);
  28.         for (auto& f : facts[i])
  29.             neigh[f].push_back(i);
  30.     }
  31.     int s, t;
  32.     cin >> s >> t;
  33.     if (s == t)
  34.     {
  35.         cout << 1 << endl << t << endl;
  36.         return 0;
  37.     }
  38.     s--; t--;
  39.     queue<int> que;
  40.     passed[s] = 1;
  41.     que.push(s);
  42.     while (!que.empty())
  43.     {
  44.         int v = que.front();
  45.         que.pop();
  46.         for (auto& f : facts[v]) if (!passed_f[f])
  47.         {
  48.             passed_f[f] = 1;
  49.             for (auto& u : neigh[f])
  50.             {
  51.                 if (!passed[u])
  52.                 {
  53.                     que.push(u);
  54.                     prev[u] = v;
  55.                     passed[u] = 1;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     if (prev[t] == -1)
  61.     {
  62.         cout << -1 << endl;
  63.         return 0;
  64.     }
  65.     vector<int> route;
  66.     route.push_back(t);
  67.     while (prev[t] >= 0)
  68.     {
  69.         t = prev[t];
  70.         route.push_back(t);
  71.     }
  72.     reverse(route.begin(), route.end());
  73.     cout << route.size() << endl;
  74.     for (auto& a : route)
  75.         cout << a + 1 << " ";
  76.     cout << endl;
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement