CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:fabiank
Submission time:2024-10-21 16:00:48 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.12 sdetails
#2ACCEPTED0.12 sdetails
#3ACCEPTED0.12 sdetails
#4ACCEPTED0.12 sdetails
#5ACCEPTED0.12 sdetails
#6ACCEPTED0.34 sdetails
#7ACCEPTED0.26 sdetails
#8ACCEPTED0.28 sdetails
#9ACCEPTED0.37 sdetails
#10ACCEPTED0.32 sdetails
#11ACCEPTED0.12 sdetails
#12ACCEPTED0.38 sdetails

Compiler report

input/code.cpp: In function 'long long int ford_fulkerson(std::vector<std::vector<long long int> >&, int, int)':
input/code.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for (int i = 1; i < path_reversed.size(); i++)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = a; i < b; i++)

// Type Aliases for 1D and 2D vectors with initialization
#define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val
#define ll long long
#define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val))              // 2D vector of ints (n x m), initialized to val
#define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val

using namespace std;

void print_vector(vector<int> &x)
{
    for (int v : x)
    {
        cout << v << " ";
    }
    cout << "\n";
}

void print_matrix(vector<vector<int>> &matrix)
{
    cout << "\n"
         << "----------------" << "\n";
    for (vector<int> row : matrix)
    {
        print_vector(row);
    }
    cout << "\n"
         << "----------------" << "\n";
}

int calc_max_digit(int n)
{
    int max_digit = 0;
    while (n > 0 && max_digit < 9)
    {
        int digit = n % 10;
        if (digit > max_digit)
        {
            max_digit = digit;
        }
        n /= 10;
    }
    return max_digit;
}

// edges as edge list for outgoing node as pairs (end, cost)
vector<ll> dijkstras(int start_point, vector<vector<pair<int, int>>> edges)
{
    int n = edges.size();
    vector<bool> processed(n, false);
    vector<ll> distances(n, LLONG_MAX);
    distances[start_point] = 0;
    priority_queue<pair<ll, int>> pq;
    pq.push({0, start_point});
    while (!pq.empty())
    {
        int curr = pq.top().second;
        pq.pop();
        if (processed[curr])
        {
            continue;
        }
        processed[curr] = true;
        ll distance = distances[curr];

        for (pair<int, int> edge : edges[curr])
        {

            if (distance + edge.second < distances[edge.first])
            {
                distances[edge.first] = distance + edge.second;
                pq.push({-distances[edge.first], edge.first});
            }
        }
    }
    return distances;
}

int bfs_edmondson_karp(const vector<vector<ll>> &connections,
                       const int source, const int target, vector<int> &path_reversed)
{
    int n = connections.size();

    queue<pair<int, ll>> queue;
    queue.push({source, LLONG_MAX});
    vector<int> predecessor(n, -2);
    predecessor[source] = -1;

    while (!queue.empty())
    {
        int current = queue.front().first;
        ll current_bottleneck = queue.front().second;
        queue.pop();

        if (current == target)
        {
            while (current != -1)
            {
                path_reversed.push_back(current);
                current = predecessor[current];
            }
            return current_bottleneck;
        }

        for (int edge_end = 0; edge_end < n; edge_end++)
        {
            ll edge_cap = connections[current][edge_end];
            if (edge_cap > 0 && predecessor[edge_end] == -2)
            {
                predecessor[edge_end] = current;
                queue.push({edge_end, min(current_bottleneck, edge_cap)});
            }
        }
    }

    return -1;
}

ll ford_fulkerson(vector<vector<ll>> &residual_graph, const int source, const int target)
{
    ll flow = 0;

    while (true)
    {
        vector<int> path_reversed;
        int path_capacity = bfs_edmondson_karp(residual_graph, source, target, path_reversed);

        if (path_capacity < 0)
        {
            break;
        }

        flow += path_capacity;
        for (int i = 1; i < path_reversed.size(); i++)
        {
            int edge_end = path_reversed[i - 1];
            int edge_start = path_reversed[i];
            // reduce forwards edge
            residual_graph[edge_start][edge_end] -= path_capacity;
            assert(residual_graph[edge_start][edge_end] >= 0);
            // add to backwards edge
            residual_graph[edge_end][edge_start] += path_capacity;
            assert(residual_graph[edge_end][edge_start] >= 0);
        }
    }
    return flow;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    const int MAX_N = 2000002;
    const int MAX_LOG = 18;

    int n, q;
    int parent[MAX_N];
    int depth[MAX_N];
    int up[MAX_N][MAX_LOG + 1];

    cin >> n >> q;

    parent[1] = 0; // The general director has no boss
    depth[1] = 0;  // Depth of the root is 0

    // Reading the boss (parent) of each employee
    for (int i = 2; i <= n; ++i)
    {
        cin >> parent[i];
    }

    // Initializing the up table and calculating depths
    for (int i = 1; i <= n; ++i)
    {
        up[i][0] = parent[i];
        if (i != 1)
        {
            depth[i] = depth[parent[i]] + 1;
        }
    }

    // Preprocessing the up table for binary lifting
    for (int k = 1; k <= MAX_LOG; ++k)
    {
        for (int i = 1; i <= n; ++i)
        {
            if (up[i][k - 1] != 0)
            {
                up[i][k] = up[up[i][k - 1]][k - 1];
            }
            else
            {
                up[i][k] = 0;
            }
        }
    }

    // Answering queries
    while (q--)
    {
        int a, b;
        cin >> a >> b;
        if (depth[a] < depth[b])
        {
            swap(a, b);
        }
        int diff = depth[a] - depth[b];

        // Lifting a to the same depth as b
        for (int k = MAX_LOG; k >= 0; --k)
        {
            if (diff >= (1 << k))
            {
                a = up[a][k];
                diff -= (1 << k);
            }
        }

        if (a == b)
        {
            cout << a << "\n";
        }
        else
        {
            // Finding the lowest common boss
            for (int k = MAX_LOG; k >= 0; --k)
            {
                if (up[a][k] != 0 && up[a][k] != up[b][k])
                {
                    a = up[a][k];
                    b = up[b][k];
                }
            }
            int lca = up[a][0];
            if (lca == 0)
                lca = 1; // If no common ancestor found, the root is the LCA
            cout << lca << "\n";
        }
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 10
1 2 3 4 5 6 7 8 9
6 9
8 10
10 3
...

correct output
6
8
3
1
8
...

user output
6
8
3
1
8
...

Test 2

Verdict: ACCEPTED

input
10 10
1 1 1 1 1 1 1 1 1
1 7
3 4
4 1
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 3

Verdict: ACCEPTED

input
10 10
1 1 1 1 2 3 4 4 1
1 8
2 7
8 3
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 4

Verdict: ACCEPTED

input
10 10
1 1 3 1 2 2 5 3 9
7 2
7 6
3 9
...

correct output
2
2
3
1
1
...

user output
2
2
3
1
1
...

Test 5

Verdict: ACCEPTED

input
10 10
1 2 3 2 5 3 2 2 4
6 1
1 3
1 9
...

correct output
1
1
1
2
2
...

user output
1
1
1
2
2
...

Test 6

Verdict: ACCEPTED

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
74862
8750
16237
72298
58111
...

user output
74862
8750
16237
72298
58111
...

Test 7

Verdict: ACCEPTED

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 8

Verdict: ACCEPTED

input
200000 200000
1 2 1 2 3 2 1 6 3 1 10 12 13 4...

correct output
1
2
2
2
1
...

user output
1
2
2
2
1
...

Test 9

Verdict: ACCEPTED

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2796
633
633
151
2690
...

user output
2796
633
633
151
2690
...

Test 10

Verdict: ACCEPTED

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
365
73
103
365
216
...

user output
365
73
103
365
216
...

Test 11

Verdict: ACCEPTED

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
1
1
1
2

Test 12

Verdict: ACCEPTED

input
200000 200000
1 1 2 3 4 5 6 7 8 9 10 11 12 1...

correct output
27468
6353
27468
6353
6353
...

user output
27468
6353
27468
6353
6353
...