#include "highway.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
int n, m;
vector<int>w;
vector<int>gs[maxn];
vector<pair<int,int> > g[maxn];
vector<int>gt[maxn];
int dist[maxn][2], parent[maxn][2];
bool visited[maxn];
ll dista;
map<pair<int,int>, int> ind;
void bfs(int st, int d) {
memset(visited,false,sizeof(visited));
queue<int>q;
q.push(st);
parent[st][d] = -1;
visited[st] = true;
while(!q.empty()) {
int curr = q.front();
q.pop();
for(auto i:g[curr]) {
if(!visited[i.first]) {
visited[i.first] = true;
dist[i.first][d] = dist[curr][d] + 1;
parent[i.first][d] = i.second;
q.push(i.first);
}
}
}
}
set<pair<int,int> > sta, stb;
void preprocess() {
for(int i=0;i<n;i++) {
if(dist[i][0] == dist[i][1]) continue; // skip this node
if(dist[i][0] < dist[i][1]) {
for(int j:gs[i]) {
if(dist[j][0] < dist[j][1]) {
sta.insert(mp(min(i, j), max(i, j)));
}
}
}
else {
for(int j:gs[i]) {
if(dist[j][0] > dist[j][1]) {
stb.insert(mp(min(i, j), max(i, j)));
}
}
}
}
}
int solve(set<pair<int,int> > edges, int root, int d) {
// build bfs tree
memset(visited,false,sizeof(visited));
for(auto i:edges) {
gt[i.first].pb(i.second);
gt[i.second].pb(i.first);
}
vector<int>v;
queue<int>q;
q.push(root);
visited[root] = true;
while(!q.empty()) {
int curr = q.front();
q.pop();
v.pb(curr);
for(auto i:gt[curr]) {
if(!visited[i]) {
visited[i] = true;
dist[i][d] = dist[curr][d] + 1;
q.push(i);
}
}
}
if(v.size() == 1) return v[0];
// perform binary search
/*cout<<d<<": ";
for(int i:v) {
cout<<i<<" ";
} cout<<"\n";*/
int li = 0, ri = v.size() - 1;
for(int i=0;i<v.size()-1;i++) {
int cind = ind[mp(v[i], v[i+1])];
w[cind] = 1;
}
while(li < ri) {
int mid = (li + ri) / 2;
// all before mid put them as low cost and other as high cost
for(int i=0;i<mid-1;i++) {
int cind = ind[mp(v[i], v[i+1])];
w[cind] = 0;
}
ll cdist = ask(w);
for(int i=0;i<mid-1;i++) {
int cind = ind[mp(v[i], v[i+1])];
w[cind] = 1;
}
if(cdist == dista) {
ri = mid;
}
else {
li = mid + 1;
}
}
return v[li];
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
n = N; m = U.size();
int li=0, ri=m-1;
for(int i=0;i<m;i++) {
ind[mp(U[i], V[i])] = ind[mp(V[i], U[i])] = i;
gs[U[i]].pb(V[i]);
gs[V[i]].pb(U[i]);
g[U[i]].pb(mp(V[i], i));
g[V[i]].pb(mp(U[i], i));
}
for(int i=0;i<m;i++) w.pb(0);
dista = ask(w);
while(li<ri) {
int mid = (li+ri)/2;
for(int i=li;i<=mid;i++) {
w[i] = 1;
}
ll distb = ask(w);
for(int i=li;i<=mid;i++) {
w[i] = 0;
}
if(dista == distb) {
li = mid + 1;
}
else {
ri = mid;
}
}
int x = li;
int u = U[x], v = V[x];
bfs(u, 0);
bfs(v, 1);
preprocess();
int xx = solve(sta, u, 0);
int yy = solve(stb, v, 1);
answer(xx, yy);
}
Compilation message
highway.cpp: In function 'int solve(std::set<std::pair<int, int> >, int, int)':
highway.cpp:109:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size()-1;i++) {
~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
7488 KB |
Output is incorrect: {s, t} is wrong. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
7932 KB |
Output is incorrect: {s, t} is wrong. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
10952 KB |
Output is incorrect: {s, t} is wrong. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
7928 KB |
Output is incorrect: {s, t} is wrong. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
99 ms |
11628 KB |
Output is incorrect: {s, t} is wrong. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
67 ms |
11456 KB |
Output is incorrect: {s, t} is wrong. |
2 |
Halted |
0 ms |
0 KB |
- |