This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "highway.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
using int64 = long long;
using ld = long double;
constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
void find_pair(const int n, const vector<int> U, const vector<int> V, const int a, const int b) {
const int m = U.size();
vector<int> w(m);
const int64 all_a = ask(w);
int lo = -1, hi = m - 1;
while (lo + 1 < hi) {
const int mid = (lo + hi) / 2;
for (int i = lo + 1; i <= mid; i++) {
w[i] = 1;
}
if (const int64 c = ask(w); c > all_a) {
hi = mid;
for (int i = lo + 1; i <= mid; i++) {
w[i] = 0;
}
} else {
lo = mid + 1;
}
}
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < m; i++) {
g[U[i]].emplace_back(V[i], i);
g[V[i]].emplace_back(U[i], i);
}
auto bfs = [&](const int s) {
vector<int> dist(n, kInf);
dist[s] = 0;
queue<int> q;
q.push(s);
vector<bool> visit(n, false);
visit[s] = true;
while (not q.empty()) {
const int x = q.front();
for (const auto &[y, _] : g[x]) {
if (visit[y]) continue;
q.push(y);
visit[y] = true;
dist[y] = dist[x] + 1;
}
q.pop();
}
return dist;
};
const int x = U[lo + 1], y = V[lo + 1];
vector<int> distx = bfs(x), disty = bfs(y);
vector<int> partition(n);
for (int i = 0; i < n; i++) {
partition[i] = (distx[i] < disty[i] ? 0 : 1);
}
vector<vector<pair<int, int>>> t(n);
for (int i = 0; i < m; i++) {
const int pu = partition[U[i]], pv = partition[V[i]];
if (pu == pv or (U[i] == x and V[i] == y)) {
t[U[i]].emplace_back(V[i], i);
t[V[i]].emplace_back(U[i], i);
} else {
w[i] = 1;
}
}
auto find_st = [&](const int s, const int p) {
function<void(int, int)> dfs1 = [&](const int u, const int p) {
for (const auto &[v, i] : t[u]) {
if (v == p) continue;
w[i] = 1;
dfs1(v, u);
}
};
dfs1(s, p);
const int64 this_1 = ask(w);
const int depth = (this_1 - all_a) / (b - a);
if (depth == 0) {
return s;
}
vector<pair<int, int>> candidates;
function<void(int, int, int, int)> dfs2 = [&](const int x, const int par, const int d, const int idx) {
if (d == depth) {
candidates.emplace_back(x, idx);
return;
}
for (const auto &[y, i] : t[x]) {
if (y == par) continue;
dfs2(y, x, d + 1, i);
}
};
dfs2(s, p, 0, -1);
for (int i = 0; i < m; i++) {
const int pu = partition[U[i]], pv = partition[V[i]];
if (pu == pv or (U[i] == x and V[i] == y)) {
w[i] = 0;
} else {
w[i] = 1;
}
}
int lo = 0, hi = (int) candidates.size() - 1;
while (lo < hi) {
const int mid = (lo + hi) / 2;
for (int i = lo; i <= mid; i++) {
w[candidates[i].second] = 1;
}
for (int i = mid + 1; i <= hi; i++) {
w[candidates[i].second] = 0;
}
if (ask(w) > all_a) {
hi = mid;
} else {
lo = mid + 1;
}
}
return candidates[lo].first;
};
answer(find_st(x, y),
find_st(y, x));
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |