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>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
mt19937 mt(random_device{}());
struct edge {
int to, idx;
edge() = default;
edge(int t, int i) : to(t), idx(i) {}
};
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
int M = U.size();
vector<vector<edge>> G(N);
rep (i, M) {
G[U[i]].emplace_back(V[i], i);
G[V[i]].emplace_back(U[i], i);
}
ll dist = ask(vector<int>(M, 0));
int s;
{
int ok = 0, ng = M;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
vector<int> f(M);
rep (i, M) f[i] = i < mid;
if (ask(f) != dist) ng = mid;
else ok = mid;
}
// ok 本目までの辺で最短経路のカットになる
// 以下 ok 未満の辺は全て B として、 ok の各端点につながる頂点を探す
s = ok;
}
int sv0 = U[s], sv1 = V[s];
// sv0 から行く方が sv1 からより近い頂点集合の中に v0 がいる
vector<int> dist1(N, -1), dist2(N, -1);
queue<int> que;
dist1[sv0] = 0;
que.push(sv0);
while (!que.empty()) {
int v = que.front(); que.pop();
for (auto e : G[v]) {
if (dist1[e.to] == -1 && e.idx > s) {
dist1[e.to] = dist1[v] + 1;
que.push(e.to);
}
}
}
dist2[sv1] = 0;
que.push(sv1);
while (!que.empty()) {
int v = que.front(); que.pop();
for (auto e : G[v]) {
if (dist2[e.to] == -1 && e.idx > s) {
dist2[e.to] = dist2[v] + 1;
que.push(e.to);
}
}
}
vector<int> near0(N);
rep (i, N) {
if (dist1[i] != -1 && (dist2[i] == -1 || dist1[i] < dist2[i])) near0[i] = 1;
if (dist2[i] != -1 && (dist1[i] == -1 || dist2[i] < dist1[i])) near0[i] = -1;
}
// 遠い辺から無効にしていったとき、はじめて距離が変わったときの辺の端点が答え?
int ans0, ans1;
{
vector<pair<int, int>> es;
rep2 (i, s + 1, M) {
if (near0[U[i]] != 1 || near0[V[i]] != 1) continue;
int d = max(dist1[U[i]], dist1[V[i]]);
es.emplace_back(d, i);
}
sort(rall(es));
int ok = es.size() + 1, ng = 0;
while (ok - ng > 1) {
int mid = (ok + ng) / 2;
vector<int> f(M);
rep (i, s) f[i] = 1;
rep (i, mid) f[es[i].second] = 1;
if (ask(f) != dist) ok = mid;
else ng = mid;
}
if (ok == es.size() + 1) ans0 = sv0;
else {
--ok;
if (dist1[U[es[ok].second]] > dist1[V[es[ok].second]]) ans0 = U[es[ok].second];
else ans0 = V[es[ok].second];
}
}
{
vector<pair<int, int>> es;
rep2 (i, s + 1, M) {
if (near0[U[i]] != -1 || near0[V[i]] != -1) continue;
int d = max(dist2[U[i]], dist2[V[i]]);
es.emplace_back(d, i);
}
sort(rall(es));
int ok = es.size() + 1, ng = 0;
while (ok - ng > 1) {
int mid = (ok + ng) / 2;
vector<int> f(M);
rep (i, s) f[i] = 1;
rep (i, mid) f[es[i].second] = 1;
if (ask(f) != dist) ok = mid;
else ng = mid;
}
if (ok == es.size() + 1) ans1 = sv1;
else {
--ok;
if (dist2[U[es[ok].second]] > dist2[V[es[ok].second]]) ans1 = U[es[ok].second];
else ans1 = V[es[ok].second];
}
}
answer(ans0, ans1);
}
Compilation message (stderr)
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:99:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | if (ok == es.size() + 1) ans0 = sv0;
| ~~~^~~~~~~~~~~~~~~~
highway.cpp:123:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | if (ok == es.size() + 1) ans1 = sv1;
| ~~~^~~~~~~~~~~~~~~~
# | 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... |