#include "highway.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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, ll &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;
}
return l;
}
vector<int> bfs(int &n, int &m, int &a, int &b, ll &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, ll &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<pii> sbt[2];
for(int i=0; i<n; i++){
if(dist[0][i] <= dist[1][i]) sbt[0].push_back({dist[0][i], i});
else sbt[1].push_back({dist[1][i], i});
}
for(int i=0; i<2; i++) sort(sbt[i].begin(), sbt[i].end());
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].s] = 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].s;
else ans.s = sbt[j][ini].s;
}
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
ll 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
highway.cpp: In function 'pii find_endpoints(int&, int&, std::vector<int>&, std::vector<int>&, int&, int&, ll&, int&)':
highway.cpp:67:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=meio; i<sbt[j].size(); i++) mark[sbt[j][i].s] = 1;
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2376 KB |
Output is correct |
2 |
Correct |
2 ms |
2376 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
2 ms |
2376 KB |
Output is correct |
5 |
Correct |
2 ms |
2376 KB |
Output is correct |
6 |
Correct |
2 ms |
2376 KB |
Output is correct |
7 |
Correct |
2 ms |
2376 KB |
Output is correct |
8 |
Correct |
3 ms |
2376 KB |
Output is correct |
9 |
Correct |
2 ms |
2412 KB |
Output is correct |
10 |
Correct |
2 ms |
2376 KB |
Output is correct |
11 |
Correct |
2 ms |
2376 KB |
Output is correct |
12 |
Correct |
2 ms |
2376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2376 KB |
Output is correct |
2 |
Correct |
16 ms |
3144 KB |
Output is correct |
3 |
Correct |
147 ms |
9376 KB |
Output is correct |
4 |
Correct |
164 ms |
9408 KB |
Output is correct |
5 |
Correct |
145 ms |
9356 KB |
Output is correct |
6 |
Correct |
184 ms |
9452 KB |
Output is correct |
7 |
Correct |
171 ms |
9428 KB |
Output is correct |
8 |
Correct |
147 ms |
9348 KB |
Output is correct |
9 |
Correct |
188 ms |
9392 KB |
Output is correct |
10 |
Correct |
194 ms |
9432 KB |
Output is correct |
11 |
Correct |
187 ms |
8976 KB |
Output is correct |
12 |
Correct |
224 ms |
9172 KB |
Output is correct |
13 |
Correct |
137 ms |
9168 KB |
Output is correct |
14 |
Correct |
193 ms |
9168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
3144 KB |
Output is correct |
2 |
Correct |
37 ms |
3812 KB |
Output is correct |
3 |
Correct |
53 ms |
4480 KB |
Output is correct |
4 |
Correct |
165 ms |
8956 KB |
Output is correct |
5 |
Correct |
161 ms |
8952 KB |
Output is correct |
6 |
Correct |
157 ms |
9276 KB |
Output is correct |
7 |
Correct |
124 ms |
9224 KB |
Output is correct |
8 |
Correct |
111 ms |
8932 KB |
Output is correct |
9 |
Correct |
164 ms |
9200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2376 KB |
Output is correct |
2 |
Correct |
17 ms |
3264 KB |
Output is correct |
3 |
Correct |
156 ms |
7932 KB |
Output is correct |
4 |
Correct |
169 ms |
9256 KB |
Output is correct |
5 |
Correct |
186 ms |
9324 KB |
Output is correct |
6 |
Correct |
178 ms |
9300 KB |
Output is correct |
7 |
Correct |
182 ms |
9424 KB |
Output is correct |
8 |
Correct |
136 ms |
9448 KB |
Output is correct |
9 |
Correct |
216 ms |
9416 KB |
Output is correct |
10 |
Correct |
143 ms |
9324 KB |
Output is correct |
11 |
Correct |
146 ms |
9128 KB |
Output is correct |
12 |
Correct |
181 ms |
9172 KB |
Output is correct |
13 |
Correct |
186 ms |
9196 KB |
Output is correct |
14 |
Correct |
149 ms |
8784 KB |
Output is correct |
15 |
Correct |
190 ms |
9396 KB |
Output is correct |
16 |
Correct |
141 ms |
9436 KB |
Output is correct |
17 |
Correct |
141 ms |
9172 KB |
Output is correct |
18 |
Correct |
135 ms |
9184 KB |
Output is correct |
19 |
Correct |
190 ms |
9500 KB |
Output is correct |
20 |
Correct |
190 ms |
9188 KB |
Output is correct |
21 |
Correct |
179 ms |
9832 KB |
Output is correct |
22 |
Correct |
175 ms |
9904 KB |
Output is correct |
23 |
Correct |
184 ms |
9572 KB |
Output is correct |
24 |
Correct |
141 ms |
9536 KB |
Output is correct |
25 |
Correct |
139 ms |
9200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
3232 KB |
Output is correct |
2 |
Correct |
24 ms |
3216 KB |
Output is correct |
3 |
Correct |
169 ms |
9620 KB |
Output is correct |
4 |
Correct |
176 ms |
9908 KB |
Output is correct |
5 |
Correct |
282 ms |
10604 KB |
Output is correct |
6 |
Correct |
243 ms |
10668 KB |
Output is correct |
7 |
Correct |
283 ms |
10780 KB |
Output is correct |
8 |
Correct |
203 ms |
10608 KB |
Output is correct |
9 |
Correct |
165 ms |
8504 KB |
Output is correct |
10 |
Correct |
167 ms |
9048 KB |
Output is correct |
11 |
Correct |
222 ms |
8844 KB |
Output is correct |
12 |
Correct |
218 ms |
9892 KB |
Output is correct |
13 |
Correct |
222 ms |
10408 KB |
Output is correct |
14 |
Correct |
288 ms |
10436 KB |
Output is correct |
15 |
Correct |
270 ms |
10284 KB |
Output is correct |
16 |
Correct |
252 ms |
8908 KB |
Output is correct |
17 |
Correct |
150 ms |
9844 KB |
Output is correct |
18 |
Correct |
158 ms |
9892 KB |
Output is correct |
19 |
Correct |
144 ms |
9820 KB |
Output is correct |
20 |
Correct |
150 ms |
10052 KB |
Output is correct |
21 |
Correct |
220 ms |
10172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3144 KB |
Output is correct |
2 |
Correct |
23 ms |
3264 KB |
Output is correct |
3 |
Correct |
218 ms |
9640 KB |
Output is correct |
4 |
Correct |
200 ms |
9840 KB |
Output is correct |
5 |
Correct |
241 ms |
9948 KB |
Output is correct |
6 |
Correct |
281 ms |
11016 KB |
Output is correct |
7 |
Correct |
158 ms |
9776 KB |
Output is correct |
8 |
Correct |
170 ms |
9768 KB |
Output is correct |
9 |
Correct |
246 ms |
9852 KB |
Output is correct |
10 |
Correct |
278 ms |
10728 KB |
Output is correct |
11 |
Correct |
269 ms |
10648 KB |
Output is correct |
12 |
Correct |
275 ms |
10560 KB |
Output is correct |
13 |
Correct |
231 ms |
8876 KB |
Output is correct |
14 |
Correct |
171 ms |
9012 KB |
Output is correct |
15 |
Correct |
214 ms |
9048 KB |
Output is correct |
16 |
Correct |
158 ms |
9028 KB |
Output is correct |
17 |
Correct |
191 ms |
9080 KB |
Output is correct |
18 |
Correct |
232 ms |
9056 KB |
Output is correct |
19 |
Correct |
212 ms |
10368 KB |
Output is correct |
20 |
Correct |
263 ms |
10308 KB |
Output is correct |
21 |
Correct |
291 ms |
10696 KB |
Output is correct |
22 |
Correct |
266 ms |
10512 KB |
Output is correct |
23 |
Correct |
280 ms |
10640 KB |
Output is correct |
24 |
Correct |
217 ms |
10484 KB |
Output is correct |
25 |
Correct |
224 ms |
10428 KB |
Output is correct |
26 |
Correct |
254 ms |
10420 KB |
Output is correct |
27 |
Correct |
178 ms |
10064 KB |
Output is correct |
28 |
Correct |
185 ms |
9864 KB |
Output is correct |
29 |
Correct |
190 ms |
10172 KB |
Output is correct |
30 |
Correct |
177 ms |
10056 KB |
Output is correct |
31 |
Correct |
189 ms |
9932 KB |
Output is correct |
32 |
Correct |
148 ms |
9844 KB |
Output is correct |
33 |
Correct |
152 ms |
9980 KB |
Output is correct |
34 |
Correct |
161 ms |
9880 KB |
Output is correct |
35 |
Correct |
194 ms |
9972 KB |
Output is correct |
36 |
Correct |
158 ms |
9808 KB |
Output is correct |
37 |
Correct |
156 ms |
10192 KB |
Output is correct |
38 |
Correct |
144 ms |
10064 KB |
Output is correct |
39 |
Correct |
195 ms |
10376 KB |
Output is correct |
40 |
Correct |
258 ms |
10152 KB |
Output is correct |
41 |
Correct |
260 ms |
10152 KB |
Output is correct |
42 |
Correct |
254 ms |
9916 KB |
Output is correct |