# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
668379 | 2022-12-03T18:02:29 Z | 600Mihnea | City Mapping (NOI18_citymapping) | C++17 | 19 ms | 600 KB |
#include "citymapping.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; mt19937 rng((long long) (new char)); ll ask(int i, int j) { return get_distance(i + 1, j + 1); } void find_roads(int n, int q, int e_a[], int e_b[], int e_w[]) { int root = rng() % n; vector<ll> dist0(n, 0); vector<int> ord; for (int i = 0; i < n; i++) { if (root == i) { dist0[i] = 0; continue; } dist0[i] = ask(root, i); ord.push_back(i); } sort(ord.begin(), ord.end(), [&] (int i, int j) {return dist0[i] < dist0[j];}); vector<int> deja = {0}; vector<vector<int>> g(n); vector<int> sub(n), big(n), u(n), par(n); int top = 0; auto add_edge = [&] (int a, int b) { /// cout << " add_edge : " << a << " " << b << "\n"; e_a[top] = a + 1; e_b[top] = b + 1; e_w[top] = dist0[b] - dist0[a]; g[a].push_back(b); top++; }; function<void(int)> dfs = [&] (int a) { sub[a] = 1; if (g[a].empty()) { u[a] = a; return; } assert(!g[a].empty()); big[a] = g[a][0]; for (auto &b : g[a]) { dfs(b); par[b] = a; sub[a] += sub[b]; if (sub[b] > sub[big[a]]) { big[a] = b; } } u[a] = u[big[a]]; }; function<void(int, int)> locate = [&] (int root, int new_vertex) { if (sub[root] == 1) { add_edge(root, new_vertex); return; } assert(!g[root].empty()); int b = u[root]; assert(b != root); ll dist_b_v = ask(b, new_vertex); ll dist_b_r = dist0[b] - dist0[root]; ll dist_v_r = dist0[new_vertex] - dist0[root]; /** x + y = dist_b_r z + y = dist_b_v x + z = dist_v_r z = sum - dist_b_r x = sum - dist_b_v y - sum - dist_v_r **/ ll sum_dub = (dist_b_v + dist_b_r + dist_v_r); assert(sum_dub % 2 == 0); ll sum = sum_dub / 2; ll z = sum - dist_b_r; ll x = sum - dist_b_v; ll y = sum - dist_v_r; assert(x + y + z == sum); assert(0 <= x); assert(0 <= y); assert(0 <= z); assert(x + y == dist_b_r); assert(z + y == dist_b_v); assert(x + z == dist_v_r); int fix_inainte = -1; /// scapa de astea!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! /// assert(x + y == ask(root, b)); /// assert(x + z == ask(root, new_vertex)); /// assert(z + y == ask(b, new_vertex)); /// scapa de astea!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if (y == 0) { add_edge(b, new_vertex); return; } // cout << "x = " << x << "\n"; // cout << "y = " << y << "\n"; // cout << "z = " << z << "\n"; while (y > 0) { assert(b != root); y -= (dist0[b] - dist0[par[b]]); fix_inainte = b; b = par[b]; } assert(y == 0); int mid_point = b; if (z == 0) { add_edge(mid_point, new_vertex); return; } assert(z > 0); ///cout << " = " << fix_inainte << " " << b << " " << root << "\n"; assert(0 <= fix_inainte && fix_inainte < n); assert(par[fix_inainte] == b); if ((int) g[mid_point].size() == 1) { add_edge(mid_point, new_vertex); return; } assert((int) g[mid_point].size() == 2 || (int) g[mid_point].size() == 3); vector<int> oth; for (auto &vecin : g[mid_point]) { if (vecin == fix_inainte) { continue; } oth.push_back(vecin); } assert((int) oth.size() == 1 || (int) oth.size() == 2); assert((int) oth.size() == (int) g[mid_point].size() - 1); if ((int) oth.size() == 1) { if (dist0[oth[0]] - dist0[mid_point] >= z) { add_edge(mid_point, new_vertex); return; } if (ask(oth[0], new_vertex) < z) { locate(oth[0], new_vertex); return; } add_edge(mid_point, new_vertex); return; } assert((int) oth.size() == 2); if (ask(oth[0], new_vertex) < ask(oth[1], new_vertex)) { locate(oth[0], new_vertex); } else { locate(oth[1], new_vertex); } }; for (auto &vertex : ord) { dfs(root); locate(root, vertex); } }
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 468 KB | Correct: 3055 out of 500000 queries used. |
2 | Correct | 17 ms | 468 KB | Correct: 2697 out of 500000 queries used. |
3 | Correct | 16 ms | 500 KB | Correct: 6833 out of 500000 queries used. |
4 | Correct | 16 ms | 468 KB | Correct: 8779 out of 500000 queries used. |
5 | Correct | 18 ms | 512 KB | Correct: 4567 out of 500000 queries used. |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 468 KB | Correct: 3055 out of 500000 queries used. |
2 | Correct | 17 ms | 468 KB | Correct: 2697 out of 500000 queries used. |
3 | Correct | 16 ms | 500 KB | Correct: 6833 out of 500000 queries used. |
4 | Correct | 16 ms | 468 KB | Correct: 8779 out of 500000 queries used. |
5 | Correct | 18 ms | 512 KB | Correct: 4567 out of 500000 queries used. |
6 | Correct | 16 ms | 572 KB | Correct: 2510 out of 500000 queries used. |
7 | Correct | 17 ms | 468 KB | Correct: 2763 out of 500000 queries used. |
8 | Correct | 15 ms | 480 KB | Correct: 6883 out of 500000 queries used. |
9 | Correct | 15 ms | 508 KB | Correct: 7890 out of 500000 queries used. |
10 | Correct | 17 ms | 468 KB | Correct: 4309 out of 500000 queries used. |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 16 ms | 468 KB | Correct: 2359 out of 12000 queries used. |
2 | Correct | 16 ms | 596 KB | Correct: 2452 out of 12000 queries used. |
3 | Correct | 17 ms | 468 KB | Correct: 3172 out of 12000 queries used. |
4 | Correct | 16 ms | 596 KB | Correct: 2106 out of 12000 queries used. |
5 | Correct | 17 ms | 576 KB | Correct: 2370 out of 12000 queries used. |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 16 ms | 468 KB | Correct: 2359 out of 12000 queries used. |
2 | Correct | 16 ms | 596 KB | Correct: 2452 out of 12000 queries used. |
3 | Correct | 17 ms | 468 KB | Correct: 3172 out of 12000 queries used. |
4 | Correct | 16 ms | 596 KB | Correct: 2106 out of 12000 queries used. |
5 | Correct | 17 ms | 576 KB | Correct: 2370 out of 12000 queries used. |
6 | Correct | 17 ms | 524 KB | Correct: 2700 out of 12000 queries used. |
7 | Correct | 15 ms | 540 KB | Correct: 2462 out of 12000 queries used. |
8 | Correct | 16 ms | 560 KB | Correct: 2783 out of 12000 queries used. |
9 | Correct | 15 ms | 468 KB | Correct: 2488 out of 12000 queries used. |
10 | Correct | 16 ms | 600 KB | Correct: 1990 out of 12000 queries used. |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 468 KB | Correct: 3055 out of 500000 queries used. |
2 | Correct | 17 ms | 468 KB | Correct: 2697 out of 500000 queries used. |
3 | Correct | 16 ms | 500 KB | Correct: 6833 out of 500000 queries used. |
4 | Correct | 16 ms | 468 KB | Correct: 8779 out of 500000 queries used. |
5 | Correct | 18 ms | 512 KB | Correct: 4567 out of 500000 queries used. |
6 | Correct | 16 ms | 572 KB | Correct: 2510 out of 500000 queries used. |
7 | Correct | 17 ms | 468 KB | Correct: 2763 out of 500000 queries used. |
8 | Correct | 15 ms | 480 KB | Correct: 6883 out of 500000 queries used. |
9 | Correct | 15 ms | 508 KB | Correct: 7890 out of 500000 queries used. |
10 | Correct | 17 ms | 468 KB | Correct: 4309 out of 500000 queries used. |
11 | Correct | 16 ms | 468 KB | Correct: 2359 out of 12000 queries used. |
12 | Correct | 16 ms | 596 KB | Correct: 2452 out of 12000 queries used. |
13 | Correct | 17 ms | 468 KB | Correct: 3172 out of 12000 queries used. |
14 | Correct | 16 ms | 596 KB | Correct: 2106 out of 12000 queries used. |
15 | Correct | 17 ms | 576 KB | Correct: 2370 out of 12000 queries used. |
16 | Correct | 17 ms | 524 KB | Correct: 2700 out of 12000 queries used. |
17 | Correct | 15 ms | 540 KB | Correct: 2462 out of 12000 queries used. |
18 | Correct | 16 ms | 560 KB | Correct: 2783 out of 12000 queries used. |
19 | Correct | 15 ms | 468 KB | Correct: 2488 out of 12000 queries used. |
20 | Correct | 16 ms | 600 KB | Correct: 1990 out of 12000 queries used. |
21 | Correct | 15 ms | 536 KB | Correct: 2230 out of 25000 queries used. |
22 | Correct | 16 ms | 468 KB | Correct: 2590 out of 25000 queries used. |
23 | Correct | 16 ms | 548 KB | Correct: 2338 out of 25000 queries used. |
24 | Correct | 16 ms | 468 KB | Correct: 2348 out of 25000 queries used. |
25 | Correct | 17 ms | 468 KB | Correct: 6582 out of 25000 queries used. |
26 | Correct | 16 ms | 468 KB | Correct: 6457 out of 25000 queries used. |
27 | Correct | 16 ms | 504 KB | Correct: 6325 out of 25000 queries used. |
28 | Correct | 17 ms | 468 KB | Correct: 6394 out of 25000 queries used. |
29 | Correct | 15 ms | 504 KB | Correct: 6622 out of 25000 queries used. |
30 | Correct | 17 ms | 468 KB | Correct: 6262 out of 25000 queries used. |
31 | Correct | 17 ms | 512 KB | Correct: 7987 out of 25000 queries used. |
32 | Correct | 17 ms | 468 KB | Correct: 2690 out of 25000 queries used. |
33 | Correct | 16 ms | 508 KB | Correct: 8122 out of 25000 queries used. |
34 | Correct | 16 ms | 508 KB | Correct: 8015 out of 25000 queries used. |
35 | Correct | 16 ms | 504 KB | Correct: 7881 out of 25000 queries used. |
36 | Correct | 16 ms | 468 KB | Correct: 7915 out of 25000 queries used. |
37 | Correct | 16 ms | 468 KB | Correct: 8035 out of 25000 queries used. |
38 | Correct | 15 ms | 468 KB | Correct: 7971 out of 25000 queries used. |
39 | Correct | 15 ms | 468 KB | Correct: 7931 out of 25000 queries used. |
40 | Correct | 16 ms | 488 KB | Correct: 8036 out of 25000 queries used. |
41 | Correct | 17 ms | 508 KB | Correct: 8024 out of 25000 queries used. |
42 | Correct | 15 ms | 468 KB | Correct: 8009 out of 25000 queries used. |
43 | Correct | 16 ms | 468 KB | Correct: 2615 out of 25000 queries used. |
44 | Correct | 15 ms | 508 KB | Correct: 7862 out of 25000 queries used. |
45 | Correct | 15 ms | 508 KB | Correct: 8017 out of 25000 queries used. |
46 | Correct | 15 ms | 504 KB | Correct: 7972 out of 25000 queries used. |
47 | Correct | 16 ms | 512 KB | Correct: 8047 out of 25000 queries used. |
48 | Correct | 16 ms | 456 KB | Correct: 7949 out of 25000 queries used. |
49 | Correct | 17 ms | 504 KB | Correct: 7904 out of 25000 queries used. |
50 | Correct | 17 ms | 500 KB | Correct: 7926 out of 25000 queries used. |
51 | Correct | 16 ms | 512 KB | Correct: 8016 out of 25000 queries used. |
52 | Correct | 17 ms | 508 KB | Correct: 7968 out of 25000 queries used. |
53 | Correct | 16 ms | 504 KB | Correct: 7934 out of 25000 queries used. |
54 | Correct | 17 ms | 468 KB | Correct: 2238 out of 25000 queries used. |
55 | Correct | 17 ms | 596 KB | Correct: 7883 out of 25000 queries used. |
56 | Correct | 16 ms | 504 KB | Correct: 7914 out of 25000 queries used. |
57 | Correct | 17 ms | 508 KB | Correct: 7960 out of 25000 queries used. |
58 | Correct | 16 ms | 468 KB | Correct: 7928 out of 25000 queries used. |
59 | Correct | 16 ms | 508 KB | Correct: 7934 out of 25000 queries used. |
60 | Correct | 18 ms | 520 KB | Correct: 4618 out of 25000 queries used. |
61 | Correct | 19 ms | 468 KB | Correct: 3783 out of 25000 queries used. |
62 | Correct | 18 ms | 468 KB | Correct: 4119 out of 25000 queries used. |
63 | Correct | 18 ms | 524 KB | Correct: 4407 out of 25000 queries used. |
64 | Correct | 19 ms | 520 KB | Correct: 4248 out of 25000 queries used. |
65 | Correct | 17 ms | 520 KB | Correct: 3065 out of 25000 queries used. |
66 | Correct | 18 ms | 512 KB | Correct: 3852 out of 25000 queries used. |
67 | Correct | 17 ms | 540 KB | Correct: 2668 out of 25000 queries used. |
68 | Correct | 17 ms | 596 KB | Correct: 2782 out of 25000 queries used. |
69 | Correct | 17 ms | 544 KB | Correct: 2008 out of 25000 queries used. |
70 | Correct | 18 ms | 536 KB | Correct: 2518 out of 25000 queries used. |