#include "dreaming.h"
#include <vector>
#include <unordered_map>
#include <deque>
#include <iostream>
using namespace std;
const int maxn = 1e5;
const int inf = 2e9;
bool used[maxn];
unordered_map<int, int> dst(int v, vector<vector<pair<int, int>>> &g) {
unordered_map<int, int> d;
deque<int> q;
q.push_back(v);
d[v] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (auto [u, w] : g[v]) {
if (!d.count(u)) {
d[u] = d[v] + w;
q.push_back(u);
}
}
}
return d;
}
pair<int, int> goFar(int v, vector<vector<pair<int, int>>> &g) {
auto d1 = dst(v, g);
int best = v;
for (auto [u, x] : d1) {
if (d1[best] < x) {
best = u;
}
used[u] = true;
}
return { best, d1[best] };
}
pair<int, int> findDiam(int v, vector<vector<pair<int, int>>> &g) {
int leafe = goFar(v, g).first;
int best = goFar(leafe, g).first;
int best1 = goFar(best, g).first;
return { best, best1 };
}
int findDiamLen(int v, vector<vector<pair<int, int>>> &g) {
int leafe = goFar(v, g).first;
int best = goFar(leafe, g).first;
return goFar(best, g).second;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vector<vector<pair<int, int>>> g(N);
for (int i = 0; i < M; ++i) {
int u = A[i], v = B[i];
g[u].push_back({ v, T[i] });
g[v].push_back({ u, T[i] });
}
vector<pair<int, int>> dims;
for (int i = 0; i < N; ++i) {
if (!used[i]) {
dims.push_back(findDiam(i, g));
}
}
pair<int, int> curr = dims[0];
/*cout << "OUT" << endl;
for (auto [x, y] : dims) {
cout << x << ' ' << y << endl;
}*/
for (int i = 1; i < dims.size(); ++i) {
auto d1 = dst(curr.first, g);
auto d2 = dst(curr.second, g);
auto d3 = dst(dims[i].first, g);
auto d4 = dst(dims[i].second, g);
pair<int, int> cand;
int b1 = inf;
for (auto [u, l] : d1) {
if (b1 > max(l, d2[u])) {
b1 = max(l, d2[u]);
cand.first = u;
}
}
b1 = inf;
for (auto [u, l] : d3) {
if (b1 > max(l, d4[u])) {
b1 = max(l, d4[u]);
cand.second = u;
}
}
//cout << "UNITE " << cand.first << ' ' << cand.second << endl;
g[cand.first].push_back({ cand.second, L });
g[cand.second].push_back({ cand.first, L });
curr = findDiam(cand.first, g);
}
return findDiamLen(0, g);
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 1; i < dims.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
19676 KB |
Output is correct |
2 |
Correct |
235 ms |
19516 KB |
Output is correct |
3 |
Correct |
145 ms |
11956 KB |
Output is correct |
4 |
Correct |
26 ms |
2864 KB |
Output is correct |
5 |
Correct |
19 ms |
2140 KB |
Output is correct |
6 |
Correct |
43 ms |
3976 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
99 ms |
8088 KB |
Output is correct |
9 |
Correct |
200 ms |
10008 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
201 ms |
14280 KB |
Output is correct |
12 |
Correct |
237 ms |
15640 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
19676 KB |
Output is correct |
2 |
Correct |
235 ms |
19516 KB |
Output is correct |
3 |
Correct |
145 ms |
11956 KB |
Output is correct |
4 |
Correct |
26 ms |
2864 KB |
Output is correct |
5 |
Correct |
19 ms |
2140 KB |
Output is correct |
6 |
Correct |
43 ms |
3976 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
99 ms |
8088 KB |
Output is correct |
9 |
Correct |
200 ms |
10008 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
201 ms |
14280 KB |
Output is correct |
12 |
Correct |
237 ms |
15640 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
243 ms |
19584 KB |
Output is correct |
40 |
Correct |
204 ms |
19568 KB |
Output is correct |
41 |
Correct |
166 ms |
11844 KB |
Output is correct |
42 |
Correct |
24 ms |
2864 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
296 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
386 ms |
21336 KB |
Output is correct |
58 |
Correct |
410 ms |
22420 KB |
Output is correct |
59 |
Correct |
331 ms |
22744 KB |
Output is correct |
60 |
Correct |
372 ms |
22428 KB |
Output is correct |
61 |
Correct |
335 ms |
23108 KB |
Output is correct |
62 |
Correct |
313 ms |
22032 KB |
Output is correct |
63 |
Correct |
325 ms |
22640 KB |
Output is correct |
64 |
Correct |
421 ms |
22652 KB |
Output is correct |
65 |
Correct |
426 ms |
22972 KB |
Output is correct |
66 |
Correct |
458 ms |
22896 KB |
Output is correct |
67 |
Correct |
335 ms |
22804 KB |
Output is correct |
68 |
Correct |
368 ms |
22836 KB |
Output is correct |
69 |
Correct |
353 ms |
22036 KB |
Output is correct |
70 |
Correct |
414 ms |
22748 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
10 ms |
852 KB |
Output is correct |
73 |
Correct |
7 ms |
980 KB |
Output is correct |
74 |
Correct |
10 ms |
984 KB |
Output is correct |
75 |
Correct |
6 ms |
852 KB |
Output is correct |
76 |
Correct |
6 ms |
964 KB |
Output is correct |
77 |
Correct |
6 ms |
980 KB |
Output is correct |
78 |
Correct |
7 ms |
960 KB |
Output is correct |
79 |
Correct |
8 ms |
852 KB |
Output is correct |
80 |
Correct |
378 ms |
21792 KB |
Output is correct |
81 |
Correct |
342 ms |
21756 KB |
Output is correct |
82 |
Correct |
366 ms |
23116 KB |
Output is correct |
83 |
Correct |
325 ms |
22808 KB |
Output is correct |
84 |
Correct |
4 ms |
852 KB |
Output is correct |
85 |
Correct |
4 ms |
704 KB |
Output is correct |
86 |
Correct |
5 ms |
852 KB |
Output is correct |
87 |
Correct |
5 ms |
836 KB |
Output is correct |
88 |
Correct |
6 ms |
852 KB |
Output is correct |
89 |
Correct |
8 ms |
860 KB |
Output is correct |
90 |
Correct |
7 ms |
852 KB |
Output is correct |
91 |
Correct |
6 ms |
980 KB |
Output is correct |
92 |
Correct |
7 ms |
852 KB |
Output is correct |
93 |
Correct |
6 ms |
836 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
1 ms |
308 KB |
Output is correct |
96 |
Correct |
1 ms |
308 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
1 ms |
340 KB |
Output is correct |
99 |
Correct |
1 ms |
340 KB |
Output is correct |
100 |
Correct |
1 ms |
340 KB |
Output is correct |
101 |
Correct |
1 ms |
340 KB |
Output is correct |
102 |
Correct |
1 ms |
340 KB |
Output is correct |
103 |
Correct |
1 ms |
340 KB |
Output is correct |
104 |
Correct |
41 ms |
2260 KB |
Output is correct |
105 |
Correct |
55 ms |
4296 KB |
Output is correct |
106 |
Correct |
1 ms |
308 KB |
Output is correct |
107 |
Correct |
107 ms |
8616 KB |
Output is correct |
108 |
Correct |
173 ms |
10612 KB |
Output is correct |
109 |
Correct |
2 ms |
360 KB |
Output is correct |
110 |
Correct |
182 ms |
15412 KB |
Output is correct |
111 |
Correct |
200 ms |
16608 KB |
Output is correct |
112 |
Correct |
2 ms |
448 KB |
Output is correct |
113 |
Correct |
376 ms |
22432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1052 ms |
5732 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
75 ms |
512 KB |
Output is correct |
27 |
Correct |
242 ms |
704 KB |
Output is correct |
28 |
Correct |
603 ms |
964 KB |
Output is correct |
29 |
Correct |
67 ms |
584 KB |
Output is correct |
30 |
Correct |
295 ms |
748 KB |
Output is correct |
31 |
Correct |
536 ms |
1016 KB |
Output is correct |
32 |
Correct |
48 ms |
520 KB |
Output is correct |
33 |
Incorrect |
242 ms |
716 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
19676 KB |
Output is correct |
2 |
Correct |
235 ms |
19516 KB |
Output is correct |
3 |
Correct |
145 ms |
11956 KB |
Output is correct |
4 |
Correct |
26 ms |
2864 KB |
Output is correct |
5 |
Correct |
19 ms |
2140 KB |
Output is correct |
6 |
Correct |
43 ms |
3976 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
99 ms |
8088 KB |
Output is correct |
9 |
Correct |
200 ms |
10008 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
201 ms |
14280 KB |
Output is correct |
12 |
Correct |
237 ms |
15640 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
243 ms |
19584 KB |
Output is correct |
40 |
Correct |
204 ms |
19568 KB |
Output is correct |
41 |
Correct |
166 ms |
11844 KB |
Output is correct |
42 |
Correct |
24 ms |
2864 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
296 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
386 ms |
21336 KB |
Output is correct |
58 |
Correct |
410 ms |
22420 KB |
Output is correct |
59 |
Correct |
331 ms |
22744 KB |
Output is correct |
60 |
Correct |
372 ms |
22428 KB |
Output is correct |
61 |
Correct |
335 ms |
23108 KB |
Output is correct |
62 |
Correct |
313 ms |
22032 KB |
Output is correct |
63 |
Correct |
325 ms |
22640 KB |
Output is correct |
64 |
Correct |
421 ms |
22652 KB |
Output is correct |
65 |
Correct |
426 ms |
22972 KB |
Output is correct |
66 |
Correct |
458 ms |
22896 KB |
Output is correct |
67 |
Correct |
335 ms |
22804 KB |
Output is correct |
68 |
Correct |
368 ms |
22836 KB |
Output is correct |
69 |
Correct |
353 ms |
22036 KB |
Output is correct |
70 |
Correct |
414 ms |
22748 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
10 ms |
852 KB |
Output is correct |
73 |
Correct |
7 ms |
980 KB |
Output is correct |
74 |
Correct |
10 ms |
984 KB |
Output is correct |
75 |
Correct |
6 ms |
852 KB |
Output is correct |
76 |
Correct |
6 ms |
964 KB |
Output is correct |
77 |
Correct |
6 ms |
980 KB |
Output is correct |
78 |
Correct |
7 ms |
960 KB |
Output is correct |
79 |
Correct |
8 ms |
852 KB |
Output is correct |
80 |
Correct |
378 ms |
21792 KB |
Output is correct |
81 |
Correct |
342 ms |
21756 KB |
Output is correct |
82 |
Correct |
366 ms |
23116 KB |
Output is correct |
83 |
Correct |
325 ms |
22808 KB |
Output is correct |
84 |
Correct |
4 ms |
852 KB |
Output is correct |
85 |
Correct |
4 ms |
704 KB |
Output is correct |
86 |
Correct |
5 ms |
852 KB |
Output is correct |
87 |
Correct |
5 ms |
836 KB |
Output is correct |
88 |
Correct |
6 ms |
852 KB |
Output is correct |
89 |
Correct |
8 ms |
860 KB |
Output is correct |
90 |
Correct |
7 ms |
852 KB |
Output is correct |
91 |
Correct |
6 ms |
980 KB |
Output is correct |
92 |
Correct |
7 ms |
852 KB |
Output is correct |
93 |
Correct |
6 ms |
836 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
1 ms |
308 KB |
Output is correct |
96 |
Correct |
1 ms |
308 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
1 ms |
340 KB |
Output is correct |
99 |
Correct |
1 ms |
340 KB |
Output is correct |
100 |
Correct |
1 ms |
340 KB |
Output is correct |
101 |
Correct |
1 ms |
340 KB |
Output is correct |
102 |
Correct |
1 ms |
340 KB |
Output is correct |
103 |
Correct |
1 ms |
340 KB |
Output is correct |
104 |
Correct |
41 ms |
2260 KB |
Output is correct |
105 |
Correct |
55 ms |
4296 KB |
Output is correct |
106 |
Correct |
1 ms |
308 KB |
Output is correct |
107 |
Correct |
107 ms |
8616 KB |
Output is correct |
108 |
Correct |
173 ms |
10612 KB |
Output is correct |
109 |
Correct |
2 ms |
360 KB |
Output is correct |
110 |
Correct |
182 ms |
15412 KB |
Output is correct |
111 |
Correct |
200 ms |
16608 KB |
Output is correct |
112 |
Correct |
2 ms |
448 KB |
Output is correct |
113 |
Correct |
376 ms |
22432 KB |
Output is correct |
114 |
Execution timed out |
1052 ms |
5732 KB |
Time limit exceeded |
115 |
Halted |
0 ms |
0 KB |
- |