#include <iostream>
#include <algorithm>
#include <vector>
#include "dreaming.h"
using namespace std;
int mn = 1e9, ans = 0;
vector <vector <pair <int, int>>> g(1e5);
vector <int> par(1e5, -1);
vector <int> down1(1e5, 0);
vector <int> down2(1e5, 0);
vector <int> up(1e5, 0);
vector <int> far(1e5, 0);
vector <bool> done(1e5, false);
vector <int> MN;
void dfs1(int x) {
for (auto p : g[x]) {
int i = p.first, w = p.second;
if (i == par[x]) continue;
par[i] = x;
dfs1(i);
if (down1[i] + w > down1[x]) down1[x] = down1[i] + w;
else if (down1[i] + w > down2[x]) down2[x] = down1[i] + w;
}
}
void dfs2(int x) {
for (auto p : g[x]) {
int i = p.first, w = p.second;
if (i == par[x]) continue;
up[i] = max(up[x], (down1[x] == down1[i] + w ? down2[x] : down1[x])) + w;
dfs2(i);
}
}
void dfs3(int x) {
far[x] = max(down1[x], up[x]);
mn = min(mn, far[x]);
ans = max(ans, far[x]);
done[x] = true;
for (auto p : g[x]) {
int i = p.first;
if (i == par[x]) continue;
dfs3(i);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
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] });
}
for (int i = 0; i < N; i++) {
if (done[i]) continue;
dfs1(i);
dfs2(i);
dfs3(i);
MN.push_back(mn);
mn = 1e9;
}
sort(MN.begin(), MN.end());
reverse(MN.begin(), MN.end());
if (MN.size() >= 2) ans = max(ans, MN[0] + MN[1] + L);
if (MN.size() >= 3) ans = max(ans, MN[1] + MN[2] + 2 * L);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13728 KB |
Output is correct |
2 |
Correct |
33 ms |
13424 KB |
Output is correct |
3 |
Correct |
24 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
5940 KB |
Output is correct |
5 |
Correct |
6 ms |
5424 KB |
Output is correct |
6 |
Correct |
14 ms |
6648 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4640 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
2 ms |
4564 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13728 KB |
Output is correct |
2 |
Correct |
33 ms |
13424 KB |
Output is correct |
3 |
Correct |
24 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
5940 KB |
Output is correct |
5 |
Correct |
6 ms |
5424 KB |
Output is correct |
6 |
Correct |
14 ms |
6648 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4640 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
7728 KB |
Output is correct |
2 |
Correct |
16 ms |
7724 KB |
Output is correct |
3 |
Correct |
16 ms |
7712 KB |
Output is correct |
4 |
Correct |
16 ms |
7716 KB |
Output is correct |
5 |
Correct |
16 ms |
7764 KB |
Output is correct |
6 |
Correct |
16 ms |
8152 KB |
Output is correct |
7 |
Correct |
18 ms |
7796 KB |
Output is correct |
8 |
Correct |
26 ms |
7632 KB |
Output is correct |
9 |
Correct |
18 ms |
7636 KB |
Output is correct |
10 |
Correct |
18 ms |
7852 KB |
Output is correct |
11 |
Correct |
2 ms |
4564 KB |
Output is correct |
12 |
Correct |
5 ms |
5200 KB |
Output is correct |
13 |
Correct |
5 ms |
5328 KB |
Output is correct |
14 |
Correct |
5 ms |
5280 KB |
Output is correct |
15 |
Correct |
5 ms |
5280 KB |
Output is correct |
16 |
Correct |
5 ms |
5280 KB |
Output is correct |
17 |
Correct |
5 ms |
5200 KB |
Output is correct |
18 |
Correct |
5 ms |
5328 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
2 ms |
4564 KB |
Output is correct |
21 |
Correct |
2 ms |
4564 KB |
Output is correct |
22 |
Correct |
2 ms |
4644 KB |
Output is correct |
23 |
Correct |
5 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
2 ms |
4564 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
13728 KB |
Output is correct |
2 |
Correct |
33 ms |
13424 KB |
Output is correct |
3 |
Correct |
24 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
5940 KB |
Output is correct |
5 |
Correct |
6 ms |
5424 KB |
Output is correct |
6 |
Correct |
14 ms |
6648 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4640 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |