#include "dreaming.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 100000 + 10;
const int INF = 1e9;
int n, m;
int dp[MAXN];
int dp2[MAXN];
int next[MAXN];
bool vis[MAXN];
std::vector <std::pair <int,int>> g[MAXN];
std::vector <int> dists;
void dfsDown(int node, int p)
{
dp[node] = dp2[node] = 0;
for (const auto &[u, d] : g[node])
{
if (u == p)
{
continue;
}
dfsDown(u, node);
if (dp[u] + d > dp[node])
{
dp2[node] = dp[node];
dp[node] = dp[u] + d;
next[node] = u;
} else if (dp[u] + d > dp2[node])
{
dp2[node] = dp[u] + d;
}
}
}
int ans;
int dfsUp(int node, int p, int edge)
{
vis[node] = true;
if (p != 0)
{
if (next[p] != node)
{
if (dp[p] + edge > dp[node])
{
dp2[node] = dp[node];
dp[node] = dp[p] + edge;
next[node] = p;
} else if (dp[p] + edge > dp2[node])
{
dp2[node] = dp[p] + edge;
}
} else
{
if (dp2[p] + edge > dp[node])
{
dp2[node] = dp[node];
dp[node] = dp2[p] + edge;
next[node] = p;
} else if (dp2[p] + edge > dp2[node])
{
dp2[node] = dp2[p] + edge;
}
}
}
dfsDown(node, 0);
ans = std::max(ans, dp[node] + dp2[node]);
int res = dp[node];
for (const auto &[u, d] : g[node])
{
if (u != p)
{
res = std::min(res, dfsUp(u, node, d));
}
}
return res;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
n = N;
m = M;
for (int i = 0 ; i < m ; ++i)
{
g[A[i] + 1].push_back({B[i] + 1, T[i]});
g[B[i] + 1].push_back({A[i] + 1, T[i]});
}
for (int i = 1 ; i <= n ; ++i)
{
if (vis[i])
{
continue;
}
assert(dp[i] == 0);
dfsDown(i, 0);
dists.push_back(dfsUp(i, 0, 0));
}
std::sort(dists.begin(), dists.end());
if (dists.size() == 1) return ans;
ans = std::max(ans, dists[dists.size() - 2] + dists[dists.size() - 1] + L);
if (dists.size() >= 3) ans = std::max(ans, dists[dists.size() - 3] + dists[dists.size() - 2] + 2*L);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1058 ms |
11596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2604 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2664 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1058 ms |
11596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
6488 KB |
Output is correct |
2 |
Correct |
15 ms |
6492 KB |
Output is correct |
3 |
Correct |
15 ms |
6488 KB |
Output is correct |
4 |
Correct |
15 ms |
6528 KB |
Output is correct |
5 |
Correct |
14 ms |
6532 KB |
Output is correct |
6 |
Correct |
20 ms |
7000 KB |
Output is correct |
7 |
Correct |
18 ms |
6744 KB |
Output is correct |
8 |
Correct |
14 ms |
6480 KB |
Output is correct |
9 |
Correct |
19 ms |
6460 KB |
Output is correct |
10 |
Correct |
15 ms |
6624 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
4560 KB |
Output is correct |
13 |
Correct |
4 ms |
4560 KB |
Output is correct |
14 |
Correct |
4 ms |
4560 KB |
Output is correct |
15 |
Correct |
4 ms |
4560 KB |
Output is correct |
16 |
Correct |
4 ms |
4560 KB |
Output is correct |
17 |
Correct |
6 ms |
4176 KB |
Output is correct |
18 |
Correct |
4 ms |
4560 KB |
Output is correct |
19 |
Correct |
6 ms |
4560 KB |
Output is correct |
20 |
Correct |
1 ms |
2656 KB |
Output is correct |
21 |
Correct |
1 ms |
2656 KB |
Output is correct |
22 |
Correct |
1 ms |
2656 KB |
Output is correct |
23 |
Correct |
6 ms |
4592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2604 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2664 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2784 KB |
Output is correct |
28 |
Correct |
3 ms |
2784 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
3 ms |
2784 KB |
Output is correct |
31 |
Correct |
3 ms |
2824 KB |
Output is correct |
32 |
Correct |
2 ms |
2656 KB |
Output is correct |
33 |
Correct |
4 ms |
2824 KB |
Output is correct |
34 |
Correct |
4 ms |
2820 KB |
Output is correct |
35 |
Correct |
2 ms |
2676 KB |
Output is correct |
36 |
Correct |
1 ms |
2656 KB |
Output is correct |
37 |
Correct |
2 ms |
2676 KB |
Output is correct |
38 |
Correct |
1 ms |
2656 KB |
Output is correct |
39 |
Correct |
2 ms |
2680 KB |
Output is correct |
40 |
Correct |
1 ms |
2656 KB |
Output is correct |
41 |
Correct |
2 ms |
2656 KB |
Output is correct |
42 |
Correct |
2 ms |
2656 KB |
Output is correct |
43 |
Correct |
2 ms |
2680 KB |
Output is correct |
44 |
Correct |
2 ms |
2684 KB |
Output is correct |
45 |
Correct |
2 ms |
2656 KB |
Output is correct |
46 |
Correct |
2 ms |
2656 KB |
Output is correct |
47 |
Correct |
2 ms |
2656 KB |
Output is correct |
48 |
Correct |
1 ms |
2656 KB |
Output is correct |
49 |
Correct |
2 ms |
2656 KB |
Output is correct |
50 |
Correct |
2 ms |
2680 KB |
Output is correct |
51 |
Correct |
2 ms |
2656 KB |
Output is correct |
52 |
Correct |
2 ms |
2656 KB |
Output is correct |
53 |
Correct |
1 ms |
2656 KB |
Output is correct |
54 |
Correct |
147 ms |
3168 KB |
Output is correct |
55 |
Correct |
113 ms |
3116 KB |
Output is correct |
56 |
Correct |
2 ms |
2820 KB |
Output is correct |
57 |
Correct |
2 ms |
2784 KB |
Output is correct |
58 |
Correct |
48 ms |
2912 KB |
Output is correct |
59 |
Correct |
61 ms |
2928 KB |
Output is correct |
60 |
Correct |
2 ms |
2784 KB |
Output is correct |
61 |
Correct |
2 ms |
2824 KB |
Output is correct |
62 |
Correct |
43 ms |
2820 KB |
Output is correct |
63 |
Correct |
41 ms |
2912 KB |
Output is correct |
64 |
Correct |
2 ms |
2776 KB |
Output is correct |
65 |
Correct |
2 ms |
2772 KB |
Output is correct |
66 |
Correct |
79 ms |
2908 KB |
Output is correct |
67 |
Correct |
42 ms |
2900 KB |
Output is correct |
68 |
Correct |
3 ms |
2772 KB |
Output is correct |
69 |
Correct |
3 ms |
2760 KB |
Output is correct |
70 |
Correct |
3 ms |
2804 KB |
Output is correct |
71 |
Correct |
2 ms |
2772 KB |
Output is correct |
72 |
Correct |
75 ms |
2900 KB |
Output is correct |
73 |
Correct |
79 ms |
2900 KB |
Output is correct |
74 |
Correct |
4 ms |
2900 KB |
Output is correct |
75 |
Correct |
3 ms |
2848 KB |
Output is correct |
76 |
Correct |
48 ms |
2912 KB |
Output is correct |
77 |
Correct |
61 ms |
2900 KB |
Output is correct |
78 |
Correct |
3 ms |
2772 KB |
Output is correct |
79 |
Correct |
2 ms |
2772 KB |
Output is correct |
80 |
Correct |
2 ms |
2644 KB |
Output is correct |
81 |
Correct |
2 ms |
2644 KB |
Output is correct |
82 |
Correct |
3 ms |
2900 KB |
Output is correct |
83 |
Correct |
3 ms |
2900 KB |
Output is correct |
84 |
Correct |
7 ms |
2808 KB |
Output is correct |
85 |
Correct |
2 ms |
2668 KB |
Output is correct |
86 |
Correct |
24 ms |
2848 KB |
Output is correct |
87 |
Correct |
59 ms |
2892 KB |
Output is correct |
88 |
Correct |
3 ms |
2772 KB |
Output is correct |
89 |
Correct |
2 ms |
2808 KB |
Output is correct |
90 |
Correct |
3 ms |
2784 KB |
Output is correct |
91 |
Correct |
3 ms |
2784 KB |
Output is correct |
92 |
Correct |
18 ms |
2784 KB |
Output is correct |
93 |
Correct |
21 ms |
2828 KB |
Output is correct |
94 |
Correct |
27 ms |
2784 KB |
Output is correct |
95 |
Correct |
38 ms |
2872 KB |
Output is correct |
96 |
Correct |
29 ms |
2880 KB |
Output is correct |
97 |
Correct |
28 ms |
2816 KB |
Output is correct |
98 |
Correct |
32 ms |
2892 KB |
Output is correct |
99 |
Correct |
54 ms |
2896 KB |
Output is correct |
100 |
Correct |
38 ms |
2832 KB |
Output is correct |
101 |
Correct |
38 ms |
2772 KB |
Output is correct |
102 |
Correct |
2 ms |
2644 KB |
Output is correct |
103 |
Correct |
2 ms |
2668 KB |
Output is correct |
104 |
Correct |
1 ms |
2644 KB |
Output is correct |
105 |
Correct |
1 ms |
2668 KB |
Output is correct |
106 |
Correct |
2 ms |
2652 KB |
Output is correct |
107 |
Correct |
1 ms |
2652 KB |
Output is correct |
108 |
Correct |
1 ms |
2672 KB |
Output is correct |
109 |
Correct |
2 ms |
2652 KB |
Output is correct |
110 |
Correct |
2 ms |
2652 KB |
Output is correct |
111 |
Correct |
1 ms |
2676 KB |
Output is correct |
112 |
Correct |
2 ms |
2652 KB |
Output is correct |
113 |
Correct |
5 ms |
2804 KB |
Output is correct |
114 |
Correct |
7 ms |
2792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1058 ms |
11596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |