This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "highway.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() { cout << "\n"; }
template<typename T, typename...U>
void dout(T t, U...u) { cout << t << (sizeof...(u) ? ", " : ""), dout(u...); }
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for(int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#ifdef WAIMAI
ll ask(const vector<int> &w);
void answer(int s, int t);
#endif
const int SIZE = 1.3e5 + 5;
ll len;
vector<pair<int, int>> adj[SIZE], all[2];
vector<int> e;
int g[SIZE];
bool is[SIZE];
int cal(int s, int M) {
int sz = all[s].size();
int l = 0, r = sz - 1;
while (l < r) {
int mid = (l + r) / 2;
FOR (i, 0, M - 1) e[i] = !is[i];
FOR (i, mid + 1, sz - 1) e[all[s][i].S] = 1;
if (ask(e) == len) r = mid;
else l = mid + 1;
}
return all[s][l].F;
}
void find_pair(int N, vector<int> U, vector<int> V, int A, int B) {
int M = U.size();
e.resize(M, 0);
FOR (i, 0, M - 1) {
adj[U[i]].pb(V[i], i);
adj[V[i]].pb(U[i], i);
}
len = ask(e);
int l = 0, r = M - 1;
while (l < r) {
int mid = (l + r) / 2;
fill(e.begin(), e.end(), 0);
fill(e.begin(), e.begin() + mid + 1, 1);
if (ask(e) == len) l = mid + 1;
else r = mid;
}
queue<int> q;
is[l] = 1;
fill(g, g + N, -1);
q.push(U[l]), g[U[l]] = 0, all[0].pb(U[l], -1);
q.push(V[l]), g[V[l]] = 1, all[1].pb(V[l], -1);
while (q.size()) {
int pos = q.front();
q.pop();
for (auto [np, id] : adj[pos]) if (g[np] == -1) {
g[np] = g[pos];
is[id] = 1;
all[g[pos]].pb(np, id);
q.push(np);
}
}
answer(cal(0, M), cal(1, M));
}
/*
in1
4 4
1 3 1 3
0 1
0 2
0 3
1 2
out1
1 3
*/
#ifdef WAIMAI
namespace {
constexpr int MAX_NUM_CALLS = 100;
constexpr ll INF = 1LL << 61;
int N, M, A, B, S, T;
vector<int> U, V;
vector<vector<pair<int, int>>> graph;
bool answered, wrong_pair;
int num_calls;
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
void wrong_answer(const char *MSG) {
printf("Wrong Answer: %s\n", MSG);
exit(0);
}
} // namespace
ll ask(const vector<int> &w) {
if (++num_calls > MAX_NUM_CALLS) {
wrong_answer("more than 100 calls to ask");
}
if (w.size() != (size_t)M) {
wrong_answer("w is invalid");
}
for (size_t i = 0; i < w.size(); ++i) {
if (!(w[i] == 0 || w[i] == 1)) {
wrong_answer("w is invalid");
}
}
vector<bool> visited(N, false);
vector<ll> current_dist(N, INF);
queue<int> qa, qb;
qa.push(S);
current_dist[S] = 0;
while (!qa.empty() || !qb.empty()) {
int v;
if (qb.empty() ||
(!qa.empty() && current_dist[qa.front()] <= current_dist[qb.front()])) {
v = qa.front();
qa.pop();
} else {
v = qb.front();
qb.pop();
}
if (visited[v]) {
continue;
}
visited[v] = true;
ll d = current_dist[v];
if (v == T) {
return d;
}
for (auto e : graph[v]) {
int vv = e.first;
int ei = e.second;
if (!visited[vv]) {
if (w[ei] == 0) {
if (current_dist[vv] > d + A) {
current_dist[vv] = d + A;
qa.push(vv);
}
} else {
if (current_dist[vv] > d + B) {
current_dist[vv] = d + B;
qb.push(vv);
}
}
}
}
}
return -1;
}
void answer(int s, int t) {
if (answered) {
wrong_answer("answered not exactly once");
}
if (!((s == S && t == T) || (s == T && t == S))) {
wrong_pair = true;
}
answered = true;
}
int main() {
N = read_int();
M = read_int();
A = read_int();
B = read_int();
S = read_int();
T = read_int();
U.resize(M);
V.resize(M);
graph.assign(N, vector<pair<int, int>>());
for (int i = 0; i < M; ++i) {
U[i] = read_int();
V[i] = read_int();
graph[U[i]].push_back({V[i], i});
graph[V[i]].push_back({U[i], i});
}
answered = false;
wrong_pair = false;
num_calls = 0;
find_pair(N, U, V, A, B);
if (!answered) {
wrong_answer("answered not exactly once");
}
if (wrong_pair) {
wrong_answer("{s, t} is wrong");
}
printf("Accepted: %d\n", num_calls);
return 0;
}
#endif
# | 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... |