답안 #1079636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079636 2024-08-28T19:35:34 Z skittles1412 통행료 (IOI18_highway) C++17
51 / 100
188 ms 13148 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

using ll = long long;

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

inline void init_io() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
}

template <typename T>
vector<T> iota(int n, T x) {
    vector<T> ans(n);
    iota(begin(ans), end(ans), x);
    return ans;
}

template <typename T>
vector<T>& operator+=(vector<T>& a, const vector<T>& b) {
    a.insert(end(a), begin(b), end(b));
    return a;
}

template <typename T>
vector<T> operator+(vector<T> a, const vector<T>& b) {
    return a += b;
}

template <typename Cb>
struct CmpByKey {
    Cb cb;

    CmpByKey(Cb cb) : cb(cb) {}

    template <typename T>
    bool operator()(const T& a, const T& b) const {
        return cb(a) < cb(b);
    }
};

template <typename T, typename Cb>
T first_true(T l, T r, const Cb& cb) {
    while (l < r) {
        T mid = (l + r) / 2;

        if (cb(mid)) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }

    return l;
}

template <typename T, typename Cb>
T last_true(T l, T r, const Cb& cb) {
    while (l < r) {
        T mid = (l + r + 1) / 2;

        if (cb(mid)) {
            l = mid;
        } else {
            r = mid - 1;
        }
    }

    return l;
}

template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
    out << "[";
    for (int i = 0; i < sz(arr); i++) {
        if (i) {
            out << ", ";
        }
        out << arr[i];
    }
    return out << "]";
}

template <typename T>
T negated(T arr) {
    for (auto& a : arr) {
        a = -a;
    }
    return arr;
}

ll ask(const std::vector<int>& w);
void answer(int s, int t);

vector<int> bfs(const vector<vector<pair<int, int>>>& graph, int src) {
    int n = sz(graph);

    vector dist(n, -1);
    queue<int> q;

    auto push = [&](int u, int d) -> void {
        if (dist[u] != -1) {
            return;
        }

        dist[u] = d;
        q.push(u);
    };

    push(src, 0);

    while (sz(q)) {
        int u = q.front();
        q.pop();

        for (auto& [v, _ei] : graph[u]) {
            push(v, dist[u] + 1);
        }
    }

    return dist;
}

void solve(int n, const vector<pair<int, int>>& edges, int wa, int) {
    int m = sz(edges);
    dbg(m);

    vector<vector<pair<int, int>>> graph(n);

    for (int i = 0; i < m; i++) {
        auto& [u, v] = edges[i];
        graph[u].emplace_back(v, i);
        graph[v].emplace_back(u, i);
    }

    long base = ask(vector<int>(m, 0));

    auto nodes_blocked = [&](const vector<int>& nodes) -> vector<int> {
        vector<int> cv(m);

        for (auto& u : nodes) {
            for (auto& [_v, ei] : graph[u]) {
                cv[ei] = true;
            }
        }

        return cv;
    };
    auto ask_nodes = [&](const vector<int>& nodes) -> bool {
        return ask(nodes_blocked(nodes)) > base;
    };

    int root = first_true(0, n, [&](int x) -> bool {
        return ask_nodes(iota(x + 1, 0));
    });
    dbg(root);

    auto base_block = iota(root, 0);
    dbg(base_block);

    auto dist = bfs(graph, root);
    auto ord = iota(n, 0);
    sort(begin(ord), end(ord), CmpByKey([&](int u) -> int {
        return dist[u];
    }));

    vector<int> subtree_id(n);
    for (auto& a : ord) {
        if (a <= root) {
            continue;
        }

        if (dist[a] == 1) {
            subtree_id[a] = a;
            continue;
        }

        for (auto& [v, _ei] : graph[a]) {
            if (dist[v] == dist[a] - 1 && v >= root) {
                subtree_id[a] = subtree_id[v];
            }
        }
    }

    int u1_ind = last_true(0, n,
                           [&](int x) -> bool {
        return ask_nodes(base_block + vector(begin(ord) + x, end(ord)));
    }),
        u1 = ord[u1_ind];

    if (dist[u1] == base / wa) {
        answer(u1, root);
        return;
    }

    vector<int> rem_nodes;
    for (auto& a : ord) {
        if (a > root && subtree_id[a] != subtree_id[u1]) {
            rem_nodes.push_back(a);
        }
    }
    dbg(subtree_id);
    int u2_ind = last_true(0, sz(rem_nodes),
                           [&](int x) -> bool {
        return ask_nodes(base_block +
                         vector(begin(rem_nodes) + x, end(rem_nodes)));
    }),
        u2 = rem_nodes[u2_ind];

    dbg(u1, u2);
    dbg(ord, u1_ind, u2_ind);
    answer(u1, u2);
}

void find_pair(int n,
               vector<int> edges_u,
               vector<int> edges_v,
               int wa,
               int wb) {
    return solve(n, [&]() {
        vector<pair<int, int>> cv;

        for (int i = 0; i < sz(edges_u); i++) {
            cv.emplace_back(edges_u[i], edges_v[i]);
        }

        return cv;
    }(), wa, wb);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 10 ms 1532 KB Output is correct
3 Correct 104 ms 10164 KB Output is correct
4 Correct 92 ms 10112 KB Output is correct
5 Correct 92 ms 10156 KB Output is correct
6 Correct 96 ms 10152 KB Output is correct
7 Correct 100 ms 10728 KB Output is correct
8 Correct 95 ms 10316 KB Output is correct
9 Correct 104 ms 11236 KB Output is correct
10 Correct 93 ms 10376 KB Output is correct
11 Correct 105 ms 9764 KB Output is correct
12 Correct 96 ms 9712 KB Output is correct
13 Correct 85 ms 9832 KB Output is correct
14 Correct 84 ms 9852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1560 KB Output is correct
2 Correct 19 ms 2548 KB Output is correct
3 Correct 28 ms 4296 KB Output is correct
4 Correct 81 ms 10216 KB Output is correct
5 Correct 87 ms 10592 KB Output is correct
6 Correct 68 ms 9736 KB Output is correct
7 Correct 86 ms 11340 KB Output is correct
8 Correct 74 ms 9848 KB Output is correct
9 Correct 93 ms 11056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 12 ms 1368 KB Output is correct
3 Correct 91 ms 8568 KB Output is correct
4 Correct 129 ms 11376 KB Output is correct
5 Correct 136 ms 11428 KB Output is correct
6 Correct 136 ms 11360 KB Output is correct
7 Correct 102 ms 10916 KB Output is correct
8 Correct 134 ms 10880 KB Output is correct
9 Correct 107 ms 10576 KB Output is correct
10 Correct 95 ms 10328 KB Output is correct
11 Correct 119 ms 10772 KB Output is correct
12 Correct 97 ms 9832 KB Output is correct
13 Correct 105 ms 9856 KB Output is correct
14 Correct 115 ms 9964 KB Output is correct
15 Correct 111 ms 10856 KB Output is correct
16 Correct 124 ms 11088 KB Output is correct
17 Correct 106 ms 10108 KB Output is correct
18 Correct 110 ms 10612 KB Output is correct
19 Correct 110 ms 10956 KB Output is correct
20 Correct 115 ms 10848 KB Output is correct
21 Correct 100 ms 10564 KB Output is correct
22 Correct 103 ms 11252 KB Output is correct
23 Correct 91 ms 10476 KB Output is correct
24 Correct 99 ms 10528 KB Output is correct
25 Correct 99 ms 9680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1596 KB Output is correct
2 Correct 12 ms 1660 KB Output is correct
3 Correct 146 ms 11752 KB Output is correct
4 Correct 167 ms 11792 KB Output is correct
5 Correct 166 ms 12744 KB Output is correct
6 Correct 166 ms 13020 KB Output is correct
7 Correct 165 ms 12692 KB Output is correct
8 Correct 188 ms 13092 KB Output is correct
9 Correct 98 ms 8516 KB Output is correct
10 Correct 114 ms 9036 KB Output is correct
11 Correct 133 ms 10044 KB Output is correct
12 Correct 157 ms 11936 KB Output is correct
13 Correct 152 ms 12028 KB Output is correct
14 Correct 175 ms 12996 KB Output is correct
15 Correct 178 ms 13148 KB Output is correct
16 Correct 127 ms 10192 KB Output is correct
17 Correct 87 ms 10772 KB Output is correct
18 Correct 91 ms 11080 KB Output is correct
19 Correct 107 ms 10844 KB Output is correct
20 Incorrect 95 ms 10952 KB Output is incorrect: {s, t} is wrong.
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1620 KB Output is correct
2 Correct 11 ms 1624 KB Output is correct
3 Correct 151 ms 11444 KB Output is correct
4 Partially correct 144 ms 11920 KB Output is partially correct
5 Correct 143 ms 11856 KB Output is correct
6 Correct 163 ms 12832 KB Output is correct
7 Correct 148 ms 11152 KB Output is correct
8 Correct 119 ms 11136 KB Output is correct
9 Correct 119 ms 11456 KB Output is correct
10 Correct 160 ms 12756 KB Output is correct
11 Incorrect 156 ms 12480 KB Output is incorrect: {s, t} is wrong.
12 Halted 0 ms 0 KB -