// In the name of Allah
#include <bits/stdc++.h>
#include "highway.h"
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2e5 + 4;
const int oo = 1e9 + 4;
int n, m, W0, W1; ll D; pii ans;
vector<pii> E; vector<int> Q, Qx;
vector<pii> adj[maxn], adjx[maxn];
int dis[maxn]; queue<int> qu;
int h[maxn]; pii par[maxn];
vector<int> ls;
bool cmp(int u, int v) {
return (h[u] > h[v]);
}
bool checkx(int x) {
for (int i = 0; i < m; i++) {
if (i < x) Q[i] = 1;
else Q[i] = 0;
}
return (ask(Q) == D);
}
bool checkf(int x) {
Q = Qx;
for (int i = 0; i < x; i++) {
int j = par[ls[i]].S; Q[j] = 1;
}
return (ask(Q) == D);
}
void bfs(int v) {
fill(dis, dis + n, oo);
dis[v] = 0; qu.push(v);
while (!qu.empty()) {
int v = qu.front(); qu.pop();
for (auto f : adjx[v]) {
int u = f.F, j = f.S;
if (dis[v] + 1 < dis[u]) {
dis[u] = dis[v] + 1; qu.push(u);
adj[v].pb(Mp(u, j)); adj[u].pb(Mp(v, j));
Qx[j] = 0;
}
}
}
}
void dfs(int v, int p) {
for (auto f : adj[v]) {
int u = f.F, j = f.S;
if (u == p) continue;
h[u] = h[v] + 1; par[u] = Mp(v, j); ls.pb(u);
dfs(u, v);
}
}
int getx() {
int l = 0, r = len(ls) + 1;
while (r - l > 1) {
int mid = (l + r) / 2;
if (checkf(mid)) l = mid;
else r = mid;
}
if (l == len(ls)) return -1;
else return ls[l];
}
void find_pair(int Nx, vector<int> Ux, vector<int> Vx, int Ax, int Bx) {
n = Nx; m = len(Ux); W0 = Ax; W1 = Bx;
for (int i = 0; i < m; i++) E.pb(Mp(Ux[i], Vx[i]));
Q.resize(m); fill(all(Q), 0); D = ask(Q);
int l = 0, r = m;
while (r - l > 1) {
int mid = (l + r) / 2;
if (checkx(mid)) l = mid;
else r = mid;
}
for (int i = l; i < m; i++) {
int u = E[i].F, v = E[i].S;
adjx[u].pb(Mp(v, i)); adjx[v].pb(Mp(u, i));
}
int ux = E[l].F, vx = E[l].S;
Qx.resize(m); fill(all(Qx), 1); bfs(ux);
ls.clear(); dfs(ux, vx); sort(all(ls), cmp);
ans.F = getx();
if (ans.F == -1) ans.F = ux;
ls.clear(); dfs(vx, ux); sort(all(ls), cmp);
ans.S = getx();
if (ans.S == -1) ans.S = vx;
answer(ans.F, ans.S);
}
# | 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... |