#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int NN = 100005;
int a[NN], b[NN], t[NN];
vector<pair<int, int>> g[NN];
int vis[NN];
int mx[NN], mx2[NN];
int far[NN];
int mn[NN];
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int n, m, l;
n = N, m = M, l = L;
for (int i = 0; i < m; ++i) {
a[i] = A[i];
b[i] = B[i];
t[i] = T[i];
}
for (int i = 0; i < m; ++i) {
g[a[i]].emplace_back(b[i], t[i]);
g[b[i]].emplace_back(a[i], t[i]);
}
int ncc = 0;
vector<int> d(n + 1);
int mxd = 0;
function<void(int, int)> Dfs = [&](int v, int p) {
vis[v] = ncc;
for (auto [u, w] : g[v]) {
if (u != p) {
Dfs(u, v);
mx2[v] = max(mx2[v], mx[u] + w);
if (mx2[v] > mx[v]) {
swap(mx2[v], mx[v]);
}
}
}
d[ncc] = max(d[ncc], mx[v] + mx2[v]);
mxd = max(mxd, d[ncc]);
};
for (int i = 1; i <= n + 1; ++i) mn[i] = 2e9;
function<void(int, int, int)> Dfs2 = [&](int v, int p, int up) {
far[v] = max(mx[v], up);
mn[ncc] = min(mn[ncc], far[v]);
for (auto [u, w] : g[v]) {
if (u == p) continue;
int nup = max(up, mx[v] == mx[u] + w ? mx2[v] : mx[v]) + w;
Dfs2(u, v, nup);
}
};
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
ncc++;
Dfs(i, i);
Dfs2(i, i, 0);
}
}
for (int i = ncc + 1; i <= n + 1; ++i) mn[i] = 0;
sort(mn + 1, mn + 1 + ncc, greater<int>());
int ans = max({mxd, l + mn[1] + mn[2], mn[2] + mn[3] + l + l});
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
16588 KB |
Output is correct |
2 |
Correct |
46 ms |
16076 KB |
Output is correct |
3 |
Correct |
30 ms |
14184 KB |
Output is correct |
4 |
Correct |
7 ms |
4820 KB |
Output is correct |
5 |
Correct |
6 ms |
3924 KB |
Output is correct |
6 |
Correct |
13 ms |
5872 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
16588 KB |
Output is correct |
2 |
Correct |
46 ms |
16076 KB |
Output is correct |
3 |
Correct |
30 ms |
14184 KB |
Output is correct |
4 |
Correct |
7 ms |
4820 KB |
Output is correct |
5 |
Correct |
6 ms |
3924 KB |
Output is correct |
6 |
Correct |
13 ms |
5872 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
7800 KB |
Output is correct |
2 |
Correct |
20 ms |
7756 KB |
Output is correct |
3 |
Correct |
21 ms |
7884 KB |
Output is correct |
4 |
Correct |
20 ms |
8020 KB |
Output is correct |
5 |
Correct |
19 ms |
7924 KB |
Output is correct |
6 |
Correct |
22 ms |
8316 KB |
Output is correct |
7 |
Correct |
25 ms |
8176 KB |
Output is correct |
8 |
Correct |
20 ms |
7764 KB |
Output is correct |
9 |
Correct |
21 ms |
7864 KB |
Output is correct |
10 |
Correct |
22 ms |
8168 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
8 ms |
4948 KB |
Output is correct |
13 |
Correct |
6 ms |
4948 KB |
Output is correct |
14 |
Correct |
6 ms |
4948 KB |
Output is correct |
15 |
Correct |
6 ms |
4968 KB |
Output is correct |
16 |
Correct |
7 ms |
4840 KB |
Output is correct |
17 |
Correct |
6 ms |
4452 KB |
Output is correct |
18 |
Correct |
7 ms |
5076 KB |
Output is correct |
19 |
Correct |
6 ms |
4820 KB |
Output is correct |
20 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
16588 KB |
Output is correct |
2 |
Correct |
46 ms |
16076 KB |
Output is correct |
3 |
Correct |
30 ms |
14184 KB |
Output is correct |
4 |
Correct |
7 ms |
4820 KB |
Output is correct |
5 |
Correct |
6 ms |
3924 KB |
Output is correct |
6 |
Correct |
13 ms |
5872 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |