#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, w;
vector<vector<int> > adj;
map<pair<int,int>,int> myMap;
vector<bool> hasVisited;
vector<int> parent;
pair<int,int> find_farthest_node (int x, bool undo) {
queue<pair<int,int> > q;
q.push(make_pair(x, 0));
hasVisited[x] = true;
parent[x] = -1;
vector<int> nodes;
vector<pair<int,int> > vec;
while (!q.empty()) {
pair<int,int> p = q.front();
nodes.push_back(p.first);
vec.push_back(make_pair(p.second, p.first));
q.pop();
for (int j: adj[p.first]) {
if (!hasVisited[j]) {
q.push(make_pair(j, p.second + myMap[make_pair(j, p.first)]));
parent[j] = p.first;
hasVisited[j] = true;
}
}
}
if (undo) {
for (int j: nodes) {
hasVisited[j] = false;
}
}
sort(vec.begin(), vec.end());
return vec.back(); //first distance, then node
}
int travelTime (int N, int M, int L, int A[], int B[], int W[]) {
a.resize(M), b.resize(M), w.resize(M), adj.resize(N), parent.resize(N);
for (int i = 0; i < M; i++) {
a[i] = A[i], b[i] = B[i], w[i] = W[i];
myMap[make_pair(a[i], b[i])] = w[i];
myMap[make_pair(b[i], a[i])] = w[i];
adj[a[i]].push_back(b[i]), adj[b[i]].push_back(a[i]);
}
hasVisited.assign(N, false);
vector<int> paths;
int ans = 0;
for (int i = 0; i < N; i++) {
if (!hasVisited[i]) {
int x = find_farthest_node(i, true).second;
pair<int,int> py = find_farthest_node(x, false);
vector<int> nodes;
for (int val = py.second; val != -1; val = parent[val]) {
nodes.push_back(val);
}
ans = max(ans, py.first);
int c = 0;
int myMin = INT_MAX;
for (int j = 1; j < (int)nodes.size(); j++) {
c += myMap[make_pair(nodes[j], nodes[j - 1])];
myMin = min(myMin,max(c, py.first - c));
}
if (nodes.size() == 1) {
myMin = 0;
}
paths.push_back(myMin);
}
}
sort(paths.begin(), paths.end());
if (paths.size() >= 2) {
ans = max(paths.back() + paths[paths.size() - 2] + L, ans);
}
if (paths.size() >= 3) {
ans = max(paths.back() + paths[paths.size() - 2] + 2 * L, ans);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
21324 KB |
Output is correct |
2 |
Correct |
310 ms |
21252 KB |
Output is correct |
3 |
Correct |
158 ms |
13960 KB |
Output is correct |
4 |
Correct |
21 ms |
3384 KB |
Output is correct |
5 |
Correct |
17 ms |
2524 KB |
Output is correct |
6 |
Correct |
36 ms |
5076 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
109 ms |
9092 KB |
Output is correct |
9 |
Correct |
173 ms |
11724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
216 ms |
15940 KB |
Output is correct |
12 |
Correct |
284 ms |
18848 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
21324 KB |
Output is correct |
2 |
Correct |
310 ms |
21252 KB |
Output is correct |
3 |
Correct |
158 ms |
13960 KB |
Output is correct |
4 |
Correct |
21 ms |
3384 KB |
Output is correct |
5 |
Correct |
17 ms |
2524 KB |
Output is correct |
6 |
Correct |
36 ms |
5076 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
109 ms |
9092 KB |
Output is correct |
9 |
Correct |
173 ms |
11724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
216 ms |
15940 KB |
Output is correct |
12 |
Correct |
284 ms |
18848 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 |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 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 |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
278 ms |
21216 KB |
Output is correct |
40 |
Correct |
315 ms |
21256 KB |
Output is correct |
41 |
Correct |
145 ms |
13904 KB |
Output is correct |
42 |
Correct |
21 ms |
3412 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
319 ms |
23508 KB |
Output is correct |
58 |
Correct |
385 ms |
22256 KB |
Output is correct |
59 |
Correct |
342 ms |
23128 KB |
Output is correct |
60 |
Correct |
380 ms |
22352 KB |
Output is correct |
61 |
Correct |
338 ms |
23212 KB |
Output is correct |
62 |
Correct |
393 ms |
22408 KB |
Output is correct |
63 |
Correct |
365 ms |
23568 KB |
Output is correct |
64 |
Correct |
391 ms |
23884 KB |
Output is correct |
65 |
Correct |
397 ms |
23316 KB |
Output is correct |
66 |
Correct |
401 ms |
23692 KB |
Output is correct |
67 |
Correct |
358 ms |
23148 KB |
Output is correct |
68 |
Correct |
370 ms |
23116 KB |
Output is correct |
69 |
Correct |
372 ms |
22516 KB |
Output is correct |
70 |
Correct |
378 ms |
22260 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
4 ms |
852 KB |
Output is correct |
73 |
Correct |
5 ms |
980 KB |
Output is correct |
74 |
Correct |
5 ms |
980 KB |
Output is correct |
75 |
Correct |
6 ms |
980 KB |
Output is correct |
76 |
Correct |
5 ms |
980 KB |
Output is correct |
77 |
Correct |
5 ms |
980 KB |
Output is correct |
78 |
Correct |
6 ms |
852 KB |
Output is correct |
79 |
Correct |
4 ms |
980 KB |
Output is correct |
80 |
Correct |
339 ms |
22952 KB |
Output is correct |
81 |
Correct |
409 ms |
22908 KB |
Output is correct |
82 |
Correct |
338 ms |
23464 KB |
Output is correct |
83 |
Correct |
323 ms |
23572 KB |
Output is correct |
84 |
Correct |
4 ms |
852 KB |
Output is correct |
85 |
Correct |
3 ms |
724 KB |
Output is correct |
86 |
Correct |
3 ms |
852 KB |
Output is correct |
87 |
Correct |
4 ms |
852 KB |
Output is correct |
88 |
Correct |
4 ms |
896 KB |
Output is correct |
89 |
Correct |
4 ms |
852 KB |
Output is correct |
90 |
Correct |
4 ms |
852 KB |
Output is correct |
91 |
Correct |
4 ms |
1016 KB |
Output is correct |
92 |
Correct |
6 ms |
852 KB |
Output is correct |
93 |
Correct |
4 ms |
852 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
0 ms |
212 KB |
Output is correct |
99 |
Correct |
0 ms |
212 KB |
Output is correct |
100 |
Correct |
0 ms |
340 KB |
Output is correct |
101 |
Correct |
0 ms |
212 KB |
Output is correct |
102 |
Correct |
1 ms |
340 KB |
Output is correct |
103 |
Correct |
0 ms |
212 KB |
Output is correct |
104 |
Correct |
16 ms |
2524 KB |
Output is correct |
105 |
Correct |
41 ms |
4940 KB |
Output is correct |
106 |
Correct |
1 ms |
340 KB |
Output is correct |
107 |
Correct |
113 ms |
9128 KB |
Output is correct |
108 |
Correct |
177 ms |
11748 KB |
Output is correct |
109 |
Correct |
1 ms |
340 KB |
Output is correct |
110 |
Correct |
214 ms |
16020 KB |
Output is correct |
111 |
Correct |
267 ms |
18712 KB |
Output is correct |
112 |
Correct |
1 ms |
468 KB |
Output is correct |
113 |
Correct |
448 ms |
23216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
9804 KB |
Output isn't correct |
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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
852 KB |
Output is correct |
29 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
21324 KB |
Output is correct |
2 |
Correct |
310 ms |
21252 KB |
Output is correct |
3 |
Correct |
158 ms |
13960 KB |
Output is correct |
4 |
Correct |
21 ms |
3384 KB |
Output is correct |
5 |
Correct |
17 ms |
2524 KB |
Output is correct |
6 |
Correct |
36 ms |
5076 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
109 ms |
9092 KB |
Output is correct |
9 |
Correct |
173 ms |
11724 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
216 ms |
15940 KB |
Output is correct |
12 |
Correct |
284 ms |
18848 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 |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 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 |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
278 ms |
21216 KB |
Output is correct |
40 |
Correct |
315 ms |
21256 KB |
Output is correct |
41 |
Correct |
145 ms |
13904 KB |
Output is correct |
42 |
Correct |
21 ms |
3412 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
319 ms |
23508 KB |
Output is correct |
58 |
Correct |
385 ms |
22256 KB |
Output is correct |
59 |
Correct |
342 ms |
23128 KB |
Output is correct |
60 |
Correct |
380 ms |
22352 KB |
Output is correct |
61 |
Correct |
338 ms |
23212 KB |
Output is correct |
62 |
Correct |
393 ms |
22408 KB |
Output is correct |
63 |
Correct |
365 ms |
23568 KB |
Output is correct |
64 |
Correct |
391 ms |
23884 KB |
Output is correct |
65 |
Correct |
397 ms |
23316 KB |
Output is correct |
66 |
Correct |
401 ms |
23692 KB |
Output is correct |
67 |
Correct |
358 ms |
23148 KB |
Output is correct |
68 |
Correct |
370 ms |
23116 KB |
Output is correct |
69 |
Correct |
372 ms |
22516 KB |
Output is correct |
70 |
Correct |
378 ms |
22260 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
4 ms |
852 KB |
Output is correct |
73 |
Correct |
5 ms |
980 KB |
Output is correct |
74 |
Correct |
5 ms |
980 KB |
Output is correct |
75 |
Correct |
6 ms |
980 KB |
Output is correct |
76 |
Correct |
5 ms |
980 KB |
Output is correct |
77 |
Correct |
5 ms |
980 KB |
Output is correct |
78 |
Correct |
6 ms |
852 KB |
Output is correct |
79 |
Correct |
4 ms |
980 KB |
Output is correct |
80 |
Correct |
339 ms |
22952 KB |
Output is correct |
81 |
Correct |
409 ms |
22908 KB |
Output is correct |
82 |
Correct |
338 ms |
23464 KB |
Output is correct |
83 |
Correct |
323 ms |
23572 KB |
Output is correct |
84 |
Correct |
4 ms |
852 KB |
Output is correct |
85 |
Correct |
3 ms |
724 KB |
Output is correct |
86 |
Correct |
3 ms |
852 KB |
Output is correct |
87 |
Correct |
4 ms |
852 KB |
Output is correct |
88 |
Correct |
4 ms |
896 KB |
Output is correct |
89 |
Correct |
4 ms |
852 KB |
Output is correct |
90 |
Correct |
4 ms |
852 KB |
Output is correct |
91 |
Correct |
4 ms |
1016 KB |
Output is correct |
92 |
Correct |
6 ms |
852 KB |
Output is correct |
93 |
Correct |
4 ms |
852 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
0 ms |
212 KB |
Output is correct |
99 |
Correct |
0 ms |
212 KB |
Output is correct |
100 |
Correct |
0 ms |
340 KB |
Output is correct |
101 |
Correct |
0 ms |
212 KB |
Output is correct |
102 |
Correct |
1 ms |
340 KB |
Output is correct |
103 |
Correct |
0 ms |
212 KB |
Output is correct |
104 |
Correct |
16 ms |
2524 KB |
Output is correct |
105 |
Correct |
41 ms |
4940 KB |
Output is correct |
106 |
Correct |
1 ms |
340 KB |
Output is correct |
107 |
Correct |
113 ms |
9128 KB |
Output is correct |
108 |
Correct |
177 ms |
11748 KB |
Output is correct |
109 |
Correct |
1 ms |
340 KB |
Output is correct |
110 |
Correct |
214 ms |
16020 KB |
Output is correct |
111 |
Correct |
267 ms |
18712 KB |
Output is correct |
112 |
Correct |
1 ms |
468 KB |
Output is correct |
113 |
Correct |
448 ms |
23216 KB |
Output is correct |
114 |
Incorrect |
82 ms |
9804 KB |
Output isn't correct |
115 |
Halted |
0 ms |
0 KB |
- |