답안 #246988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246988 2020-07-10T17:15:56 Z receed 우주 해적 (JOI14_space_pirate) C++14
47 / 100
2000 ms 9856 KB
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

const int N = 100001;
vector<int> g[N], cl, l1;
ll a[N], pos[N], used[N], cyc[N], cnt[N], ps[N], st[N], vis[N], cu[N];
ll ans[N];

// void dfs(int v) {
//     used[v] = 1;
//     for (int u : g[v])
//         if (!used[u])
//             dfs(u);
// }

// void dfs2(int v, int pos) {
//     used[v] = 1;
//     cnt[pos]++;
//     pos--;
//     if (pos < 0)
//         pos = cl.size() - 1;
//     for (int u : g[v])
//         if (!cyc[u])
//             dfs2(u, pos);
// }

void dfs3(int v, int h, int fv, ll ck) {
    vis[v] = 1;
    st[h - 1] = v;
    ans[st[(h - ck % h) % h]]++;
    for (int u : g[v]) {
        if (u == fv)
            continue;
        dfs3(u, h + 1, fv, ck);
    }
}

void dfs4(int v, ll pos, int fv) {
    assert(vis[v] != 2);
    vis[v] = 2;
    ans[cl[pos]]++;
    for (int u : g[v])
        if (u != fv && cu[u] != 2)
            dfs4(u, (pos - 1 + (ll) cl.size()) % cl.size(), fv);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll n, k;
    cin >> n >> k;
    rep(i, n) {
        cin >> a[i];
        a[i]--;
        g[a[i]].push_back(i);
        pos[i] = -1;
    }
    ll c = 0, v = 0, bl, u, dv, bd;
    while (pos[v] == -1) {
        l1.push_back(v);
        pos[v] = c++;
        v = a[v];
    }
    bl = c - pos[v];
    bd = pos[v];
    ll dvp = (k - bd) % bl;
    dv = l1[bd + dvp];
    // dfs(v);
    ans[dv] += (n - c) * n;
    // rep(i, bl) {
    //     ll of = (i - dvp + bl) % bl;
    //     rep(i, bd)
    //         ans[l1[bd + i]] += (c - 2 - i - of + bl - 1) / bl;
    // }
    /*rep(i, bd)
        rep(j, c - 1 - i)
            ans[l1[((k - bd - j) % bl + bl) % bl + bd]]++;
    rep(i, bl)
        dfs3(

    rep(i, n) {
        if (used[i])
            continue;
        u = i;
        cl.clear();
        while (pos[u] == -1) {
            pos[u] = -2;
            u = a[u];
        }
        while (!cyc[u]) {
            cnt[cl.size()] = 0;
            cl.push_back(u);
            cyc[u] = 1;
            u = a[u];
        }
        int cls = cl.size();
        rep(i, cls)
            dfs2(cl[i], i);
        rep(i, cls)
            ps[i + 1] = ps[i] + cnt[i];
        rep(i, cls) {
            cout << cl[i] << ' ' << cnt[i] << '\n';
            int rp = (i + k) % cls + 1, lp = (i + k - c % cls + 1) % cls;
            cout << ' ' << lp << ' ' << rp << '\n';
            if (lp < 0) {
                lp += cls;
                ans[cl[i]] += ps[cls];
            }
            ans[cl[i]] += c / cls * ps[cls] + ps[rp] - ps[lp];
        }
    }*/
    rep(i, c) {
        rep(j, n)
            vis[j] = cu[j] = 0;
        dfs3(l1[i], 1, l1[i], k - i);
        rep(j, n) {
            if (vis[j])
                continue;
            cl.clear();
            u = j;
            while (!cu[u]) {
                cu[u] = 1;
                assert(!vis[u]);
                u = a[u];
            }
            while (cu[u] == 1) {
                cl.push_back(u);
                cu[u] = 2;
                assert(!vis[u]);
                u = a[u];
            }
            rep(l, cl.size())
                dfs4(cl[l], (l + k - i - 1) % cl.size(), l1[i]);
        }
    }
    ll ss = 0;
    rep(i, n)
        cout << ans[i] << '\n';
}

Compilation message

space_pirate.cpp: In function 'int main()':
space_pirate.cpp:19:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i, n) for (int i = 0; i < (n); i++)
                                     ^
space_pirate.cpp:155:13: note: in expansion of macro 'rep'
             rep(l, cl.size())
             ^~~
space_pirate.cpp:159:8: warning: unused variable 'ss' [-Wunused-variable]
     ll ss = 0;
        ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 6 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2688 KB Output is correct
11 Correct 6 ms 2688 KB Output is correct
12 Correct 6 ms 2688 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 6 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 6 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2688 KB Output is correct
11 Correct 6 ms 2688 KB Output is correct
12 Correct 6 ms 2688 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 6 ms 2688 KB Output is correct
15 Correct 12 ms 2944 KB Output is correct
16 Correct 7 ms 2944 KB Output is correct
17 Correct 14 ms 2944 KB Output is correct
18 Correct 211 ms 3172 KB Output is correct
19 Correct 131 ms 3072 KB Output is correct
20 Correct 112 ms 3164 KB Output is correct
21 Correct 220 ms 3192 KB Output is correct
22 Correct 101 ms 3192 KB Output is correct
23 Correct 199 ms 3296 KB Output is correct
24 Correct 95 ms 3072 KB Output is correct
25 Correct 7 ms 2944 KB Output is correct
26 Correct 208 ms 3192 KB Output is correct
27 Correct 92 ms 3040 KB Output is correct
28 Correct 102 ms 3016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2079 ms 9856 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 6 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2688 KB Output is correct
11 Correct 6 ms 2688 KB Output is correct
12 Correct 6 ms 2688 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 6 ms 2688 KB Output is correct
15 Correct 12 ms 2944 KB Output is correct
16 Correct 7 ms 2944 KB Output is correct
17 Correct 14 ms 2944 KB Output is correct
18 Correct 211 ms 3172 KB Output is correct
19 Correct 131 ms 3072 KB Output is correct
20 Correct 112 ms 3164 KB Output is correct
21 Correct 220 ms 3192 KB Output is correct
22 Correct 101 ms 3192 KB Output is correct
23 Correct 199 ms 3296 KB Output is correct
24 Correct 95 ms 3072 KB Output is correct
25 Correct 7 ms 2944 KB Output is correct
26 Correct 208 ms 3192 KB Output is correct
27 Correct 92 ms 3040 KB Output is correct
28 Correct 102 ms 3016 KB Output is correct
29 Execution timed out 2079 ms 9856 KB Time limit exceeded
30 Halted 0 ms 0 KB -