#include <bits/stdc++.h>
#include "grader.h"
using namespace std;
vector<vector<int>> g, pa;
vector<int> dep, ord;
void dfs(int i, int p) {
pa[i][0] = p;
ord.push_back(i);
dep[i] = dep[p] + 1;
for (int j : g[i]) if (j != p)
dfs(j, i);
}
int lca(int i, int j) {
if (dep[i] < dep[j]) swap(i, j);
for (int ii = 9; ~ii; --ii) if (dep[pa[i][ii]] >= dep[j])
i = pa[i][ii];
for (int ii = 9; ~ii; --ii) if (pa[i][ii] != pa[j][ii])
i = pa[i][ii], j = pa[j][ii];
return i == j ? i : pa[i][0];
}
int findEgg (int N, vector < pair < int, int > > bridges)
{
g.assign(N, vector<int>()), pa.assign(N, vector<int>(10));
dep.resize(N), dep[0] = -1;
for (auto i : bridges) {
int x = i.first - 1, y = i.second - 1;
g[x].push_back(y), g[y].push_back(x);
}
dfs(0, 0);
for (int i = 1; i < 10; ++i) for (int j = 0; j < N; ++j)
pa[j][i] = pa[pa[j][i - 1]][i - 1];
int l = 0, r = N;
while (l < r - 1) {
int m = l + r >> 1, x = ord[l];
for (int i = l + 1; i < m; ++i)
x = lca(x, ord[i]);
vector<bool> vis(N, false);
vector<int> ans(1, x + 1);
vis[x] = true;
for (int i = l; i < m; ++i) for (int j = ord[i]; !vis[j]; j = pa[j][0])
vis[j] = true, ans.push_back(j + 1);
if (query(ans)) r = m;
else l = m;
}
return ord[l] + 1;
}
Compilation message
eastereggs.cpp: In function 'int findEgg(int, std::vector<std::pair<int, int> >)':
eastereggs.cpp:35:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int m = l + r >> 1, x = ord[l];
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
200 KB |
Number of queries: 4 |
2 |
Correct |
1 ms |
200 KB |
Number of queries: 4 |
3 |
Correct |
1 ms |
200 KB |
Number of queries: 4 |
4 |
Correct |
1 ms |
204 KB |
Number of queries: 4 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
400 KB |
Number of queries: 8 |
2 |
Correct |
10 ms |
472 KB |
Number of queries: 9 |
3 |
Correct |
14 ms |
464 KB |
Number of queries: 9 |
4 |
Correct |
14 ms |
576 KB |
Number of queries: 9 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
500 KB |
Number of queries: 9 |
2 |
Correct |
13 ms |
584 KB |
Number of queries: 9 |
3 |
Correct |
14 ms |
568 KB |
Number of queries: 9 |
4 |
Correct |
14 ms |
584 KB |
Number of queries: 9 |