Submission #785828

#TimeUsernameProblemLanguageResultExecution timeMemory
785828vjudge1Ball Machine (BOI13_ballmachine)C++17
29.84 / 100
1092 ms26020 KiB
#include <bits/stdc++.h>

using namespace std;

int n, q, root;
vector<int> parent, depth, subtreeMin;
vector<vector<int>> ancestor;
vector<vector<int>> adjList;
vector<pair<int, int>> queries;

vector<bool> colour;

int depthFirstSearch(const int& u)
{
    subtreeMin[u] = u;
    
    for (auto &v : adjList[u])
    {
        depth[v] = depth[u] + 1;
        ancestor[v][0] = u;
        for (int b = 1; ancestor[v][b - 1] != -1; b++)
            ancestor[v][b] = ancestor[ancestor[v][b - 1]][b - 1];
        subtreeMin[u] = min(depthFirstSearch(v), subtreeMin[u]);
    }
    
    sort(adjList[u].begin(), adjList[u].end(), [&](const int& v1, const int& v2) -> bool
    {
        return subtreeMin[v1] < subtreeMin[v2];
    });
    
    return subtreeMin[u];
}

bool queryTypeOne()
{
    int d = -1;
    for (int u = 1; u <= n; u++)
    {
        if (!adjList[u].empty() && adjList[u].size() != 2)
            return false;
        if (adjList[u].empty())
        {
            if (d == -1)
                d = depth[u];
            else if (d != depth[u])
                return false;
        }
    }
    return true;
}

bool queryTypeThree()
{
    if (queries[0].first != 1)
        return false;
    for (int i = 1; i < q; i++)
        if (queries[i].first != 2)
            return false;
    return true;
}

int manualInsert(const int& u)
{
    for (auto &v : adjList[u])
        if (!colour[v])
            return manualInsert(v);
    colour[u] = true;
    return u;
}

int manualRemove(int u)
{
    int cnt = 0;
    while (u != root && colour[parent[u]])
        u = parent[u], cnt++;
    colour[u] = false;
    return cnt;
}

int main()
{
    // freopen("inp.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    ios::sync_with_stdio(false);

    cin >> n >> q;
    adjList.resize(n + 1);
    parent.resize(n + 1);
    for (int u = 1; u <= n; u++)
    {
        cin >> parent[u];
        if (parent[u] == 0)
            root = u;
        else
            adjList[parent[u]].push_back(u);
    }

    depth.resize(n + 1);
    subtreeMin.resize(n + 1);
    ancestor.resize(n + 1, vector<int>(20, -1));
    depthFirstSearch(root);
    
    queries.resize(q);
    for (int i = 0; i < q; i++)
        cin >> queries[i].first >> queries[i].second;

    colour.resize(n + 1);

    if (false && queryTypeOne())
    {
        for (auto &query : queries)
        {
            int type = query.first, x = query.second;

            if (type == 1)
            {
                int u;
                while (x --> 0)
                    u = manualInsert(root);
                cout << u << '\n';
            }

            if (type == 2)
                cout << manualRemove(x) << '\n';
        }

        return 0;
    }

    if (true || queryTypeThree())
    {
        for (auto &query : queries)
        {
            int type = query.first, x = query.second;

            if (type == 1)
            {
                int u;
                while (x --> 0)
                    u = manualInsert(root);
                cout << u << '\n';
            }

            if (type == 2)
            {
                int v = x;
                for (int b = 19; b >= 0; b--)
                    if (ancestor[v][b] != -1)
                        if (colour[ancestor[v][b]])
                            v = ancestor[v][b];

                cout << depth[x] - depth[v] << '\n';
                colour[v] = false;
            }
        }

        return 0;
    }
}

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:142:30: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  142 |                 cout << u << '\n';
      |                              ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...