#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 1e9;
vector<pair<int,int>> t[maxn];
int dp[maxn], pd[maxn];
bool visited[maxn];
int dfsdw(int v, int par = -1){
int ret = dp[v];
for (auto [u,c] : t[v]){
if (u == par)
continue;
int best = 0;
if (dp[v] != dp[u]+c)
best = dp[v];
else
best = pd[v];
if (best+c > dp[u]){
pd[u] = dp[u];
dp[u] = best+c;
}
else if (best+c > pd[u])
pd[u] = best+c;
ret = min(ret, dfsdw(u,v));
}
return ret;
}
void dfsup(int v, int par = -1){
visited[v] = 1;
dp[v] = 0, pd[v] = -inf;
for (auto [u,c] : t[v]){
if (u == par)
continue;
dfsup(u,v);
if (dp[u]+c >= dp[v]){
pd[v] = dp[v];
dp[v] = dp[u]+c;
}
else if (dp[u]+c >= pd[v])
pd[v] = dp[u]+c;
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i++){
t[A[i]].push_back({B[i],T[i]});
t[B[i]].push_back({A[i],T[i]});
}
vector<int> a;
for (int i = 0; i < N; i++){
if (visited[i])
continue;
dfsup(i);
a.push_back(dfsdw(i));
}
sort(a.rbegin(), a.rend());
if (a.size() == 1)
return a[0];
int answer = a[0] + a[1] + L;
if (a.size() > 2)
answer = max(answer, a[1]+a[2]+2*L);
return answer;
}
Compilation message
dreaming.cpp: In function 'int dfsdw(int, int)':
dreaming.cpp:13:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for (auto [u,c] : t[v]){
| ^
dreaming.cpp: In function 'void dfsup(int, int)':
dreaming.cpp:35:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
35 | for (auto [u,c] : t[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
6656 KB |
Output is correct |
2 |
Correct |
31 ms |
6656 KB |
Output is correct |
3 |
Correct |
28 ms |
6656 KB |
Output is correct |
4 |
Correct |
39 ms |
6648 KB |
Output is correct |
5 |
Correct |
28 ms |
6632 KB |
Output is correct |
6 |
Correct |
27 ms |
7168 KB |
Output is correct |
7 |
Correct |
27 ms |
6784 KB |
Output is correct |
8 |
Correct |
25 ms |
6520 KB |
Output is correct |
9 |
Correct |
27 ms |
6520 KB |
Output is correct |
10 |
Correct |
30 ms |
6776 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
4220 KB |
Output is correct |
13 |
Correct |
6 ms |
4220 KB |
Output is correct |
14 |
Correct |
6 ms |
4220 KB |
Output is correct |
15 |
Correct |
6 ms |
4220 KB |
Output is correct |
16 |
Correct |
5 ms |
4220 KB |
Output is correct |
17 |
Correct |
6 ms |
4092 KB |
Output is correct |
18 |
Correct |
6 ms |
4220 KB |
Output is correct |
19 |
Correct |
7 ms |
4220 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
4220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |