Submission #499694

# Submission time Handle Problem Language Result Execution time Memory
499694 2021-12-29T09:08:22 Z CatalinT Highway Tolls (IOI18_highway) C++17
51 / 100
289 ms 21640 KB
/*
TODO: Shuffle everything
*/

#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <cassert>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <random>

using namespace std;

#include "highway.h"

using int64 = long long;

// cache

map<pair<int, int>, int> eidx;

int eget(int u, int v) {
    if (u > v) swap(u, v);
    return eidx[{u, v}];
}

void eset(int u, int v, int idx) {
    if (u > v) swap(u, v);
    eidx[{u, v}] = idx;
}

//

vector<vector<int>> adj;

vector<int> tree_idx;
vector<int> comp;
vector<int> dist;
vector<int> par;

void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
    int M = U.size();
    int S = -1;
    int T = -1;
    // Step 0. Find min dist, 1 query

    random_device rd;
    mt19937 rnd(rd());
 

    vector<int> w(M);
    int64 D = ask(w) / A;

    // Step 1. Find edge on min - path, 18 queries worst-case (!)

    int l = 0, r = M - 1, sol = -1;

    vector<int> order(M);
    iota(order.begin(), order.end(), 0);

    // shuffle(order.begin(), order.end(), rnd);

    while (l <= r) {
        int m = (l + r) >> 1;

        w.assign(M, 0);
        for (int i = 0; i <= m; i++)
            w[order[i]] = 1;

        int64 cur = ask(w);

        if (cur > D * A) {
            sol = m;
            r = m - 1;
        } else {
            l = m + 1;
        }
    }

    assert(sol != -1);

    sol = order[sol];

    cerr << "Critical edge: " << U[sol] << " -> " << V[sol] << "\n";
    cerr << "D: " << D << "\n";

    adj.assign(N, vector<int>{});
    
    for (int i = 0; i < M; i++) {
        adj[ U[i] ].push_back( V[i] );
        adj[ V[i] ].push_back( U[i] );
        eset(U[i], V[i], i);
    }

    const int x = U[sol];
    const int y = V[sol];

    queue<int> Q;

    Q.push(x);
    Q.push(y);

    dist.assign(N, -1);
    par.assign(N, -1);
    comp.assign(N, -1);

    comp[x] = 0;
    comp[y] = 1;

    dist[x] = 0;
    dist[y] = 0;

    tree_idx.push_back(eget(x, y));

    for (; !Q.empty(); ) {
        auto el = Q.front();
        Q.pop();

        for (auto v : adj[el]) {
            if (dist[v] == -1) {
                dist[v] = dist[el] + 1;
                comp[v] = comp[el];
                par[v] = el;
                tree_idx.push_back(eget(el, v));
                Q.push(v);
            }
        }
    }

    assert(tree_idx.size() == N - 1);

    // TODO: Remove
    // w.assign(M, 1);
    // for (auto e : tree_idx) {
    //     w[e] = 0;
    // }
    // assert(ask(w) == D * A);
    //

    w.assign(M, 1);
    for (int i = 0; i < N; i++) {
        if (par[i] != -1 && comp[i] == 1) {
            w[eget(i, par[i])] = 0;
        }
    }

    int64 depth = ask(w);

    bool ok = false;
    for (int64 i = 0; i < N; i++) {
        if (i * A + (D - i) * B == depth) {
            depth = i;
            ok = true;
            break;
        }
    }
    assert(ok);

    depth = D - 1 - depth;
    
    // int depth = 0;

    // l = 0;
    // r = 0;
    // sol = 0;

    // for (int i = 0; i < N; i++) {
    //     if (comp[i] == 0 && par[i] != -1) {
    //         r = max(r, dist[i]);
    //     }
    // }

    // if (!r) {
    //     depth = 0;
    // } else {
    //     sol = r;
    //     r--;
    //     while(l <= r) {
    //         int m = (l + r) >> 1;

    //         w.assign(M, 1);
    //         for (auto e : tree_idx)
    //             w[e] = 0;
    //         for (int i = 0; i < N; i++) {
    //             if (comp[i] == 0 && par[i] != -1 && dist[i] > m) {
    //                 w[eget(i, par[i])] = 1;
    //             }
    //         }

    //         if (ask(w) == D * A) {
    //             sol = m;
    //             r = m - 1;
    //         } else {
    //             l = m + 1;
    //         }
    //     }

    //     assert(sol != -1);
    //     depth = sol;
    // }

    auto solve_half_tree = [&] (int x, int y, int cc, int depth, int & R) {
        R = -1;

        cerr << "solve_half_tree, start: " << x << " par: " << y << " " << cc << "\n";
        cerr << "depth: " << depth << "\n";

        if (!depth) {
            R = x;
            assert(!dist[R]);
        } else {
            vector<int> cand;
            for (int i = 0; i < N; i++) {
                if (par[i] != -1 && dist[i] == depth && comp[i] == cc) {
                    cand.push_back(i);
                }
            }
            assert(cand.size());

            // shuffle(cand.begin(), cand.end(), rnd);

            function<int(int, int)> rec = [&] (int l, int r) {
                if (l == r) {
                    return cand[l];
                }

                int m = (l + r) >> 1;

                vector<int> w(M, 1);

                for (auto e : tree_idx)
                    w[e] = 0;

                for (int i = l; i <= m; i++) {
                    w[eget(cand[i], par[cand[i]])] = 1;
                }

                bool ok = ask(w) > D * A;

                if (ok) {
                    return rec(l, m);
                } else {
                    return rec(m + 1, r);
                }
            };

            R = rec(0, cand.size() - 1);

            assert(dist[R] == depth);
        };
    };

    solve_half_tree(x, y, 0, depth, S);
    solve_half_tree(y, x, 1, D - 1 - depth, T);

    cerr << S << " " << T << "\n";

    // auto debug = [&](int _S, int _T) {
    //     cerr << dist[_S] << " " << comp[_S] << "\n";
    //     cerr << dist[_T] << " " << comp[_T] << "\n";
    // };

    // cerr << "me:\n";
    // debug(S, T);
    // cerr << "correct:\n";
    // debug(89669, 73110);
    //debug(RS, RT);

    assert(S != T);

    answer(S, T);
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from highway.cpp:20:
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:144:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  144 |     assert(tree_idx.size() == N - 1);
      |            ~~~~~~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 2 ms 200 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 200 KB Output is correct
7 Correct 1 ms 200 KB Output is correct
8 Correct 1 ms 276 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 1 ms 200 KB Output is correct
11 Correct 1 ms 200 KB Output is correct
12 Correct 1 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 16 ms 2212 KB Output is correct
3 Correct 232 ms 19180 KB Output is correct
4 Correct 204 ms 19660 KB Output is correct
5 Correct 246 ms 19460 KB Output is correct
6 Correct 214 ms 18780 KB Output is correct
7 Correct 247 ms 19556 KB Output is correct
8 Correct 242 ms 18420 KB Output is correct
9 Correct 189 ms 18444 KB Output is correct
10 Correct 246 ms 19216 KB Output is correct
11 Correct 201 ms 14756 KB Output is correct
12 Correct 179 ms 15600 KB Output is correct
13 Correct 235 ms 15472 KB Output is correct
14 Correct 210 ms 15084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 1864 KB Output is correct
2 Correct 23 ms 3444 KB Output is correct
3 Correct 35 ms 5044 KB Output is correct
4 Correct 118 ms 14660 KB Output is correct
5 Correct 106 ms 14664 KB Output is correct
6 Correct 157 ms 14604 KB Output is correct
7 Correct 107 ms 14584 KB Output is correct
8 Correct 109 ms 14640 KB Output is correct
9 Correct 129 ms 14752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 440 KB Output is correct
2 Correct 17 ms 2216 KB Output is correct
3 Correct 162 ms 12492 KB Output is correct
4 Correct 206 ms 18732 KB Output is correct
5 Correct 173 ms 16032 KB Output is correct
6 Correct 197 ms 17064 KB Output is correct
7 Correct 187 ms 15452 KB Output is correct
8 Correct 211 ms 16640 KB Output is correct
9 Correct 236 ms 19400 KB Output is correct
10 Correct 202 ms 19232 KB Output is correct
11 Correct 220 ms 15580 KB Output is correct
12 Correct 181 ms 15160 KB Output is correct
13 Correct 182 ms 14924 KB Output is correct
14 Correct 186 ms 14820 KB Output is correct
15 Correct 237 ms 16756 KB Output is correct
16 Correct 205 ms 14824 KB Output is correct
17 Correct 177 ms 15464 KB Output is correct
18 Correct 196 ms 15892 KB Output is correct
19 Correct 191 ms 15704 KB Output is correct
20 Correct 240 ms 14864 KB Output is correct
21 Correct 225 ms 21640 KB Output is correct
22 Correct 185 ms 21468 KB Output is correct
23 Correct 245 ms 20868 KB Output is correct
24 Correct 242 ms 20860 KB Output is correct
25 Correct 184 ms 18764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 2368 KB Output is correct
2 Correct 18 ms 2320 KB Output is correct
3 Correct 270 ms 20648 KB Output is correct
4 Incorrect 289 ms 20680 KB Output is incorrect: {s, t} is wrong.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 3904 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -