#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "highway.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
//namespace {
//
//constexpr int MAX_NUM_CALLS = 100;
//constexpr long long INF = 1LL << 61;
//
//int N, M, A, B, S, T;
//std::vector<int> U, V;
//std::vector<std::vector<std::pair<int, int>>> graph;
//
//bool answered, wrong_pair;
//int num_calls;
//
//int read_int() {
// int x;
// if (scanf("%d", &x) != 1) {
// fprintf(stderr, "Error while reading input\n");
// exit(1);
// }
// return x;
//}
//
//void wrong_answer(const char *MSG) {
// printf("Wrong Answer: %s\n", MSG);
// exit(0);
//}
//
//} // namespace
//
//long long ask(const std::vector<int> &w) {
// if (++num_calls > MAX_NUM_CALLS) {
// wrong_answer("more than 100 calls to ask");
// }
// if (w.size() != (size_t)M) {
// wrong_answer("w is invalid");
// }
// for (size_t i = 0; i < w.size(); ++i) {
// if (!(w[i] == 0 || w[i] == 1)) {
// wrong_answer("w is invalid");
// }
// }
//
// std::vector<bool> visited(N, false);
// std::vector<long long> current_dist(N, INF);
// std::queue<int> qa, qb;
// qa.push(S);
// current_dist[S] = 0;
// while (!qa.empty() || !qb.empty()) {
// int v;
// if (qb.empty() ||
// (!qa.empty() && current_dist[qa.front()] <= current_dist[qb.front()])) {
// v = qa.front();
// qa.pop();
// } else {
// v = qb.front();
// qb.pop();
// }
// if (visited[v]) {
// continue;
// }
// visited[v] = true;
// long long d = current_dist[v];
// if (v == T) {
// return d;
// }
// for (auto e : graph[v]) {
// int vv = e.first;
// int ei = e.second;
// if (!visited[vv]) {
// if (w[ei] == 0) {
// if (current_dist[vv] > d + A) {
// current_dist[vv] = d + A;
// qa.push(vv);
// }
// } else {
// if (current_dist[vv] > d + B) {
// current_dist[vv] = d + B;
// qb.push(vv);
// }
// }
// }
// }
// }
// return -1;
//}
//
//void answer(int s, int t) {
// if (answered) {
// wrong_answer("answered not exactly once");
// }
//
// if (!((s == S && t == T) || (s == T && t == S))) {
// wrong_pair = true;
// }
//
// answered = true;
//}
void find_pair(int n, vector<int> U, vector<int> V, int A, int B) {
vector <vector <pair <int, int>>> g(n), e(n);
vector <int> ord(n), id(n, -1);
int m = U.size();
for (int i = 0; i < m; ++i) {
g[U[i]].push_back({V[i], i});
g[V[i]].push_back({U[i], i});
// cout << U[i] << ' ' << V[i] << "asdsdfsdf\n";
}
int L = 0, R = m - 1;
vector <int> w(m, 0);
long long tot = ask(w);
while (L < R) {
int mid = L + R >> 1;
vector <int> w(m, 0);
for (int i = mid + 1; i < m; ++i) w[i] = 1;
if (ask(w) != tot) L = mid + 1;
else R = mid;
}
int x = L;
queue <int> q;
q.push(U[x]);
q.push(V[x]);
vector <int> type(n, -1);
type[U[x]] = 0;
type[V[x]] = 1;
while (q.size()) {
int u = q.front();
q.pop();
for (auto [v, i]: g[u]) {
if (type[v] != -1) continue;
type[v] = type[u];
id[v] = i;
e[u].push_back({v, i});
q.push(v);
}
}
// cout << x << '\n';
int ti = 0;
function <void(int)> dfs = [&](int u) {
ord[ti++] = u;
for (auto [v, i]: e[u]) {
dfs(v);
}
};
// cout << x << '\n';
dfs(U[x]);
// cout << U[x] << ' ' << V[x] << "aa\n";
// for (int i = 0; i < ti; ++i) cout << ord[i] << ' ';
// cout << '\n';
w.assign(m, -1);
// cout << id[0] << "aaa\n";
// for (int i = 0; i < n; ++i) cout << type[i] << ' ';
// cout << '\n';
for (int i = 0; i < n; ++i) {
if (i == U[x] || i == V[x]) continue;
w[id[i]] = 0;
}
w[x] = 0;
for (int i = 0; i < m; ++i) if (w[i] == -1) w[i] = 1;
tot = ask(w);
// cout << tot << "aafcdsfsdgf\n";
L = 0, R = ti - 1;
while (L < R) {
int mid = L + R >> 1;
vector <int> w(m, -1);
for (int i = 0; i <= mid; ++i) {
if (ord[i] == U[x] || ord[i] == V[x]) continue;
if (type[ord[i]] == 0) w[id[ord[i]]] = 0;
}
for (int i = 0; i < n; ++i) {
if (i == U[x] || i == V[x]) continue;
if (type[i] == 1) w[id[i]] = 0;
}
w[x] = 0;
for (int i = 0; i < m; ++i) if (w[i] == -1) w[i] = 1;
if (ask(w) == tot) R = mid;
else L = mid + 1;
}
int S = ord[L];
ti = 0;
dfs(V[x]);
// for (int i = 0; i < ti; ++i) cout << ord[i] << ' ';
// cout << '\n';
// for (int i = 0; i < ti; ++i) cout << ord[i] << ' ';
// cout << '\n';
L = 0, R = ti - 1;
while (L < R) {
int mid = L + R >> 1;
vector <int> w(m, -1);
for (int i = 0; i <= mid; ++i) {
if (ord[i] == U[x] || ord[i] == V[x]) continue;
if (type[ord[i]] == 1) w[id[ord[i]]] = 0;
}
for (int i = 0; i < n; ++i) {
if (i == U[x] || i == V[x]) continue;
if (type[i] == 0) w[id[i]] = 0;
}
w[x] = 0;
for (int i = 0; i < m; ++i) if (w[i] == -1) w[i] = 1;
if (ask(w) == tot) R = mid;
else L = mid + 1;
}
int T = ord[L];
// cout << S << ' ' << T << '\n';
answer(S, T);
}
/*
11 11 1 2 1 8
0 1
0 2
1 3
1 4
4 5
4 6
2 7
2 8
8 9
8 10
1 10
*/
//int main() {
//// freopen("IOI18_highway.inp", "r", stdin);
//// freopen("IOI18_highway.out", "w", stdout);
// N = read_int();
// M = read_int();
// A = read_int();
// B = read_int();
// S = read_int();
// T = read_int();
//// cout << S << ' ' << T << '\n';
//// cout << (S ^ T) << '\n';
// U.resize(M);
// V.resize(M);
// graph.assign(N, std::vector<std::pair<int, int>>());
// for (int i = 0; i < M; ++i) {
// U[i] = read_int();
// V[i] = read_int();
// graph[U[i]].push_back({V[i], i});
// graph[V[i]].push_back({U[i], i});
// }
//
// answered = false;
// wrong_pair = false;
// num_calls = 0;
// find_pair(N, U, V, A, B);
// if (!answered) {
// wrong_answer("answered not exactly once");
// }
// if (wrong_pair) {
//// cerr << N << ' ' << M << ' ' << A << ' ' << B << ' ' << S << ' ' << T << '\n';
//// for (int i = 0; i < M; ++i) cerr << U[i] << ' ' << V[i] << '\n';
//// assert(0);
// wrong_answer("{s, t} is wrong");
// }
// printf("Accepted: %d\n", num_calls);
// return 0;
//}
Compilation message
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:124:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
124 | int mid = L + R >> 1;
| ~~^~~
highway.cpp:140:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
140 | for (auto [v, i]: g[u]) {
| ^
highway.cpp: In lambda function:
highway.cpp:152:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
152 | for (auto [v, i]: e[u]) {
| ^
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:175:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
175 | int mid = L + R >> 1;
| ~~^~~
highway.cpp:199:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
199 | int mid = L + R >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
13 ms |
1656 KB |
Output is correct |
3 |
Correct |
152 ms |
12764 KB |
Output is correct |
4 |
Correct |
140 ms |
12776 KB |
Output is correct |
5 |
Correct |
116 ms |
12764 KB |
Output is correct |
6 |
Correct |
139 ms |
12756 KB |
Output is correct |
7 |
Correct |
131 ms |
12756 KB |
Output is correct |
8 |
Correct |
113 ms |
12908 KB |
Output is correct |
9 |
Correct |
147 ms |
12884 KB |
Output is correct |
10 |
Correct |
128 ms |
12864 KB |
Output is correct |
11 |
Correct |
162 ms |
14720 KB |
Output is correct |
12 |
Correct |
156 ms |
15184 KB |
Output is correct |
13 |
Correct |
182 ms |
14732 KB |
Output is correct |
14 |
Correct |
133 ms |
14912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2292 KB |
Output is correct |
2 |
Correct |
24 ms |
3896 KB |
Output is correct |
3 |
Correct |
32 ms |
6276 KB |
Output is correct |
4 |
Correct |
100 ms |
16544 KB |
Output is correct |
5 |
Correct |
106 ms |
16880 KB |
Output is correct |
6 |
Correct |
100 ms |
18060 KB |
Output is correct |
7 |
Correct |
98 ms |
18684 KB |
Output is correct |
8 |
Correct |
115 ms |
17028 KB |
Output is correct |
9 |
Correct |
100 ms |
17652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
336 KB |
Output is correct |
2 |
Correct |
14 ms |
1652 KB |
Output is correct |
3 |
Correct |
95 ms |
10008 KB |
Output is correct |
4 |
Correct |
129 ms |
12776 KB |
Output is correct |
5 |
Correct |
117 ms |
12776 KB |
Output is correct |
6 |
Correct |
141 ms |
12764 KB |
Output is correct |
7 |
Correct |
130 ms |
12772 KB |
Output is correct |
8 |
Correct |
135 ms |
12740 KB |
Output is correct |
9 |
Correct |
170 ms |
12772 KB |
Output is correct |
10 |
Correct |
147 ms |
12776 KB |
Output is correct |
11 |
Correct |
155 ms |
14472 KB |
Output is correct |
12 |
Correct |
161 ms |
15192 KB |
Output is correct |
13 |
Correct |
172 ms |
14752 KB |
Output is correct |
14 |
Correct |
188 ms |
14996 KB |
Output is correct |
15 |
Correct |
111 ms |
12772 KB |
Output is correct |
16 |
Correct |
109 ms |
12772 KB |
Output is correct |
17 |
Correct |
151 ms |
14724 KB |
Output is correct |
18 |
Correct |
151 ms |
14872 KB |
Output is correct |
19 |
Correct |
110 ms |
12772 KB |
Output is correct |
20 |
Correct |
130 ms |
15436 KB |
Output is correct |
21 |
Correct |
109 ms |
12620 KB |
Output is correct |
22 |
Correct |
120 ms |
12612 KB |
Output is correct |
23 |
Correct |
138 ms |
12284 KB |
Output is correct |
24 |
Correct |
151 ms |
12716 KB |
Output is correct |
25 |
Correct |
150 ms |
18180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1736 KB |
Output is correct |
2 |
Correct |
20 ms |
1664 KB |
Output is correct |
3 |
Correct |
172 ms |
13200 KB |
Output is correct |
4 |
Correct |
143 ms |
13820 KB |
Output is correct |
5 |
Correct |
229 ms |
14436 KB |
Output is correct |
6 |
Correct |
199 ms |
14432 KB |
Output is correct |
7 |
Correct |
202 ms |
14504 KB |
Output is correct |
8 |
Correct |
212 ms |
14484 KB |
Output is correct |
9 |
Correct |
117 ms |
8548 KB |
Output is correct |
10 |
Correct |
127 ms |
8736 KB |
Output is correct |
11 |
Correct |
150 ms |
9816 KB |
Output is correct |
12 |
Correct |
199 ms |
12596 KB |
Output is correct |
13 |
Correct |
196 ms |
13608 KB |
Output is correct |
14 |
Correct |
221 ms |
14520 KB |
Output is correct |
15 |
Correct |
205 ms |
14536 KB |
Output is correct |
16 |
Correct |
164 ms |
9864 KB |
Output is correct |
17 |
Correct |
136 ms |
12464 KB |
Output is correct |
18 |
Correct |
138 ms |
12720 KB |
Output is correct |
19 |
Correct |
117 ms |
12696 KB |
Output is correct |
20 |
Correct |
117 ms |
12672 KB |
Output is correct |
21 |
Correct |
210 ms |
14912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1744 KB |
Output is correct |
2 |
Correct |
14 ms |
1668 KB |
Output is correct |
3 |
Correct |
139 ms |
13224 KB |
Output is correct |
4 |
Correct |
178 ms |
13388 KB |
Output is correct |
5 |
Correct |
199 ms |
13696 KB |
Output is correct |
6 |
Correct |
209 ms |
14532 KB |
Output is correct |
7 |
Correct |
173 ms |
13228 KB |
Output is correct |
8 |
Correct |
183 ms |
13436 KB |
Output is correct |
9 |
Correct |
151 ms |
13600 KB |
Output is correct |
10 |
Correct |
236 ms |
14424 KB |
Output is correct |
11 |
Correct |
221 ms |
14500 KB |
Output is correct |
12 |
Correct |
233 ms |
14552 KB |
Output is correct |
13 |
Correct |
126 ms |
9812 KB |
Output is correct |
14 |
Correct |
159 ms |
8840 KB |
Output is correct |
15 |
Correct |
119 ms |
9804 KB |
Output is correct |
16 |
Correct |
117 ms |
8736 KB |
Output is correct |
17 |
Correct |
136 ms |
9824 KB |
Output is correct |
18 |
Correct |
127 ms |
8760 KB |
Output is correct |
19 |
Correct |
203 ms |
12664 KB |
Output is correct |
20 |
Correct |
219 ms |
13584 KB |
Output is correct |
21 |
Correct |
204 ms |
14680 KB |
Output is correct |
22 |
Correct |
218 ms |
14476 KB |
Output is correct |
23 |
Correct |
230 ms |
14532 KB |
Output is correct |
24 |
Correct |
196 ms |
14628 KB |
Output is correct |
25 |
Correct |
231 ms |
14524 KB |
Output is correct |
26 |
Correct |
196 ms |
14636 KB |
Output is correct |
27 |
Correct |
122 ms |
12876 KB |
Output is correct |
28 |
Correct |
116 ms |
12604 KB |
Output is correct |
29 |
Correct |
111 ms |
13132 KB |
Output is correct |
30 |
Correct |
139 ms |
12708 KB |
Output is correct |
31 |
Correct |
160 ms |
12708 KB |
Output is correct |
32 |
Correct |
145 ms |
12480 KB |
Output is correct |
33 |
Correct |
132 ms |
12836 KB |
Output is correct |
34 |
Correct |
133 ms |
12680 KB |
Output is correct |
35 |
Correct |
95 ms |
12560 KB |
Output is correct |
36 |
Correct |
129 ms |
12420 KB |
Output is correct |
37 |
Correct |
116 ms |
12916 KB |
Output is correct |
38 |
Correct |
123 ms |
12696 KB |
Output is correct |
39 |
Correct |
188 ms |
15260 KB |
Output is correct |
40 |
Correct |
233 ms |
15392 KB |
Output is correct |
41 |
Correct |
242 ms |
14832 KB |
Output is correct |
42 |
Correct |
226 ms |
14740 KB |
Output is correct |