Submission #893218

# Submission time Handle Problem Language Result Execution time Memory
893218 2023-12-26T18:10:08 Z vjudge1 Ancient Books (IOI17_books) C++17
0 / 100
1 ms 348 KB
#include "books.h"

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using ii = pair<int, int>;

struct UF {
    vi e;
    UF(int N) : e(N, -1) {}
    int repr(int u) {
        while(e[u] >= 0) u = e[u];
        return u;
    }

    int join(int u, int v) {
        u = repr(u);
        v = repr(v);
        if(u == v) return 0;
        if(e[u] >= e[v]) swap(u, v);
        e[u] += e[v];
        e[v] = u;
        return 1;
    }
};

long long minimum_walk(vi p, int s) {
    int n = p.size();
    ll re = 0;
    for(int i = 0; i < n; ++i)
        re += abs(i - p[i]);
    vi cul(n, 0), viz(n, 0), alb(n, 0);
    int nrcic = 0;
    for(int i = 0; i < n; ++i) {
        if(!viz[i]) {
            int u = i;
            while(!viz[u]) {
                viz[u] = 1;
                cul[u] = nrcic;
                u = p[u];
            }
            if(i == p[i]) alb[nrcic] = 1;
            ++nrcic;
        }
    }
    alb.resize(nrcic);
    alb[cul[s]] = 0;
    vector<vi> L(nrcic);
    for(int i = 0; i + 1 < n; ++i) {
        if(cul[i] != cul[i + 1]) {
            L[cul[i]].push_back(cul[i + 1]);
            L[cul[i + 1]].push_back(cul[i]);
        }
    }
    viz.assign(nrcic, 0);

    function<void(int, vi&, int&)> dfs0 = [&](int u, vi &V, int&cnt) {
        viz[u] = 1;
        ++cnt;
        for(auto it : L[u]) {
            if(!viz[it] && alb[it]) {
                dfs0(it, V, cnt);
            }
            else if(!viz[it] && !alb[it]) {
                V.push_back(it);
            }
        }
    };
    vector<pair<int, ii> > E;
    for(int i = 0; i < nrcic; ++i) {
        if(alb[i] && !viz[i]) {
            vi vec;
            int cnt = 0;
            dfs0(i, vec, cnt);
            assert(vec.size() <= 2);
            if(vec.size() == 2) {
                int u = vec[0], v = vec[1];
                E.push_back({cnt, ii{u, v}});
            }
        }
    }

    for(int i = 0; i < nrcic; ++i)
        for(auto it : L[i]) {
            if(!alb[i] && !alb[it])
                E.push_back({0, ii{i, it}});
        }
    sort(E.begin(), E.end());
    UF Sol(nrcic);
    for(auto [w, muchie] : E) {
        auto [u, v] = muchie;
        if(Sol.join(u, v)) {
            re += (w + 1) * 2;
        }
    }
    return re;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4074'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -