Submission #499654

# Submission time Handle Problem Language Result Execution time Memory
499654 2021-12-29T07:10:29 Z CatalinT Highway Tolls (IOI18_highway) C++17
51 / 100
323 ms 262148 KB
#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>
 
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> dist;
vector<int> par;

void dfs(int n, int p) {
    for (int v : adj[n]) {
        if (v != p) {
            dist[v] = dist[n] + 1;
            par[v] = n;
            dfs(v, n);
        }
    }
}

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

    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;

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

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

        int64 cur = ask(w);

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

    assert(sol != -1);

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

    // 51 points, tree
    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);
    }

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

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

        dist.assign(N, 0);
        par.assign(N, -1);
        dfs(x, y);

        w.assign(M, 0);

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

        int64 depth = ask(w);

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

        cerr << "depth: " << depth << "\n";

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

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

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

                vector<int> w(M);

                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);
        };
    };

    solve_half_tree(U[sol], V[sol], S);
    solve_half_tree(V[sol], U[sol], T);

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

    assert(S != T);

    answer(S, T);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 272 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 1 ms 284 KB Output is correct
7 Correct 2 ms 200 KB Output is correct
8 Correct 1 ms 200 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 436 KB Output is correct
2 Correct 18 ms 2068 KB Output is correct
3 Correct 227 ms 17992 KB Output is correct
4 Correct 180 ms 18428 KB Output is correct
5 Correct 216 ms 18464 KB Output is correct
6 Correct 205 ms 17796 KB Output is correct
7 Correct 202 ms 18348 KB Output is correct
8 Correct 209 ms 17216 KB Output is correct
9 Correct 226 ms 17324 KB Output is correct
10 Correct 241 ms 18096 KB Output is correct
11 Correct 196 ms 14380 KB Output is correct
12 Correct 223 ms 15692 KB Output is correct
13 Correct 198 ms 15320 KB Output is correct
14 Correct 220 ms 15084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2112 KB Output is correct
2 Correct 26 ms 3872 KB Output is correct
3 Correct 35 ms 5820 KB Output is correct
4 Correct 131 ms 15832 KB Output is correct
5 Correct 136 ms 16012 KB Output is correct
6 Correct 141 ms 16584 KB Output is correct
7 Correct 111 ms 17256 KB Output is correct
8 Correct 104 ms 16332 KB Output is correct
9 Correct 105 ms 16412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 22 ms 2088 KB Output is correct
3 Correct 153 ms 11764 KB Output is correct
4 Correct 176 ms 17692 KB Output is correct
5 Correct 177 ms 14768 KB Output is correct
6 Correct 181 ms 15812 KB Output is correct
7 Correct 227 ms 14040 KB Output is correct
8 Correct 167 ms 15432 KB Output is correct
9 Correct 214 ms 18108 KB Output is correct
10 Correct 190 ms 18028 KB Output is correct
11 Correct 172 ms 15208 KB Output is correct
12 Correct 208 ms 15400 KB Output is correct
13 Correct 203 ms 14832 KB Output is correct
14 Correct 191 ms 14680 KB Output is correct
15 Correct 176 ms 15460 KB Output is correct
16 Correct 178 ms 13548 KB Output is correct
17 Correct 182 ms 15212 KB Output is correct
18 Correct 192 ms 15972 KB Output is correct
19 Correct 176 ms 14436 KB Output is correct
20 Correct 176 ms 15228 KB Output is correct
21 Correct 182 ms 20160 KB Output is correct
22 Correct 200 ms 19692 KB Output is correct
23 Correct 218 ms 19788 KB Output is correct
24 Correct 190 ms 19872 KB Output is correct
25 Correct 208 ms 21256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 323 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 243 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -