28B - pSort - CodeForces Solution


dfs and similar dsu graphs *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define w     \
    int t;    \
    cin >> t; \
    while (t--)
#define v(n) \
    int n;   \
    cin >> n;
#define pb push_back
#define vi(name, size)             \
    vector<int> name(size);        \
    for (int i = 0; i < size; i++) \
        cin >> name[i];
#define f(n) for (int i = 0; i < n; i++)
#define all(vv) vv.begin(), vv.end()
#define pri(vv)                             \
    f(vv.size()) cout << vv[i] << char(32); \
    cout << endl;
const int N = 555555;
int fa[N], n, a[N], d[N];
int find(int x)
{
    return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;

    for (int i = 0; i <= n * n; i++)
    {
        fa[i] = i;
    }

    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }

    for (int i = 1; i <= n; i++)
    {
        cin >> d[i];

        int l = i - d[i], r = i + d[i];

        if (l >= 1)
        {
            fa[find(l)] = find(i);
        }

        if (r <= n)
        {
            fa[find(r)] = find(i);
        }
    }

    for (int i = 1; i <= n; i++)
    {
        if (find(a[i]) != find(i))
        {
            cout << "NO";

            return 0;
        }
    }

    cout << "YES";

    return 0;
}


Comments

Submit
0 Comments
More Questions

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
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum