Submission #572078

# Submission time Handle Problem Language Result Execution time Memory
572078 2022-06-03T14:08:15 Z elazarkoren Highway Tolls (IOI18_highway) C++17
100 / 100
246 ms 27536 KB
#include "highway.h"
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;

const int MAX_N = 2e5;

int n, m;
vii graph[MAX_N];
pii par[MAX_N];
int U[MAX_N], V[MAX_N];

vi dist[MAX_N];

bitset<MAX_N> visited;
vi curr_edges;
void Dfs(int node, int parent, int d) {
    dist[d].push_back(node);
    visited[node] = true;
    par[node].x = parent;
    for (auto [neighbor, ind] : graph[node]) {
        if (neighbor == parent) par[node].y = ind;
        else {
            curr_edges.push_back(ind);
            Dfs(neighbor, node, d + 1);
        }
    }
}

ll original = 0;

vi edges;
int Solve(int u, int v) {
    for (int i = 0; i < n; i++) dist[i].clear();
    visited.reset();
    curr_edges.clear();
    Dfs(u, v, 0);
    vi query(m, 1);
    for (int i : edges) query[i] = 0;
    for (int i : curr_edges) query[i] = 1;
    int begin = 0, end = curr_edges.size(), mid;
    while (begin < end) {
        mid = (begin + end) >> 1;
        for (int i = 0; i < mid; i++) {
            query[curr_edges[i]] = 0;
        }
        if (ask(query) != original) begin = mid + 1;
        else end = mid;
        for (int i = 0; i < mid; i++) {
            query[curr_edges[i]] = 1;
        }
    }
    if (!end) return u;
    int i = curr_edges[end - 1];
    return par[U[i]].x == V[i] ? U[i] : V[i];
}

vii Bfs(int node) {
    vii dists(n, {n, -1});
    dists[node] = {0, -1};
    queue<int> q;
    q.push(node);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (auto [u, i] : graph[v]) {
            if (dists[v].x + 1 < dists[u].x) {
                dists[u] = {dists[v].x + 1, i};
                q.push(u);
            }
        }
    }
    return dists;
}

void find_pair(int N, vi U1, vi V1, int A, int B) {
    m = U1.size(), n = N;
    for (int i = 0; i < m; i++) {
        U[i] = U1[i], V[i] = V1[i];
    }
    for (int i = 0; i < m; i++) {
        graph[U[i]].push_back({V[i], i});
        graph[V[i]].push_back({U[i], i});
    }
    original = ask(vi(m));
    int begin = 0, end = m, mid;
    while (begin < end) {
        mid = (begin + end) >> 1;
        vi v(m);
        fill(v.begin(), v.begin() + mid + 1, 1);
        if (ask(v) > original) end = mid;
        else begin = mid + 1;
    }
    int x = V[end], y = U[end];
    vii d1 = Bfs(x), d2 = Bfs(y);
    d1[x].y = end, d2[y].y = end;
    for (int i = 0; i < n; i++) {
        if (i == y) continue;
        if (d1[i].x < d2[i].x) {
            edges.push_back(d1[i].y);
        } else if (d1[i].x > d2[i].x){
            edges.push_back(d2[i].y);
        }
    }
    for (int i = 0; i < n; i++) graph[i].clear();
    vi v(m, 1);
    for (int i : edges) {
        graph[U[i]].push_back({V[i], i});
        graph[V[i]].push_back({U[i], i});
        v[i] = 0;
    }
    answer(Solve(U[end], V[end]), Solve(V[end], U[end]));
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 6 ms 9680 KB Output is correct
3 Correct 5 ms 9680 KB Output is correct
4 Correct 5 ms 9716 KB Output is correct
5 Correct 5 ms 9680 KB Output is correct
6 Correct 6 ms 9680 KB Output is correct
7 Correct 5 ms 9680 KB Output is correct
8 Correct 5 ms 9680 KB Output is correct
9 Correct 6 ms 9716 KB Output is correct
10 Correct 5 ms 9680 KB Output is correct
11 Correct 5 ms 9680 KB Output is correct
12 Correct 6 ms 9680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9808 KB Output is correct
2 Correct 17 ms 10960 KB Output is correct
3 Correct 131 ms 19520 KB Output is correct
4 Correct 135 ms 19432 KB Output is correct
5 Correct 133 ms 19536 KB Output is correct
6 Correct 123 ms 19352 KB Output is correct
7 Correct 127 ms 19408 KB Output is correct
8 Correct 122 ms 19480 KB Output is correct
9 Correct 130 ms 19464 KB Output is correct
10 Correct 120 ms 19452 KB Output is correct
11 Correct 156 ms 20188 KB Output is correct
12 Correct 137 ms 21604 KB Output is correct
13 Correct 133 ms 21056 KB Output is correct
14 Correct 136 ms 21196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 11576 KB Output is correct
2 Correct 24 ms 13384 KB Output is correct
3 Correct 40 ms 15528 KB Output is correct
4 Correct 97 ms 24152 KB Output is correct
5 Correct 95 ms 24248 KB Output is correct
6 Correct 100 ms 26084 KB Output is correct
7 Correct 108 ms 27536 KB Output is correct
8 Correct 105 ms 24980 KB Output is correct
9 Correct 127 ms 25652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9808 KB Output is correct
2 Correct 19 ms 10748 KB Output is correct
3 Correct 90 ms 17208 KB Output is correct
4 Correct 118 ms 19496 KB Output is correct
5 Correct 120 ms 19516 KB Output is correct
6 Correct 115 ms 19448 KB Output is correct
7 Correct 117 ms 19440 KB Output is correct
8 Correct 158 ms 19556 KB Output is correct
9 Correct 128 ms 19428 KB Output is correct
10 Correct 156 ms 19428 KB Output is correct
11 Correct 140 ms 20624 KB Output is correct
12 Correct 133 ms 21728 KB Output is correct
13 Correct 138 ms 21280 KB Output is correct
14 Correct 196 ms 20712 KB Output is correct
15 Correct 120 ms 19480 KB Output is correct
16 Correct 118 ms 19496 KB Output is correct
17 Correct 139 ms 20804 KB Output is correct
18 Correct 143 ms 21260 KB Output is correct
19 Correct 161 ms 19400 KB Output is correct
20 Correct 135 ms 22052 KB Output is correct
21 Correct 109 ms 20128 KB Output is correct
22 Correct 108 ms 20128 KB Output is correct
23 Correct 114 ms 19680 KB Output is correct
24 Correct 115 ms 20056 KB Output is correct
25 Correct 143 ms 26836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 10788 KB Output is correct
2 Correct 19 ms 10840 KB Output is correct
3 Correct 151 ms 19936 KB Output is correct
4 Correct 204 ms 20128 KB Output is correct
5 Correct 228 ms 21660 KB Output is correct
6 Correct 246 ms 21544 KB Output is correct
7 Correct 190 ms 21284 KB Output is correct
8 Correct 181 ms 21076 KB Output is correct
9 Correct 141 ms 17992 KB Output is correct
10 Correct 179 ms 18320 KB Output is correct
11 Correct 202 ms 19344 KB Output is correct
12 Correct 214 ms 20200 KB Output is correct
13 Correct 206 ms 20672 KB Output is correct
14 Correct 192 ms 21180 KB Output is correct
15 Correct 239 ms 21408 KB Output is correct
16 Correct 167 ms 18832 KB Output is correct
17 Correct 121 ms 19532 KB Output is correct
18 Correct 126 ms 19912 KB Output is correct
19 Correct 117 ms 19836 KB Output is correct
20 Correct 120 ms 19968 KB Output is correct
21 Correct 212 ms 22092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 10796 KB Output is correct
2 Correct 22 ms 10824 KB Output is correct
3 Correct 158 ms 19428 KB Output is correct
4 Correct 167 ms 19708 KB Output is correct
5 Correct 162 ms 20056 KB Output is correct
6 Correct 215 ms 21232 KB Output is correct
7 Correct 135 ms 19932 KB Output is correct
8 Correct 178 ms 20240 KB Output is correct
9 Correct 161 ms 20060 KB Output is correct
10 Correct 208 ms 21156 KB Output is correct
11 Correct 174 ms 21052 KB Output is correct
12 Correct 209 ms 21272 KB Output is correct
13 Correct 143 ms 19120 KB Output is correct
14 Correct 128 ms 18364 KB Output is correct
15 Correct 156 ms 19232 KB Output is correct
16 Correct 131 ms 18372 KB Output is correct
17 Correct 165 ms 19252 KB Output is correct
18 Correct 180 ms 18392 KB Output is correct
19 Correct 175 ms 20304 KB Output is correct
20 Correct 185 ms 21032 KB Output is correct
21 Correct 186 ms 21292 KB Output is correct
22 Correct 208 ms 21196 KB Output is correct
23 Correct 175 ms 21096 KB Output is correct
24 Correct 246 ms 21120 KB Output is correct
25 Correct 231 ms 21368 KB Output is correct
26 Correct 228 ms 21232 KB Output is correct
27 Correct 119 ms 19828 KB Output is correct
28 Correct 113 ms 19576 KB Output is correct
29 Correct 122 ms 20088 KB Output is correct
30 Correct 117 ms 19868 KB Output is correct
31 Correct 114 ms 19620 KB Output is correct
32 Correct 145 ms 19752 KB Output is correct
33 Correct 117 ms 19956 KB Output is correct
34 Correct 139 ms 19692 KB Output is correct
35 Correct 114 ms 19740 KB Output is correct
36 Correct 122 ms 19528 KB Output is correct
37 Correct 158 ms 19772 KB Output is correct
38 Correct 123 ms 19824 KB Output is correct
39 Correct 218 ms 22572 KB Output is correct
40 Correct 232 ms 22792 KB Output is correct
41 Correct 195 ms 22084 KB Output is correct
42 Correct 197 ms 21668 KB Output is correct