#include "dreaming.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, l;
vector<pair<int, int>> dp;
vector<vector<pair<int, int>>> g;
int dfs1(int u, int par, int c, vector<int> &cmp_of) {
cmp_of[u] = c;
for (auto [v, w] : g[u]) {
if (v == par) continue;
int time = dfs1(v, u, c, cmp_of) + w;
dp[u] = max(dp[u], max(make_pair(time, dp[u].first), make_pair(dp[u].first, time)));
}
return dp[u].first;
}
void dfs2(int u, int par, int c, vector<int> &cmp_of) {
cmp_of[u] = c;
for (auto [v, w] : g[u]) {
if (v == par) continue;
int time = (dp[u].first != dp[v].first + w ? dp[u].first : dp[u].second) + w;
dp[v] = max(dp[v], max(make_pair(time, dp[v].first), make_pair(dp[v].first, time)));
dfs2(v, u, c, cmp_of);
}
}
int solve() {
dp.resize(n, {0, 0});
vector<int> cmp_of(n, -1), longest;
for (int i = 0; i < n; i++) {
if (cmp_of[i] == -1) {
dfs1(i, -1, longest.size(), cmp_of);
longest.push_back(n + 1);
}
}
fill(cmp_of.begin(), cmp_of.end(), -1);
longest.clear();
int ans = 0;
for (int i = 0; i < n; i++) {
if (cmp_of[i] == -1) {
dfs2(i, -1, longest.size(), cmp_of);
longest.push_back(n + 1);
}
longest[cmp_of[i]] = min(longest[cmp_of[i]], dp[i].first);
ans = max(ans, dp[i].first + dp[i].second);
}
sort(longest.rbegin(), longest.rend());
if (longest.size() > 1) ans = max(ans, longest[0] + l + longest[1]);
if (longest.size() > 2) ans = max(ans, longest[1] + 2 * l + longest[2]);
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
m = M;
l = L;
g.resize(n);
for (int i = 0; i < m; i++) {
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
return solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
14900 KB |
Output is correct |
2 |
Correct |
43 ms |
14396 KB |
Output is correct |
3 |
Correct |
29 ms |
12288 KB |
Output is correct |
4 |
Correct |
7 ms |
2388 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
11 ms |
3580 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
20 ms |
6412 KB |
Output is correct |
9 |
Correct |
28 ms |
9744 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
42 ms |
11060 KB |
Output is correct |
12 |
Correct |
55 ms |
12884 KB |
Output is correct |
13 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
14900 KB |
Output is correct |
2 |
Correct |
43 ms |
14396 KB |
Output is correct |
3 |
Correct |
29 ms |
12288 KB |
Output is correct |
4 |
Correct |
7 ms |
2388 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
11 ms |
3580 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
20 ms |
6412 KB |
Output is correct |
9 |
Correct |
28 ms |
9744 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
42 ms |
11060 KB |
Output is correct |
12 |
Correct |
55 ms |
12884 KB |
Output is correct |
13 |
Correct |
1 ms |
444 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
6616 KB |
Output is correct |
2 |
Correct |
22 ms |
6740 KB |
Output is correct |
3 |
Correct |
22 ms |
6828 KB |
Output is correct |
4 |
Correct |
23 ms |
6664 KB |
Output is correct |
5 |
Correct |
21 ms |
6696 KB |
Output is correct |
6 |
Correct |
24 ms |
7344 KB |
Output is correct |
7 |
Correct |
23 ms |
6968 KB |
Output is correct |
8 |
Correct |
20 ms |
6520 KB |
Output is correct |
9 |
Correct |
20 ms |
6592 KB |
Output is correct |
10 |
Correct |
22 ms |
6940 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
14900 KB |
Output is correct |
2 |
Correct |
43 ms |
14396 KB |
Output is correct |
3 |
Correct |
29 ms |
12288 KB |
Output is correct |
4 |
Correct |
7 ms |
2388 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
11 ms |
3580 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
20 ms |
6412 KB |
Output is correct |
9 |
Correct |
28 ms |
9744 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
42 ms |
11060 KB |
Output is correct |
12 |
Correct |
55 ms |
12884 KB |
Output is correct |
13 |
Correct |
1 ms |
444 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |