#include <bits/stdc++.h>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 0;
const int OOO = 1;
using namespace std;
void answer(int s, int t);
ll ask(const vector<int> &w);
struct edge {
int to, i;
edge() {}
edge(int tt, int ii) {
to = tt;
i = ii;
}
};
vector<int> w;
vector<int> ord;
vector<int> special;
int n, m;
ll d;
vector<vector<edge>> g;
vector<int> vis, dep;
int crit;
ll f(int x) {
for (int i = 0; i < x; i++) w[ord[i]] = 0;
for (int i = x; i < ord.size(); i++) w[ord[i]] = 1;
return ask(w);
}
void bfs(int v) {
ord.clear();
vis.resize(n, 0);
dep.resize(n, 0);
vis[v] = 1;
queue<int> q;
q.push(v);
while (!q.empty()) {
int x = q.front();
q.pop();
for (const auto &i : g[x])
if (!vis[i.to]) {
dep[i.to] = dep[x] + 1;
ord.push_back(i.i);
vis[i.to] = 1;
q.push(i.to);
}
}
}
void dfs(int v) {
vis[v] = 1;
for (const auto &i : g[v])
if (!vis[i.to] && w[i.i] == 0) {
special[i.i] = 1;
dfs(i.to);
}
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
if (N == 2) {
answer(0, 1);
return;
}
n = N;
g.resize(n);
m = U.size();
w.resize(m);
for (int i = 0; i < m; i++) {
g[U[i]].push_back(edge(V[i], i));
g[V[i]].push_back(edge(U[i], i));
}
for (auto &i : w) i = 0;
d = ask(w);
if (OO) cout << "d = " << d << '\n';
int lo, hi, mid;
// 1: find edge on shortest path
ord.resize(m);
for (int i = 0; i < m; i++) ord[i] = i;
lo = 0, hi = m - 1;
while (lo < hi) {
mid = (lo + hi + 1) / 2;
if (f(mid) == d) hi = mid - 1;
else lo = mid;
}
crit = lo;
if (OO) cout << "critical edge index " << crit << ", (" << U[crit] << ", " << V[crit] << ")\n";
// 2: root at 1 end of the edge, and bsearch in the subtree of the other for S/T
for (auto &i : w) i = 1;
// prioritize the critical edge
for (auto &i : g[U[crit]])
if (i.i == crit) {
swap(i, g[U[crit]][0]);
}
bfs(U[crit]);
for (auto &i : ord) w[i] = 0;
// determine all edges in the subtree of the other end
for (auto &i : vis) i = 0;
vis[U[crit]] = 1;
special.resize(m, 0);
special[crit] = 1;
dfs(V[crit]);
vector<int> ord1, ord2;
for (const auto &i : ord)
if (special[i])
ord1.push_back(i);
else
ord2.push_back(i);
if (OO) {
cout << "rooted at " << U[crit] << '\n';
cout << "bfs: ";
for (const auto &i : ord) cout << i << " "; cout << '\n';
cout << "ord1: ";
for (const auto &i : ord1) cout << i << " "; cout << '\n';
cout << "ord2: ";
for (const auto &i : ord2) cout << i << " "; cout << '\n';
cout << "dep: ";
for (const auto &i : dep) cout << i << " "; cout << '\n';
}
// bsearch in the subtree
ord = ord1;
lo = 0, hi = (int)ord.size() - 1;
while (lo < hi) {
mid = (lo + hi + 1) / 2;
if (f(mid) == d) hi = mid - 1;
else lo = mid;
}
int S, T;
if (OO) {
cout << "first bsearch yields edge " << ord[lo] << '\n';
}
if (dep[U[ord[lo]]] > dep[V[ord[lo]]]) S = U[ord[lo]];
else S = V[ord[lo]];
if (OO) cout << "resulting in S = " << S << '\n';
if ((ll)A * dep[S] == d) {
T = U[crit];
answer(S, T);
return;
}
// bsearch on everything else
for (auto &i : ord1) w[i] = 0;
ord = ord2;
lo = 0, hi = (int)ord.size() - 1;
while (lo < hi) {
mid = (lo + hi + 1) / 2;
if (f(mid) == d) hi = mid - 1;
else lo = mid;
}
if (OO) {
cout << "second bsearch yields edge " << ord[lo] << '\n';
}
if (dep[U[ord[lo]]] > dep[V[ord[lo]]]) T = U[ord[lo]];
else T = V[ord[lo]];
if (OO) cout << "resulting in T = " << T << '\n';
answer(S, T);
}
Compilation message
highway.cpp: In function 'll f(int)':
highway.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = x; i < ord.size(); i++) w[ord[i]] = 1;
~~^~~~~~~~~~~~
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:125:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (const auto &i : ord) cout << i << " "; cout << '\n';
^~~
highway.cpp:125:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (const auto &i : ord) cout << i << " "; cout << '\n';
^~~~
highway.cpp:127:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (const auto &i : ord1) cout << i << " "; cout << '\n';
^~~
highway.cpp:127:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (const auto &i : ord1) cout << i << " "; cout << '\n';
^~~~
highway.cpp:129:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (const auto &i : ord2) cout << i << " "; cout << '\n';
^~~
highway.cpp:129:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (const auto &i : ord2) cout << i << " "; cout << '\n';
^~~~
highway.cpp:131:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (const auto &i : dep) cout << i << " "; cout << '\n';
^~~
highway.cpp:131:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (const auto &i : dep) cout << i << " "; cout << '\n';
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
412 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
320 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
328 KB |
Output is correct |
9 |
Correct |
2 ms |
252 KB |
Output is correct |
10 |
Correct |
2 ms |
328 KB |
Output is correct |
11 |
Correct |
2 ms |
248 KB |
Output is correct |
12 |
Correct |
2 ms |
248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
45 ms |
1340 KB |
Output is correct |
3 |
Correct |
258 ms |
9596 KB |
Output is correct |
4 |
Correct |
207 ms |
9592 KB |
Output is correct |
5 |
Correct |
228 ms |
9588 KB |
Output is correct |
6 |
Correct |
194 ms |
9568 KB |
Output is correct |
7 |
Correct |
203 ms |
9592 KB |
Output is correct |
8 |
Correct |
183 ms |
9600 KB |
Output is correct |
9 |
Correct |
199 ms |
9688 KB |
Output is correct |
10 |
Correct |
207 ms |
9596 KB |
Output is correct |
11 |
Correct |
229 ms |
9612 KB |
Output is correct |
12 |
Correct |
219 ms |
10064 KB |
Output is correct |
13 |
Correct |
221 ms |
9160 KB |
Output is correct |
14 |
Correct |
224 ms |
9112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1528 KB |
Output is correct |
2 |
Correct |
39 ms |
2676 KB |
Output is correct |
3 |
Correct |
61 ms |
3404 KB |
Output is correct |
4 |
Correct |
168 ms |
10232 KB |
Output is correct |
5 |
Correct |
132 ms |
10792 KB |
Output is correct |
6 |
Correct |
177 ms |
9548 KB |
Output is correct |
7 |
Correct |
175 ms |
9256 KB |
Output is correct |
8 |
Correct |
167 ms |
9892 KB |
Output is correct |
9 |
Correct |
175 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
45 ms |
1372 KB |
Output is correct |
3 |
Correct |
161 ms |
7660 KB |
Output is correct |
4 |
Correct |
193 ms |
9736 KB |
Output is correct |
5 |
Correct |
167 ms |
9592 KB |
Output is correct |
6 |
Correct |
175 ms |
9584 KB |
Output is correct |
7 |
Correct |
201 ms |
9600 KB |
Output is correct |
8 |
Correct |
204 ms |
9588 KB |
Output is correct |
9 |
Correct |
218 ms |
9468 KB |
Output is correct |
10 |
Correct |
198 ms |
9592 KB |
Output is correct |
11 |
Correct |
220 ms |
9632 KB |
Output is correct |
12 |
Correct |
193 ms |
9344 KB |
Output is correct |
13 |
Correct |
213 ms |
9444 KB |
Output is correct |
14 |
Correct |
222 ms |
9828 KB |
Output is correct |
15 |
Correct |
194 ms |
9588 KB |
Output is correct |
16 |
Correct |
207 ms |
9604 KB |
Output is correct |
17 |
Correct |
260 ms |
9852 KB |
Output is correct |
18 |
Correct |
201 ms |
9876 KB |
Output is correct |
19 |
Correct |
213 ms |
9588 KB |
Output is correct |
20 |
Correct |
203 ms |
10192 KB |
Output is correct |
21 |
Correct |
185 ms |
10040 KB |
Output is correct |
22 |
Correct |
164 ms |
10124 KB |
Output is correct |
23 |
Correct |
169 ms |
9776 KB |
Output is correct |
24 |
Correct |
182 ms |
9840 KB |
Output is correct |
25 |
Correct |
204 ms |
9256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1444 KB |
Output is correct |
2 |
Correct |
29 ms |
1400 KB |
Output is correct |
3 |
Correct |
231 ms |
9992 KB |
Output is correct |
4 |
Correct |
232 ms |
10588 KB |
Output is correct |
5 |
Correct |
304 ms |
11516 KB |
Output is correct |
6 |
Correct |
311 ms |
11552 KB |
Output is correct |
7 |
Correct |
306 ms |
11712 KB |
Output is correct |
8 |
Correct |
290 ms |
11628 KB |
Output is correct |
9 |
Correct |
228 ms |
8008 KB |
Output is correct |
10 |
Correct |
246 ms |
8332 KB |
Output is correct |
11 |
Correct |
262 ms |
9056 KB |
Output is correct |
12 |
Correct |
303 ms |
10676 KB |
Output is correct |
13 |
Correct |
268 ms |
11204 KB |
Output is correct |
14 |
Correct |
319 ms |
11520 KB |
Output is correct |
15 |
Correct |
314 ms |
11668 KB |
Output is correct |
16 |
Correct |
269 ms |
9236 KB |
Output is correct |
17 |
Correct |
193 ms |
9884 KB |
Output is correct |
18 |
Correct |
209 ms |
10068 KB |
Output is correct |
19 |
Correct |
180 ms |
9952 KB |
Output is correct |
20 |
Correct |
203 ms |
9980 KB |
Output is correct |
21 |
Correct |
310 ms |
11996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1380 KB |
Output is correct |
2 |
Correct |
21 ms |
1352 KB |
Output is correct |
3 |
Correct |
254 ms |
10024 KB |
Output is correct |
4 |
Correct |
286 ms |
10268 KB |
Output is correct |
5 |
Correct |
250 ms |
10576 KB |
Output is correct |
6 |
Correct |
299 ms |
11628 KB |
Output is correct |
7 |
Correct |
248 ms |
10148 KB |
Output is correct |
8 |
Correct |
289 ms |
10296 KB |
Output is correct |
9 |
Correct |
354 ms |
10580 KB |
Output is correct |
10 |
Correct |
292 ms |
11596 KB |
Output is correct |
11 |
Correct |
321 ms |
11488 KB |
Output is correct |
12 |
Correct |
310 ms |
11580 KB |
Output is correct |
13 |
Correct |
253 ms |
9204 KB |
Output is correct |
14 |
Correct |
240 ms |
8328 KB |
Output is correct |
15 |
Correct |
259 ms |
9088 KB |
Output is correct |
16 |
Correct |
240 ms |
8336 KB |
Output is correct |
17 |
Correct |
273 ms |
9088 KB |
Output is correct |
18 |
Correct |
230 ms |
8404 KB |
Output is correct |
19 |
Correct |
277 ms |
10700 KB |
Output is correct |
20 |
Correct |
280 ms |
11156 KB |
Output is correct |
21 |
Correct |
313 ms |
11744 KB |
Output is correct |
22 |
Correct |
311 ms |
11596 KB |
Output is correct |
23 |
Correct |
319 ms |
11688 KB |
Output is correct |
24 |
Correct |
302 ms |
11604 KB |
Output is correct |
25 |
Correct |
309 ms |
11608 KB |
Output is correct |
26 |
Correct |
294 ms |
11728 KB |
Output is correct |
27 |
Correct |
211 ms |
9904 KB |
Output is correct |
28 |
Correct |
189 ms |
10036 KB |
Output is correct |
29 |
Correct |
204 ms |
10236 KB |
Output is correct |
30 |
Correct |
204 ms |
10036 KB |
Output is correct |
31 |
Correct |
194 ms |
10008 KB |
Output is correct |
32 |
Correct |
191 ms |
9888 KB |
Output is correct |
33 |
Correct |
218 ms |
10168 KB |
Output is correct |
34 |
Correct |
189 ms |
9896 KB |
Output is correct |
35 |
Correct |
177 ms |
9944 KB |
Output is correct |
36 |
Correct |
206 ms |
9840 KB |
Output is correct |
37 |
Correct |
203 ms |
10160 KB |
Output is correct |
38 |
Correct |
228 ms |
10076 KB |
Output is correct |
39 |
Correct |
313 ms |
12208 KB |
Output is correct |
40 |
Correct |
316 ms |
12208 KB |
Output is correct |
41 |
Correct |
277 ms |
11992 KB |
Output is correct |
42 |
Correct |
296 ms |
11764 KB |
Output is correct |