#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 = 150100;
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];
int pind[maxn];
bool visited[maxn];
ll dista;
map<pair<int,int>, int> ind;
int type[maxn];
vector<pair<int,int> > sta, stb;
bool important[maxn];
void bfs(int u, int v) {
visited[u] = visited[v] = true;
queue<pair<int,int> > q;
type[u] = 0;
type[v] = 1;
q.push(mp(u, 0));
q.push(mp(v, 1));
pind[u] = pind[v] = -1;
while(!q.empty()) {
int curr = q.front().first;
int currtype = q.front().second;
q.pop();
for(auto i:g[curr]) {
int ni = i.first;
int nind = i.second;
if(visited[ni]) continue;
visited[ni] = true;
type[ni] = currtype;
pind[ni] = nind;
important[nind] = true;
q.push(mp(ni, currtype));
}
}
}
int solve(int root, int d) {
// build bfs tree
memset(visited,false,sizeof(visited));
//memset(parent,-1,sizeof(parent));
vector<int>v, v2, vind;
queue<int>q;
q.push(root);
visited[root] = true;
parent[root][d] = -1;
while(!q.empty()) {
int curr = q.front();
q.pop();
v.pb(parent[curr][d]);
v2.pb(curr);
vind.pb(pind[curr]);
for(auto i:gs[curr]) {
if(!visited[i] && type[i] == type[curr]) {
visited[i] = true;
parent[i][d] = curr;
dist[i][d] = dist[curr][d] + 1;
q.push(i);
}
}
}
if(v.size() == 1) return v2[0];
// perform binary search
for(int i=0;i<v.size();i++) {
if(v[i] == -1) continue; // root
int cind = vind[i];
w[cind] = 1;
}
for(int i=0;i<m;i++) {
if(!important[i]) w[i] = 1;
}
int li = 0, ri = v.size() - 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;i++) {
if(v[i] == -1) continue; // root
int cind = vind[i];
w[cind] = 0;
}
ll cdist = ask(w);
for(int i=0;i<=mid;i++) {
if(v[i] == -1) continue; // root
int cind = vind[i];
w[cind] = 1;
}
if(cdist == dista) {
ri = mid;
}
else {
li = mid + 1;
}
}
for(int i=0;i<v.size();i++) {
if(v[i] == -1) continue; // root
int cind = vind[i];
w[cind] = 0;
}
for(int i=0;i<m;i++) {
if(!important[i]) w[i] = 0;
}
return v2[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));
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];
important[x] = true;
//cout<<u<<" "<<v<<"\n";
bfs(u, v);
//preprocess();
//memset(visited,false,sizeof(visited));
//cout<<"Ans: "<<xx<<" "<<yy<<"\n";
answer(solve(u, 0), solve(v, 1));
}
Compilation message
highway.cpp: In function 'int solve(int, int)':
highway.cpp:91:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++) {
~^~~~~~~~~
highway.cpp:128:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++) {
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11000 KB |
Output is correct |
2 |
Correct |
12 ms |
11048 KB |
Output is correct |
3 |
Correct |
12 ms |
11008 KB |
Output is correct |
4 |
Correct |
12 ms |
11000 KB |
Output is correct |
5 |
Correct |
12 ms |
11040 KB |
Output is correct |
6 |
Correct |
12 ms |
11044 KB |
Output is correct |
7 |
Correct |
12 ms |
11048 KB |
Output is correct |
8 |
Correct |
12 ms |
11040 KB |
Output is correct |
9 |
Correct |
12 ms |
11028 KB |
Output is correct |
10 |
Correct |
12 ms |
11000 KB |
Output is correct |
11 |
Correct |
11 ms |
11044 KB |
Output is correct |
12 |
Correct |
12 ms |
11048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
11128 KB |
Output is correct |
2 |
Correct |
35 ms |
12316 KB |
Output is correct |
3 |
Correct |
266 ms |
23204 KB |
Output is correct |
4 |
Correct |
271 ms |
22820 KB |
Output is correct |
5 |
Correct |
299 ms |
22824 KB |
Output is correct |
6 |
Correct |
280 ms |
23112 KB |
Output is correct |
7 |
Correct |
254 ms |
22816 KB |
Output is correct |
8 |
Correct |
258 ms |
22808 KB |
Output is correct |
9 |
Correct |
238 ms |
22860 KB |
Output is correct |
10 |
Correct |
256 ms |
22888 KB |
Output is correct |
11 |
Correct |
354 ms |
21712 KB |
Output is correct |
12 |
Correct |
338 ms |
22264 KB |
Output is correct |
13 |
Correct |
302 ms |
22392 KB |
Output is correct |
14 |
Correct |
232 ms |
22364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
12264 KB |
Output is correct |
2 |
Correct |
51 ms |
13356 KB |
Output is correct |
3 |
Correct |
66 ms |
14748 KB |
Output is correct |
4 |
Correct |
172 ms |
21484 KB |
Output is correct |
5 |
Correct |
191 ms |
21480 KB |
Output is correct |
6 |
Correct |
191 ms |
22004 KB |
Output is correct |
7 |
Correct |
170 ms |
22384 KB |
Output is correct |
8 |
Correct |
193 ms |
21288 KB |
Output is correct |
9 |
Correct |
195 ms |
22252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
11132 KB |
Output is correct |
2 |
Correct |
36 ms |
12408 KB |
Output is correct |
3 |
Correct |
185 ms |
20540 KB |
Output is correct |
4 |
Correct |
243 ms |
22824 KB |
Output is correct |
5 |
Correct |
259 ms |
22784 KB |
Output is correct |
6 |
Correct |
270 ms |
22816 KB |
Output is correct |
7 |
Correct |
258 ms |
22824 KB |
Output is correct |
8 |
Correct |
267 ms |
22808 KB |
Output is correct |
9 |
Correct |
263 ms |
22796 KB |
Output is correct |
10 |
Correct |
255 ms |
22844 KB |
Output is correct |
11 |
Correct |
274 ms |
22408 KB |
Output is correct |
12 |
Correct |
292 ms |
22328 KB |
Output is correct |
13 |
Correct |
288 ms |
22272 KB |
Output is correct |
14 |
Correct |
240 ms |
21736 KB |
Output is correct |
15 |
Correct |
248 ms |
22916 KB |
Output is correct |
16 |
Correct |
212 ms |
22948 KB |
Output is correct |
17 |
Correct |
295 ms |
22368 KB |
Output is correct |
18 |
Correct |
271 ms |
22384 KB |
Output is correct |
19 |
Correct |
270 ms |
22816 KB |
Output is correct |
20 |
Correct |
253 ms |
22476 KB |
Output is correct |
21 |
Correct |
201 ms |
23860 KB |
Output is correct |
22 |
Correct |
228 ms |
23864 KB |
Output is correct |
23 |
Correct |
215 ms |
22808 KB |
Output is correct |
24 |
Correct |
202 ms |
22732 KB |
Output is correct |
25 |
Correct |
249 ms |
22524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
12376 KB |
Output is correct |
2 |
Correct |
33 ms |
12420 KB |
Output is correct |
3 |
Correct |
328 ms |
23176 KB |
Output is correct |
4 |
Correct |
304 ms |
23644 KB |
Output is correct |
5 |
Correct |
374 ms |
24168 KB |
Output is correct |
6 |
Incorrect |
339 ms |
24672 KB |
Output is incorrect: {s, t} is wrong. |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
12368 KB |
Output is correct |
2 |
Correct |
57 ms |
12392 KB |
Output is correct |
3 |
Correct |
289 ms |
22660 KB |
Output is correct |
4 |
Incorrect |
314 ms |
23368 KB |
Output is incorrect: {s, t} is wrong. |
5 |
Halted |
0 ms |
0 KB |
- |