1775D - Friendly Spiders - CodeForces Solution


dfs and similar graphs math number theory shortest paths *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i, n) for (ll i = 0; i < n; i++)
#define forn(i, n) for (ll i = 1; i < n; i++)
#define forne(i, n) for (ll i = 1; i <= n; i++)
#define revn(i, n) for (ll i = n - 1; i >= 0; i--)
#define sortl(v) sort(v.begin(), v.end())
#define reversal(v) reverse(v.begin(), v.end())
#define countset(n) __builtin_popcountll(n)
#define maxelement(v) *max_element(v.begin(), v.end())
#define minelement(v) *min_element(v.begin(), v.end())
#define vi vector<ll>
#define f(map) for (auto it : map)
#define read(n) \
    ll n;       \
    cin >> n;
const int inf = 1e9 + 7;
const int maxn = 3e5 + 10;
const int M = 2 * maxn;
vector<ll> fact(1e6);
vector<ll> inversion(1e6);
vector<bool> seive(maxn, true);
vector<ll> primes(maxn, 0);
void sieveness()
{
    for (int i = 2; i < maxn; i++)
        if (!primes[i])
            for (int j = i; j < maxn; j += i)
                primes[j] = i;
}
vector<ll> par(M, -1);
vector<ll> e[M];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    sieveness();
    ll test = 1;
    // cin >> test;
    while (test--)
    {

        ll i, j, a, b, c, d, v1, v2, f, cnt, ans, print;
        read(n);
        forne(i, n)
        {
            cin >> a;
            // cout << a << endl;
            while (a > 1)
            {
                e[i].push_back(maxn + primes[a]);
                e[primes[a] + maxn].push_back(i);
                a /= primes[a];
            }
        }
        queue<int> q;
        read(s);
        read(t);
        q.push(s);
        par[s] = s;
        while (q.size())
        {
            ll u = q.front();
            q.pop();
            for (auto it : e[u])
            {
                if (par[it] != -1)
                    continue;
                par[it] = u;
                q.push(it);
            }
        }
        if (par[t] == -1)
        {
            cout << "-1" << endl;
            continue;
        }
        vector<ll> res;
        res.push_back(t);
        while (par[t] != s)
        {
            t = par[par[t]];
            res.push_back(t);
        }
        reversal(res);
        cout << res.size() << endl;
        for (auto it : res)
        {
            cout << it << " ";
        }
        cout << endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing