#include "highway.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
typedef long long llong;
const int MAXN = 90000 + 10;
const int INF = 1e9;
int n, m;
int sz[MAXN];
int par[MAXN];
int parEdge[MAXN];
std::queue <int> q;
std::vector <int> w;
std::vector <std::pair <int,int>> g[MAXN];
std::queue <std::pair <int,int>> qDist;
void initDFS(int node, int p)
{
sz[node] = 1;
for (const auto &[u, idx] : g[node])
{
if (u == p)
{
continue;
}
par[u] = node;
parEdge[u] = idx;
initDFS(u, node);
sz[node] += sz[u];
}
if (node == 1)
{
sz[node]--;
}
}
int markEdges(int root, int cnt)
{
std::fill(w.begin(), w.end(), 0);
if (cnt == 0)
{
return -1;
}
while (!q.empty())
{
q.pop();
}
q.push(root);
int last = -1;
if (root != 1)
{
w[parEdge[root]] = 1;
last = parEdge[root];
cnt--;
}
while (cnt)
{
int top = q.front();
q.pop();
for (const auto &[u, idx] : g[top])
{
if (cnt == 0)
{
break;
}
if (u == par[top])
{
continue;
}
w[idx] = 1;
last = idx;
q.push(u);
cnt--;
}
}
return last;
}
std::vector <std::pair <int,int>> getKth(int node, int k)
{
assert(node != 1);
w[parEdge[node]] = 1;
std::vector <std::pair <int,int>> candidates;
qDist.push({node, 1});
while (!qDist.empty())
{
auto [top, dist] = qDist.front();
qDist.pop();
if (dist == k)
{
candidates.push_back({top, parEdge[top]});
continue;
}
for (const auto &[u, idx] : g[top])
{
if (u == par[top])
{
continue;
}
w[idx] = 1;
qDist.push({u, dist + 1});
}
}
return candidates;
}
void find_pair(int N, std::vector <int> U, std::vector <int> V, int A, int B)
{
n = N;
m = U.size();
for (int i = 0 ; i < m ; ++i)
{
U[i]++;
V[i]++;
g[U[i]].push_back({V[i], i});
g[V[i]].push_back({U[i], i});
}
initDFS(1, 0);
w.resize(m, 0);
llong dist = ask(w) / A;
int l = 0, r = n, mid, last = -1;
while (l < r - 1)
{
mid = (l + r) / 2;
int res = markEdges(1, mid);
if (ask(w) == dist * A) l = mid;
else
{
r = mid;
last = res;
}
}
assert(last != -1);
if (U[last] == par[V[last]])
{
std::swap(U[last], V[last]);
}
markEdges(U[last], sz[U[last]]);
llong res = ask(w);
llong distX = (res - dist * A) / (B - A);
llong distY = dist - distX;
std::fill(w.begin(), w.end(), 0);
std::vector <std::pair <int,int>> candidates = getKth(U[last], distX);
l = -1;
r = candidates.size() - 1;
while (l < r - 1)
{
mid = (l + r) / 2;
for (const auto &[u, idx] : candidates)
{
w[idx] = 0;
}
for (int i = 0 ; i <= mid ; ++i)
{
w[candidates[i].second] = 1;
}
res = ask(w);
if (res == distX * B + distY * A) r = mid;
else l = mid;
}
int s, t;
s = candidates[r].first;
if (distY == 0)
{
answer(s - 1, V[last] - 1);
return;
}
candidates.clear();
bool isGood = false;
std::fill(w.begin(), w.end(), 0);
for (const auto &[u, idx] : g[V[last]])
{
if (u == par[V[last]])
{
continue;
}
if (u == U[last])
{
isGood = true;
continue;
}
if (isGood)
{
std::vector <std::pair <int,int>> curr = getKth(u, distY);
for (const auto &i : curr)
{
candidates.push_back(i);
}
}
}
l = -1, r = candidates.size() - 1;
while (l < r - 1)
{
mid = (l + r) / 2;
for (const auto &[u, idx] : candidates)
{
w[idx] = 0;
}
for (int i = 0 ; i <= mid ; ++i)
{
w[candidates[i].second] = 1;
}
res = ask(w);
if (res == distX * A + distY * B) r = mid;
else l = mid;
}
t = candidates[r].first;
answer(s - 1, t - 1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2428 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
10 ms |
3128 KB |
Output is correct |
3 |
Correct |
84 ms |
8908 KB |
Output is correct |
4 |
Correct |
107 ms |
8908 KB |
Output is correct |
5 |
Correct |
78 ms |
8904 KB |
Output is correct |
6 |
Correct |
92 ms |
8864 KB |
Output is correct |
7 |
Correct |
115 ms |
8784 KB |
Output is correct |
8 |
Correct |
100 ms |
8932 KB |
Output is correct |
9 |
Correct |
69 ms |
8668 KB |
Output is correct |
10 |
Correct |
102 ms |
8904 KB |
Output is correct |
11 |
Correct |
95 ms |
9416 KB |
Output is correct |
12 |
Correct |
102 ms |
10020 KB |
Output is correct |
13 |
Correct |
71 ms |
9544 KB |
Output is correct |
14 |
Correct |
89 ms |
8984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3664 KB |
Output is correct |
2 |
Correct |
20 ms |
4936 KB |
Output is correct |
3 |
Correct |
25 ms |
6164 KB |
Output is correct |
4 |
Correct |
79 ms |
13788 KB |
Output is correct |
5 |
Correct |
99 ms |
13684 KB |
Output is correct |
6 |
Correct |
90 ms |
13772 KB |
Output is correct |
7 |
Correct |
67 ms |
13696 KB |
Output is correct |
8 |
Correct |
93 ms |
13680 KB |
Output is correct |
9 |
Correct |
86 ms |
13688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
11 ms |
3028 KB |
Output is correct |
3 |
Correct |
77 ms |
7288 KB |
Output is correct |
4 |
Correct |
129 ms |
8644 KB |
Output is correct |
5 |
Correct |
82 ms |
8644 KB |
Output is correct |
6 |
Correct |
76 ms |
8644 KB |
Output is correct |
7 |
Correct |
106 ms |
8660 KB |
Output is correct |
8 |
Correct |
130 ms |
8636 KB |
Output is correct |
9 |
Correct |
115 ms |
8836 KB |
Output is correct |
10 |
Correct |
88 ms |
8804 KB |
Output is correct |
11 |
Correct |
78 ms |
8884 KB |
Output is correct |
12 |
Correct |
75 ms |
10104 KB |
Output is correct |
13 |
Correct |
121 ms |
9652 KB |
Output is correct |
14 |
Correct |
121 ms |
10052 KB |
Output is correct |
15 |
Correct |
83 ms |
8668 KB |
Output is correct |
16 |
Correct |
102 ms |
8652 KB |
Output is correct |
17 |
Correct |
109 ms |
9792 KB |
Output is correct |
18 |
Correct |
78 ms |
9396 KB |
Output is correct |
19 |
Correct |
68 ms |
8660 KB |
Output is correct |
20 |
Correct |
96 ms |
10516 KB |
Output is correct |
21 |
Correct |
88 ms |
9792 KB |
Output is correct |
22 |
Correct |
111 ms |
9296 KB |
Output is correct |
23 |
Correct |
116 ms |
10048 KB |
Output is correct |
24 |
Correct |
106 ms |
10484 KB |
Output is correct |
25 |
Correct |
106 ms |
13068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
172 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
161 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |