#include "citymapping.h"
#include <vector>
#include <algorithm>
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
using namespace std;
vector<pii > edge[1001];
int sz[1001],p[1001];
ll d[1001][1001];
int dfs(int x) {
if (edge[x].empty()) return x;
int mx = 0;
int nxt= 0;
for (auto p : edge[x]) {
int c = p.first;
if (mx < sz[c]) {
nxt = c;
mx = sz[c];
}
}
return dfs(nxt);
}
int find_par(int root,int x,ll dist) {
if (edge[root].empty()) return root;
int leaf = dfs(root);
ll x2l = get_distance(x, leaf);
int lca = leaf;
while (d[root][leaf] + dist < (d[root][lca] << 1) + x2l)
lca = p[lca];
if (edge[lca].size() <= 1) return lca;
//2
int l = edge[lca][0].first, r = edge[lca][1].first;
if (sz[l] < sz[r]) return find_par(l, x, dist - d[root][l]);
else return find_par(r, x, dist - d[root][r]);
}
void find_roads(int N, int Q, int A[], int B[], int W[]) {
vector<pli > v;
for (int i = 2; i <= N; ++i) {
d[1][i] = d[i][1] = get_distance(1, i);
v.push_back({ d[1][i],i });
}
sort(v.begin(), v.end());
sz[1] = 1;
for (auto pr : v) {
int x = pr.second;
ll dist = pr.first;
int par = find_par(1, x, dist);
edge[par].push_back({ x,dist - d[par][1] });
p[x] = par; sz[x] = 1;
while (par) {
++sz[par];
d[par][x] = d[x][par] = d[1][x] - d[1][par];
par = p[par];
}
}
int len = 0;
for (int i = 1; i <= N; ++i) {
for (auto p : edge[i]) {
A[len] = i;
B[len] = p.first;
W[len] = p.second;
++len;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
8448 KB |
Correct: 2689 out of 500000 queries used. |
2 |
Correct |
10 ms |
8320 KB |
Correct: 2153 out of 500000 queries used. |
3 |
Correct |
6 ms |
8320 KB |
Correct: 4163 out of 500000 queries used. |
4 |
Incorrect |
6 ms |
8320 KB |
Reported list of edges differ from actual. |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
8448 KB |
Correct: 2689 out of 500000 queries used. |
2 |
Correct |
10 ms |
8320 KB |
Correct: 2153 out of 500000 queries used. |
3 |
Correct |
6 ms |
8320 KB |
Correct: 4163 out of 500000 queries used. |
4 |
Incorrect |
6 ms |
8320 KB |
Reported list of edges differ from actual. |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8320 KB |
Correct: 2084 out of 12000 queries used. |
2 |
Correct |
12 ms |
8320 KB |
Correct: 2302 out of 12000 queries used. |
3 |
Correct |
12 ms |
8448 KB |
Correct: 2455 out of 12000 queries used. |
4 |
Correct |
14 ms |
8320 KB |
Correct: 2468 out of 12000 queries used. |
5 |
Correct |
15 ms |
8320 KB |
Correct: 2239 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8320 KB |
Correct: 2084 out of 12000 queries used. |
2 |
Correct |
12 ms |
8320 KB |
Correct: 2302 out of 12000 queries used. |
3 |
Correct |
12 ms |
8448 KB |
Correct: 2455 out of 12000 queries used. |
4 |
Correct |
14 ms |
8320 KB |
Correct: 2468 out of 12000 queries used. |
5 |
Correct |
15 ms |
8320 KB |
Correct: 2239 out of 12000 queries used. |
6 |
Correct |
13 ms |
8448 KB |
Correct: 2472 out of 12000 queries used. |
7 |
Correct |
13 ms |
8448 KB |
Correct: 2381 out of 12000 queries used. |
8 |
Correct |
13 ms |
8448 KB |
Correct: 2206 out of 12000 queries used. |
9 |
Correct |
13 ms |
8448 KB |
Correct: 2234 out of 12000 queries used. |
10 |
Correct |
12 ms |
8320 KB |
Correct: 2301 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
8448 KB |
Correct: 2689 out of 500000 queries used. |
2 |
Correct |
10 ms |
8320 KB |
Correct: 2153 out of 500000 queries used. |
3 |
Correct |
6 ms |
8320 KB |
Correct: 4163 out of 500000 queries used. |
4 |
Incorrect |
6 ms |
8320 KB |
Reported list of edges differ from actual. |
5 |
Halted |
0 ms |
0 KB |
- |