제출 #730107

#제출 시각아이디문제언어결과실행 시간메모리
730107t6twotwo통행료 (IOI18_highway)C++17
51 / 100
347 ms262144 KiB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;
void find_pair(int N, vector<int> U, vector<int> V, int A, int B) {
    int M = U.size();
    vector<vector<pair<int, int>>> adj(N);
    for (int i = 0; i < M; i++) {
        adj[U[i]].emplace_back(V[i], i);
        adj[V[i]].emplace_back(U[i], i);
    }
    auto solve = [&](int r) {
        vector<int> a(M);
        int64_t dis = ask(a);
        vector<int> P(N, -1), cand;
        auto dfs = [&](auto dfs, int x, int p, int dep) -> void {
            if (dis / A == dep) {
                cand.push_back(x);
            }
            for (auto [y, z] : adj[x]) {
                if (y != p) {
                    P[y] = z;
                    dfs(dfs, y, x, dep + 1);
                }
            }
        };
        dfs(dfs, r, -1, 0);
        int K = cand.size();
        int lo = 0, hi = K - 1;
        while (lo < hi) {
            int x = (lo + hi) / 2;
            for (int i = lo; i <= x; i++) {
                a[P[cand[i]]] = 1;
            }
            int64_t y = ask(a);
            for (int i = lo; i <= x; i++) {
                a[P[cand[i]]] = 0;
            }
            if (y > dis) {
                hi = x;
            } else {
                lo = x + 1;
            }
        }
        answer(r, cand[lo]);
    };
    vector<int> dep(N), P(N, -1);
    auto dfs = [&](auto dfs, int x, int p) -> void {
        for (auto [y, z] : adj[x]) {
            if (y != p) {
                P[y] = z;
                dep[y] = dep[x] + 1;
                dfs(dfs, y, x);
            }
        }
    };
    dfs(dfs, 0, -1);
    vector<int> a(M);
    int64_t dis = ask(a);
    int lo = 1, hi = *max_element(dep.begin(), dep.end());
    while (lo < hi) {
        int x = (lo + hi + 1) / 2;
        for (int i = 1; i < N; i++) {
            if (dep[i] >= x) {
                a[P[i]] = 1;
            }
        }
        int64_t y = ask(a);
        for (int i = 1; i < N; i++) {
            if (dep[i] >= x) {
                a[P[i]] = 0;
            }
        }
        if (y > dis) {
            lo = x;
        } else {
            hi = x - 1;
        }
    }
    vector<int> cand;
    for (int i = 0; i < N; i++) {
        if (dep[i] == lo) {
            cand.push_back(i);
        }
    }
    int K = cand.size();
    lo = 0, hi = K - 1;
    while (lo < hi) {
        int x = (lo + hi) / 2;
        for (int i = lo; i <= x; i++) {
            a[P[cand[i]]] = 1;
        }
        int64_t y = ask(a);
        for (int i = lo; i <= x; i++) {
            a[P[cand[i]]] = 0;
        }
        if (y > dis) {
            hi = x;
        } else {
            lo = x + 1;
        }
    }
    solve(cand[lo]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...