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>
using namespace std;
typedef pair<int,int> pii;
#define f first
#define s second
/* ask with a vector saying the edge value (a or b) for each edge to know the smallest dist
1) find an edge in the path S -> T:
D&C in the edges, trying to find anyone that is in the path.
To do so, just paint everything with A and get the min dist from S to T
Then, in the D&C paint all edges at the current set with B, if it changes
the distance from S to T, than of these edges is in the path.
2) Let e be an edge in the path S -> T, how to find one of the endpoints, S or T ?
multisource bfs from the endpoints of e. Then enumerate the edges
according to their distante. Do bb in the prefix of edges, the last one that
changes the dist from S to T has S or T as the farthest endpoint.
3) Just do step 2) again
*/
const int maxn = 9e4 + 10;
vector<int> grafo[maxn];
int find_edge(int &n, int &m, vector<int> &u, vector<int> &v, int &a, int &b, int &d){ // log m queries
int l = 0, mid, r = m-1;
while(l != r){
mid = (l + r) >> 1;
vector<int> aux(m, 0);
for(int i=0; i<=mid; i++) aux[i] = 1;
if(ask(aux) != d) r = mid;
else l = mid + 1;
}
cout << u[l] << " " << v[l] << "\n";
return l;
}
vector<int> bfs(int &n, int &m, int &a, int &b, int &d, int root){
queue<pii> fila;
fila.push({root, 0});
vector<int> dist(n, -1);
while(!fila.empty()){
int x = fila.front().f, dis = fila.front().s;
fila.pop();
if(dist[x] != -1) continue;
dist[x] = dis;
for(int viz : grafo[x]) fila.push({viz, dis+1});
}
return dist;
}
pii find_endpoints(int &n, int &m, vector<int> &u, vector<int> &v, int &a, int &b, int &d, int &e){ // 2*log m queries
vector<int> dist[2];
dist[0] = bfs(n, m, a, b, d, u[e]);
dist[1] = bfs(n, m, a, b, d, v[e]);
pii ans = {0, 0};
vector<int> sbt[2];
for(int i=0; i<n; i++){
cout << i << " " << dist[0][i] << " " << dist[1][i] << "\n";
if(dist[0][i] <= dist[1][i]) sbt[0].push_back(i);
else sbt[1].push_back(i);
}
for(int j=0; j<2; j++){
int ini = 0, meio, fim = (int)sbt[j].size() - 1;
while(ini != fim){
meio = (ini + fim + 1) >> 1;
vector<int> aux(m, 0);
vector<bool> mark(n, 0);
for(int i=meio; i<sbt[j].size(); i++) mark[sbt[j][i]] = 1;
for(int i=0; i<m; i++) if(mark[u[i]] != mark[v[i]]) aux[i] = 1;
if(ask(aux) != d) ini = meio;
else fim = meio - 1;
}
if(j) ans.f = sbt[j][ini];
else ans.s = sbt[j][ini];
}
return ans;
}
void find_pair(int n, vector<int> u, vector<int> v, int a, int b) {
int m = u.size();
for(int i=0; i<m; i++){
grafo[u[i]].push_back(v[i]);
grafo[v[i]].push_back(u[i]);
}
vector<int> aux(m, 0); // 1 query
int d = ask(aux);
int e = find_edge(n, m, u, v, a, b, d);
pii ans = find_endpoints(n, m, u, v, a, b, d, e);
answer(ans.f, ans.s);
/*
4 4
1
3
1
3
0 1
0 2
0 3
1 2
*/
}
Compilation message (stderr)
highway.cpp: In function 'pii find_endpoints(int&, int&, std::vector<int>&, std::vector<int>&, int&, int&, int&, int&)':
highway.cpp:67:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=meio; i<sbt[j].size(); i++) mark[sbt[j][i]] = 1;
| ~^~~~~~~~~~~~~~
# | 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... |