Submission #744615

# Submission time Handle Problem Language Result Execution time Memory
744615 2023-05-18T19:53:08 Z pls33 Highway Tolls (IOI18_highway) C++17
100 / 100
235 ms 12228 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifndef _AAAAAAAAA
#include "highway.h"
#else
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B);
long long ask(const std::vector<int> &w);
void answer(int s, int t);
#endif

using namespace std;
using namespace __gnu_pbds;

#pragma region dalykai
template <typename F>
void _debug(F f)
{
    f();
}

#ifndef _AAAAAAAAA
#define debug(x)
#else
#define debug(x) _debug(x)
#endif
using p32 = pair<int, int>;
using p32u = pair<uint32_t, uint32_t>;
using p64 = pair<int64_t, int64_t>;
using p64u = pair<uint64_t, uint64_t>;
using vi16 = vector<int16_t>;
using vi16u = vector<uint16_t>;
using vi32 = vector<int>;
using vi32u = vector<uint32_t>;
using vi64 = vector<int64_t>;
using vi64u = vector<uint64_t>;
using vp32 = vector<p32>;
using vp32u = vector<p32u>;
using vp64 = vector<p64>;
using vp64u = vector<p64u>;
using vvi32 = vector<vi32>;
using vvi32u = vector<vi32u>;
using vvi64 = vector<vi64>;
using vvi64u = vector<vi64u>;
using vvp32 = vector<vp32>;
using vvp32u = vector<vp32u>;
using vvp64 = vector<vp64>;
using vvp64u = vector<vp64u>;
using f80 = long double;
#pragma endregion

using state_t = bitset<size_t(13 * 1e4)>;

// kelinta briauna keicia trumpiausia kelia??
int find_blocking_edge(int64_t cost, int edge_count)
{
    vi32 query(edge_count);

    int low = 0, high = edge_count - 1;
    while (low != high)
    {
        int mid = (low + high) / 2;
        for (int i = 0; i < edge_count; i++)
        {
            query[i] = i <= mid;
        }

        int64_t new_cost = ask(query);
        if (new_cost != cost)
        {
            high = mid;
        }
        else
        {
            low = mid + 1;
        }
    }

    return low;
}

p32 find_partition(int a, int b, vvp32 &adj, vp32 &order_a, vp32 &order_b)
{
    // virsune, is kur
    queue<tuple<int, int>> q;
    q.emplace(a, 0);
    q.emplace(b, 1);

    state_t visited;
    visited[a] = true;
    visited[b] = true;

    int count_a = 0;
    int count_b = 0;
    while (!q.empty())
    {
        auto &[vertex, source] = q.front();
        q.pop();

        for (auto &[next, edge] : adj[vertex])
        {
            if (visited[next])
            {
                continue;
            }
            visited[next] = true;
            int &count = source ? count_b : count_a;
            vp32 &order = source ? order_b : order_a;

            order[count].second = next;
            order[edge].first = count++;
            q.emplace(next, source);
        }
    }

    return {count_a, count_b};
}

int find_last_edge(vp32 &order, int64_t cost, int edge_count, int count)
{
    vi32 query(edge_count);
    int low = -1, high = count - 1;

    while (low != high)
    {
        int mid = (low + 1 + high) / 2;
        for (int i = 0; i < edge_count; i++)
        {
            debug([&]()
                  {
                      if (order[i].first < mid)
                      {
                          printf("blokuoju: %d (keliai iki %d)\n", i, mid);
                      }
                      //
                  });
            query[i] = order[i].first >= mid;
        }
        debug([&]()
              {
                  putchar('\n');
                  //
              });

        int64_t new_cost = ask(query);
        debug([&]()
              {
                  printf("turiu %" PRId64 ", gavau %" PRId64 "\n", cost, new_cost);
                  //
              });
        if (new_cost != cost)
        {
            low = mid;
        }
        else
        {
            high = mid - 1;
        }
    }

    return low;
}

void find_pair(int n, std::vector<int> u, std::vector<int> v, int A, int B)
{
    A = A;
    B = B;
    const int edge_count = (int)u.size();

    vvp32 adj(n);
    for (int i = 0; i < edge_count; i++)
    {
        debug([&]()
              {
                  printf("%d -- %d [label=\"%d\"]\n", u[i], v[i], i);
                  //
              });

        int a = u[i], b = v[i];

        adj[a].emplace_back(b, i);
        adj[b].emplace_back(a, i);
    }

    vi32 sample(u.size());
    int64_t cost = ask(sample);

    int initial = find_blocking_edge(cost, edge_count);
    int a = u[initial], b = v[initial];

    debug([&]()
          {
              printf("blokuoja: %d -- %d\n", a, b);
              //
          });

    vp32 order_a(edge_count, {-1, -1}), order_b(edge_count, {-1, -1});
    auto [count_a, count_b] = find_partition(a, b, adj, order_a, order_b);

    for (int i = 0; i < edge_count; i++)
    {
        if (i == initial)
        {
            continue;
        }
        if (order_a[i].first == order_b[i].first && order_a[i].first == -1)
        {
            order_a[i].first = edge_count;
            order_b[i].first = edge_count;
            // order_a[i].first = order_b[i].first = INT_MAX;
        }
    }

    /* debug([&]()
           {
               for (int i = 0; auto &[index, next] : order_a)
               {
                   printf("%d -- %d, galas %d\n", i, index, next);
                   i++;
               }
               putchar('\n');

               for (int i = 0; auto &[index, next] : order_b)
               {
                   printf("%d -- %d, galas %d\n", i, index, next);
                   i++;
               }
               putchar('\n');
               //
           });*/

    vi32 thing = {-1, -1};

    for (int i = 0; i < 2; i++)
    {
        int end = find_last_edge(order_a, cost, edge_count, count_a + 2);
        thing[i] = (end == -1) ? a : order_a[end].second;

        // break;
        swap(a, b);
        swap(order_a, order_b);
        swap(count_a, count_b);
    }
    answer(thing[0], thing[1]);
}

#ifdef _AAAAAAAAA

namespace
{
    constexpr int MAX_NUM_CALLS = 100;
    constexpr long long INF = 1LL << 61;

    int N, M, A, B, S, T;
    std::vector<int> U, V;
    std::vector<std::vector<std::pair<int, int>>> graph;

    bool answered, wrong_pair;
    int num_calls;

    int read_int()
    {
        int x;
        if (scanf("%d", &x) != 1)
        {
            fprintf(stderr, "Error while reading input\n");
            exit(1);
        }
        return x;
    }

    void wrong_answer(const char *MSG)
    {
        printf("Wrong Answer: %s\n", MSG);
        exit(0);
    }

} // namespace

long long ask(const std::vector<int> &w)
{
    if (++num_calls > MAX_NUM_CALLS)
    {
        wrong_answer("more than 100 calls to ask");
    }
    if (w.size() != (size_t)M)
    {
        wrong_answer("w is invalid");
    }
    for (size_t i = 0; i < w.size(); ++i)
    {
        if (!(w[i] == 0 || w[i] == 1))
        {
            wrong_answer("w is invalid");
        }
    }

    std::vector<bool> visited(N, false);
    std::vector<long long> current_dist(N, INF);
    std::queue<int> qa, qb;
    qa.push(S);
    current_dist[S] = 0;
    while (!qa.empty() || !qb.empty())
    {
        int v;
        if (qb.empty() ||
            (!qa.empty() && current_dist[qa.front()] <= current_dist[qb.front()]))
        {
            v = qa.front();
            qa.pop();
        }
        else
        {
            v = qb.front();
            qb.pop();
        }
        if (visited[v])
        {
            continue;
        }
        visited[v] = true;
        long long d = current_dist[v];
        if (v == T)
        {
            return d;
        }
        for (auto e : graph[v])
        {
            int vv = e.first;
            int ei = e.second;
            if (!visited[vv])
            {
                if (w[ei] == 0)
                {
                    if (current_dist[vv] > d + A)
                    {
                        current_dist[vv] = d + A;
                        qa.push(vv);
                    }
                }
                else
                {
                    if (current_dist[vv] > d + B)
                    {
                        current_dist[vv] = d + B;
                        qb.push(vv);
                    }
                }
            }
        }
    }
    return -1;
}

void answer(int s, int t)
{
    if (answered)
    {
        wrong_answer("answered not exactly once");
    }

    printf("\n----------------\ngautas atsakymas: %d ir %d\n", s, t);
    if (!((s == S && t == T) || (s == T && t == S)))
    {
        wrong_pair = true;
    }

    answered = true;
}

int main()
{
    freopen("greitkeliai.in", "r", stdin);
#ifndef __linux__
    atexit([]()
           {
        freopen("con", "r", stdin);
        system("pause"); });
#endif
    N = read_int();
    M = read_int();
    A = read_int();
    B = read_int();
    S = read_int();
    T = read_int();
    U.resize(M);
    V.resize(M);
    graph.assign(N, std::vector<std::pair<int, int>>());
    for (int i = 0; i < M; ++i)
    {
        U[i] = read_int();
        V[i] = read_int();
        graph[U[i]].push_back({V[i], i});
        graph[V[i]].push_back({U[i], i});
    }

    answered = false;
    wrong_pair = false;
    num_calls = 0;
    find_pair(N, U, V, A, B);
    if (!answered)
    {
        wrong_answer("answered not exactly once");
    }
    if (wrong_pair)
    {
        wrong_answer("{s, t} is wrong");
    }
    printf("Accepted: %d\n", num_calls);
    return 0;
}
#endif

Compilation message

highway.cpp:15: warning: ignoring '#pragma region dalykai' [-Wunknown-pragmas]
   15 | #pragma region dalykai
      | 
highway.cpp:50: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
   50 | #pragma endregion
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 0 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 336 KB Output is correct
2 Correct 14 ms 1252 KB Output is correct
3 Correct 112 ms 9416 KB Output is correct
4 Correct 134 ms 9404 KB Output is correct
5 Correct 141 ms 9416 KB Output is correct
6 Correct 128 ms 9376 KB Output is correct
7 Correct 105 ms 9412 KB Output is correct
8 Correct 118 ms 9392 KB Output is correct
9 Correct 85 ms 9428 KB Output is correct
10 Correct 109 ms 9492 KB Output is correct
11 Correct 127 ms 8796 KB Output is correct
12 Correct 112 ms 8764 KB Output is correct
13 Correct 164 ms 8776 KB Output is correct
14 Correct 104 ms 8768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1232 KB Output is correct
2 Correct 22 ms 2104 KB Output is correct
3 Correct 27 ms 3108 KB Output is correct
4 Correct 120 ms 8776 KB Output is correct
5 Correct 131 ms 8776 KB Output is correct
6 Correct 104 ms 8760 KB Output is correct
7 Correct 128 ms 8780 KB Output is correct
8 Correct 99 ms 8836 KB Output is correct
9 Correct 107 ms 8884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 10 ms 1264 KB Output is correct
3 Correct 97 ms 7416 KB Output is correct
4 Correct 143 ms 9456 KB Output is correct
5 Correct 125 ms 9404 KB Output is correct
6 Correct 101 ms 9400 KB Output is correct
7 Correct 112 ms 9412 KB Output is correct
8 Correct 113 ms 9392 KB Output is correct
9 Correct 151 ms 9392 KB Output is correct
10 Correct 128 ms 9428 KB Output is correct
11 Correct 121 ms 8764 KB Output is correct
12 Correct 133 ms 8776 KB Output is correct
13 Correct 118 ms 8780 KB Output is correct
14 Correct 130 ms 8780 KB Output is correct
15 Correct 154 ms 9424 KB Output is correct
16 Correct 90 ms 9432 KB Output is correct
17 Correct 124 ms 8848 KB Output is correct
18 Correct 151 ms 8852 KB Output is correct
19 Correct 122 ms 9496 KB Output is correct
20 Correct 121 ms 8860 KB Output is correct
21 Correct 82 ms 9868 KB Output is correct
22 Correct 107 ms 9864 KB Output is correct
23 Correct 114 ms 9516 KB Output is correct
24 Correct 105 ms 9740 KB Output is correct
25 Correct 142 ms 8904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 1316 KB Output is correct
2 Correct 20 ms 1396 KB Output is correct
3 Correct 159 ms 9976 KB Output is correct
4 Correct 165 ms 10672 KB Output is correct
5 Correct 150 ms 12064 KB Output is correct
6 Correct 197 ms 12064 KB Output is correct
7 Correct 189 ms 12188 KB Output is correct
8 Correct 160 ms 12068 KB Output is correct
9 Correct 117 ms 9060 KB Output is correct
10 Correct 109 ms 9428 KB Output is correct
11 Correct 136 ms 9864 KB Output is correct
12 Correct 187 ms 11416 KB Output is correct
13 Correct 228 ms 11736 KB Output is correct
14 Correct 235 ms 12032 KB Output is correct
15 Correct 228 ms 12228 KB Output is correct
16 Correct 155 ms 10264 KB Output is correct
17 Correct 129 ms 9840 KB Output is correct
18 Correct 141 ms 10048 KB Output is correct
19 Correct 98 ms 10028 KB Output is correct
20 Correct 96 ms 10236 KB Output is correct
21 Correct 164 ms 11940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 1304 KB Output is correct
2 Correct 14 ms 1312 KB Output is correct
3 Correct 165 ms 10076 KB Output is correct
4 Correct 159 ms 10400 KB Output is correct
5 Correct 137 ms 10728 KB Output is correct
6 Correct 157 ms 12020 KB Output is correct
7 Correct 141 ms 10072 KB Output is correct
8 Correct 132 ms 10368 KB Output is correct
9 Correct 123 ms 10724 KB Output is correct
10 Correct 187 ms 12020 KB Output is correct
11 Correct 202 ms 12032 KB Output is correct
12 Correct 186 ms 12032 KB Output is correct
13 Correct 113 ms 9856 KB Output is correct
14 Correct 130 ms 9424 KB Output is correct
15 Correct 137 ms 9984 KB Output is correct
16 Correct 142 ms 9420 KB Output is correct
17 Correct 161 ms 9828 KB Output is correct
18 Correct 115 ms 9476 KB Output is correct
19 Correct 151 ms 11456 KB Output is correct
20 Correct 145 ms 11708 KB Output is correct
21 Correct 191 ms 12104 KB Output is correct
22 Correct 159 ms 12020 KB Output is correct
23 Correct 224 ms 12140 KB Output is correct
24 Correct 192 ms 12036 KB Output is correct
25 Correct 178 ms 12024 KB Output is correct
26 Correct 151 ms 12072 KB Output is correct
27 Correct 103 ms 9916 KB Output is correct
28 Correct 134 ms 9836 KB Output is correct
29 Correct 142 ms 10260 KB Output is correct
30 Correct 127 ms 10068 KB Output is correct
31 Correct 132 ms 10004 KB Output is correct
32 Correct 103 ms 9900 KB Output is correct
33 Correct 121 ms 10192 KB Output is correct
34 Correct 94 ms 10004 KB Output is correct
35 Correct 132 ms 10076 KB Output is correct
36 Correct 92 ms 9844 KB Output is correct
37 Correct 106 ms 10104 KB Output is correct
38 Correct 87 ms 10096 KB Output is correct
39 Correct 165 ms 12096 KB Output is correct
40 Correct 183 ms 12056 KB Output is correct
41 Correct 151 ms 12028 KB Output is correct
42 Correct 156 ms 11964 KB Output is correct