40922023-03-13 21:30:05Szin AttilaMágikus sorozatcpp14Accepted 100/100193ms96612 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

const int maxN = 2e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;

int main() {
   InTheNameOfGod;

    int n;
    cin >> n;

    vector<int> v(n);
    for(int &i : v) cin >> i;

    vector<vector<int> > g(n+1);
    vector<int> be(n, 0), mo(n, -1);

    for(int i = 1; i < n; i++) {
        g[v[i]].push_back(i+v[i]);
    }

    priority_queue<pair<int, int> > q;
    for(int i = 0; i < n; i++) {
        q.push({-1, -i});
    }

    vector<set<int> > no(n);

    while(!q.empty()) {
        pair<int, int> cur = q.top();
        q.pop();

        int x = -cur.second, val = -cur.first;

        if(no[x].find(val) != no[x].end() || mo[x] != -1) continue;
        mo[x] = val;

        for(int sz : g[x]) {
            if(mo[sz] == -1) {
                no[sz].insert(val);
                q.push({-val-1, -sz});
            }
        }
    }

    for(int i : mo) cout << i << ' ';

    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1700 KiB
2Accepted8ms4848 KiB
subtask215/15
3Accepted4ms2948 KiB
4Accepted4ms3160 KiB
5Accepted4ms3372 KiB
subtask315/15
6Accepted3ms2860 KiB
7Accepted3ms3096 KiB
8Accepted2ms3012 KiB
9Accepted2ms3004 KiB
10Accepted3ms3136 KiB
subtask430/30
11Accepted4ms4156 KiB
12Accepted4ms4392 KiB
13Accepted4ms4320 KiB
14Accepted4ms4200 KiB
subtask540/40
15Accepted180ms92712 KiB
16Accepted182ms92644 KiB
17Accepted165ms75036 KiB
18Accepted136ms64116 KiB
19Accepted144ms63876 KiB
20Accepted156ms73312 KiB
21Accepted165ms73480 KiB
22Accepted165ms73468 KiB
23Accepted165ms73900 KiB
24Accepted193ms87428 KiB
25Accepted171ms73748 KiB
26Accepted160ms74320 KiB
27Accepted180ms80144 KiB
28Accepted179ms94948 KiB
29Accepted179ms94176 KiB
30Accepted130ms77468 KiB
31Accepted158ms85280 KiB
32Accepted164ms96612 KiB