#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5;
vector<pair<int, int>> ad[mxN];
bool vis[mxN];
int dp[mxN][2];
int mn = 1e9 + 5;
int ans = 0;
void dfs(int node) {
vis[node] = true;
for (auto [child, w] : ad[node]) {
if (vis[child]) continue;
dfs(child);
if (dp[child][1] + w >= dp[node][1]) {
dp[node][0] = dp[node][1];
dp[node][1] = dp[child][1] + w;
} else if (dp[child][1] + w > dp[node][0]) {
dp[node][0] = dp[child][1] + w;
}
}
ans = max(ans, dp[node][1] + dp[node][0]);
}
void dfs1(int node, int par, int cur) {
mn = min(mn, max(cur, dp[node][1]));
for (auto [child, w] : ad[node]) {
if (child == par) continue;
dfs1(child, node, max(cur, (dp[node][1] == dp[child][1] + w ? dp[node][0] : dp[node][1])) + w);
}
}
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
for (int i = 0; i < m; i++) {
ad[a[i]].push_back({b[i], t[i]});
ad[b[i]].push_back({a[i], t[i]});
}
vector<int> v;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i);
mn = 1e9 + 5;
dfs1(i, -1, 0);
v.push_back(mn);
}
}
sort(v.rbegin(), v.rend());
if (v.size() >= 1) {
ans = max(ans, v[0]);
}
if (v.size() >= 1) {
ans = max(ans, v[0] + v[1] + l);
}
if (v.size() >= 2) {
ans = max(ans, v[1] + v[2] + 2 * l);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs(int)':
dreaming.cpp:12:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
12 | for (auto [child, w] : ad[node]) {
| ^
dreaming.cpp: In function 'void dfs1(int, int, int)':
dreaming.cpp:26:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for (auto [child, w] : ad[node]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
12028 KB |
Output is correct |
2 |
Correct |
55 ms |
11724 KB |
Output is correct |
3 |
Correct |
44 ms |
10544 KB |
Output is correct |
4 |
Correct |
8 ms |
3916 KB |
Output is correct |
5 |
Correct |
7 ms |
3404 KB |
Output is correct |
6 |
Correct |
13 ms |
4684 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
12028 KB |
Output is correct |
2 |
Correct |
55 ms |
11724 KB |
Output is correct |
3 |
Correct |
44 ms |
10544 KB |
Output is correct |
4 |
Correct |
8 ms |
3916 KB |
Output is correct |
5 |
Correct |
7 ms |
3404 KB |
Output is correct |
6 |
Correct |
13 ms |
4684 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
6084 KB |
Output is correct |
2 |
Correct |
32 ms |
6200 KB |
Output is correct |
3 |
Correct |
24 ms |
6092 KB |
Output is correct |
4 |
Correct |
31 ms |
6152 KB |
Output is correct |
5 |
Correct |
29 ms |
6104 KB |
Output is correct |
6 |
Correct |
27 ms |
6580 KB |
Output is correct |
7 |
Correct |
31 ms |
6300 KB |
Output is correct |
8 |
Correct |
22 ms |
6100 KB |
Output is correct |
9 |
Correct |
22 ms |
6024 KB |
Output is correct |
10 |
Correct |
23 ms |
6288 KB |
Output is correct |
11 |
Correct |
3 ms |
2636 KB |
Output is correct |
12 |
Correct |
6 ms |
3784 KB |
Output is correct |
13 |
Correct |
6 ms |
3960 KB |
Output is correct |
14 |
Correct |
6 ms |
3784 KB |
Output is correct |
15 |
Correct |
6 ms |
3784 KB |
Output is correct |
16 |
Correct |
6 ms |
3784 KB |
Output is correct |
17 |
Correct |
6 ms |
3528 KB |
Output is correct |
18 |
Correct |
6 ms |
3912 KB |
Output is correct |
19 |
Correct |
5 ms |
3784 KB |
Output is correct |
20 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
12028 KB |
Output is correct |
2 |
Correct |
55 ms |
11724 KB |
Output is correct |
3 |
Correct |
44 ms |
10544 KB |
Output is correct |
4 |
Correct |
8 ms |
3916 KB |
Output is correct |
5 |
Correct |
7 ms |
3404 KB |
Output is correct |
6 |
Correct |
13 ms |
4684 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |