#include <bits/stdc++.h>
using namespace std;
#include "dreaming.h"
using ll = long long;
// #define int ll
const int N = 1e5;
#define fs first
#define sc second
#define mp make_pair
vector<pair<int, int>> g[N];
int vis[N], mxd[N], mx[N];
multiset<int> ms[N];
void dfs(int x, int p) {
vis[x] = 1;
for (auto [u, w]: g[x]) {
if (u != p) {
dfs(u, x);
mxd[x] = max(mxd[x], mxd[u] + w);
}
}
}
void dfs2(int x, int p, int f) {
ms[x].insert(f);
for (auto [u, w]: g[x]) {
if (u != p) {
ms[x].insert(mxd[u] + w);
}
}
mx[x] = *(ms[x].rbegin());
for (auto [u, w]: g[x]) {
if (u != p) {
ms[x].erase(ms[x].lower_bound(mxd[u] + w));
dfs2(u, x, *(ms[x].rbegin()) + w);
ms[x].insert(mxd[u] + w);
}
}
}
int dfs3(int x, int p) {
int res = mx[x];
for (auto [u, w]: g[x]) {
if (u == p) continue;
res = min(res, dfs3(u, x));
}
return res;
}
int travelTime(int n, int m, int l, int u[], int v[], int t[]){
for (int i = 0; i < n; ++i) {
vis[i] = 0;
mxd[i] = 0;
mx[i] = 0;
}
for (int i = 0; i < m; ++i) {
g[u[i]].push_back(mp(v[i], t[i]));
g[v[i]].push_back(mp(u[i], t[i]));
}
int ok = 1;
vector<int> mxdist;
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
dfs(i, -1);
dfs2(i, -1, 0);
mxdist.push_back(dfs3(i, -1));
}
ok &= (int) g[i].size() <= 1;
}
sort(mxdist.begin(), mxdist.end());
int ans = 0;
for (int i = 0; i < n; ++i) {
ans = max(ans, mx[i]);
}
int sz = (int) mxdist.size();
assert(sz > 0);
if (sz == 1) {
assert(m + 1 == n);
} else {
ans = max(ans, mxdist[sz - 1] + mxdist[sz - 2] + l);
for (int i = 0; i < sz - 2; ++i) {
ans = max(ans, mxdist[sz - 1] + mxdist[i] + l + l);
}
}
// for (int i = 0; i < n; ++i) {
// cout << "! " << i << ' ' << mx[i] << '\n';
// }
return ans;
}
// int32_t main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// int nn = 4, M = 2, L = 2;
// int A[]{1, 0};//{0,8,2,5,5,1,1,10};
// int B[]{2, 4};//{8,2,7,11,1,3,9,6};
// int C[]{1000, 2000};//{4,2,4,3,7,1,5,3};
// cout << "answer: " << travelTime(nn, M, L, A, B, C);
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
26756 KB |
Output is correct |
2 |
Correct |
61 ms |
26196 KB |
Output is correct |
3 |
Correct |
36 ms |
23128 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
11096 KB |
Output is correct |
6 |
Correct |
12 ms |
13352 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
24 ms |
16900 KB |
Output is correct |
9 |
Correct |
39 ms |
20308 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
52 ms |
22100 KB |
Output is correct |
12 |
Correct |
57 ms |
24404 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
3 ms |
9564 KB |
Output is correct |
3 |
Correct |
3 ms |
9560 KB |
Output is correct |
4 |
Correct |
2 ms |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9764 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
26756 KB |
Output is correct |
2 |
Correct |
61 ms |
26196 KB |
Output is correct |
3 |
Correct |
36 ms |
23128 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
11096 KB |
Output is correct |
6 |
Correct |
12 ms |
13352 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
24 ms |
16900 KB |
Output is correct |
9 |
Correct |
39 ms |
20308 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
52 ms |
22100 KB |
Output is correct |
12 |
Correct |
57 ms |
24404 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
3 ms |
9564 KB |
Output is correct |
16 |
Correct |
3 ms |
9560 KB |
Output is correct |
17 |
Correct |
2 ms |
9560 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
3 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9764 KB |
Output is correct |
32 |
Correct |
2 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
9564 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
39 |
Correct |
69 ms |
26724 KB |
Output is correct |
40 |
Correct |
66 ms |
26316 KB |
Output is correct |
41 |
Correct |
46 ms |
22796 KB |
Output is correct |
42 |
Correct |
8 ms |
12124 KB |
Output is correct |
43 |
Correct |
2 ms |
9564 KB |
Output is correct |
44 |
Correct |
2 ms |
9560 KB |
Output is correct |
45 |
Correct |
2 ms |
9564 KB |
Output is correct |
46 |
Correct |
2 ms |
9564 KB |
Output is correct |
47 |
Correct |
2 ms |
9564 KB |
Output is correct |
48 |
Correct |
2 ms |
9564 KB |
Output is correct |
49 |
Correct |
3 ms |
9560 KB |
Output is correct |
50 |
Correct |
2 ms |
9564 KB |
Output is correct |
51 |
Correct |
2 ms |
9560 KB |
Output is correct |
52 |
Correct |
2 ms |
9564 KB |
Output is correct |
53 |
Correct |
2 ms |
9564 KB |
Output is correct |
54 |
Correct |
2 ms |
9564 KB |
Output is correct |
55 |
Correct |
2 ms |
9564 KB |
Output is correct |
56 |
Correct |
2 ms |
9564 KB |
Output is correct |
57 |
Correct |
72 ms |
23120 KB |
Output is correct |
58 |
Correct |
63 ms |
23028 KB |
Output is correct |
59 |
Correct |
70 ms |
23160 KB |
Output is correct |
60 |
Correct |
64 ms |
23076 KB |
Output is correct |
61 |
Correct |
71 ms |
23128 KB |
Output is correct |
62 |
Correct |
73 ms |
22868 KB |
Output is correct |
63 |
Correct |
60 ms |
22860 KB |
Output is correct |
64 |
Correct |
77 ms |
22872 KB |
Output is correct |
65 |
Correct |
63 ms |
22868 KB |
Output is correct |
66 |
Correct |
72 ms |
23056 KB |
Output is correct |
67 |
Correct |
73 ms |
23124 KB |
Output is correct |
68 |
Correct |
77 ms |
23020 KB |
Output is correct |
69 |
Correct |
70 ms |
23044 KB |
Output is correct |
70 |
Correct |
73 ms |
23004 KB |
Output is correct |
71 |
Correct |
2 ms |
9564 KB |
Output is correct |
72 |
Correct |
3 ms |
10076 KB |
Output is correct |
73 |
Correct |
4 ms |
10072 KB |
Output is correct |
74 |
Correct |
3 ms |
10072 KB |
Output is correct |
75 |
Correct |
4 ms |
10076 KB |
Output is correct |
76 |
Correct |
4 ms |
10076 KB |
Output is correct |
77 |
Correct |
3 ms |
10076 KB |
Output is correct |
78 |
Correct |
4 ms |
10076 KB |
Output is correct |
79 |
Correct |
3 ms |
9932 KB |
Output is correct |
80 |
Correct |
92 ms |
22960 KB |
Output is correct |
81 |
Correct |
88 ms |
23144 KB |
Output is correct |
82 |
Correct |
65 ms |
22868 KB |
Output is correct |
83 |
Correct |
73 ms |
22864 KB |
Output is correct |
84 |
Correct |
3 ms |
9816 KB |
Output is correct |
85 |
Correct |
3 ms |
9820 KB |
Output is correct |
86 |
Correct |
3 ms |
9820 KB |
Output is correct |
87 |
Correct |
4 ms |
9820 KB |
Output is correct |
88 |
Correct |
4 ms |
9820 KB |
Output is correct |
89 |
Correct |
4 ms |
9820 KB |
Output is correct |
90 |
Correct |
3 ms |
9820 KB |
Output is correct |
91 |
Correct |
3 ms |
10076 KB |
Output is correct |
92 |
Correct |
3 ms |
9820 KB |
Output is correct |
93 |
Correct |
3 ms |
9820 KB |
Output is correct |
94 |
Correct |
3 ms |
9564 KB |
Output is correct |
95 |
Correct |
2 ms |
9564 KB |
Output is correct |
96 |
Correct |
2 ms |
9564 KB |
Output is correct |
97 |
Correct |
2 ms |
9564 KB |
Output is correct |
98 |
Correct |
2 ms |
9564 KB |
Output is correct |
99 |
Correct |
2 ms |
9564 KB |
Output is correct |
100 |
Correct |
2 ms |
9564 KB |
Output is correct |
101 |
Correct |
2 ms |
9560 KB |
Output is correct |
102 |
Correct |
2 ms |
9564 KB |
Output is correct |
103 |
Correct |
2 ms |
9560 KB |
Output is correct |
104 |
Correct |
7 ms |
11100 KB |
Output is correct |
105 |
Correct |
13 ms |
13276 KB |
Output is correct |
106 |
Correct |
2 ms |
9564 KB |
Output is correct |
107 |
Correct |
26 ms |
16852 KB |
Output is correct |
108 |
Correct |
35 ms |
20316 KB |
Output is correct |
109 |
Correct |
2 ms |
9564 KB |
Output is correct |
110 |
Correct |
61 ms |
22092 KB |
Output is correct |
111 |
Correct |
58 ms |
24148 KB |
Output is correct |
112 |
Correct |
2 ms |
9816 KB |
Output is correct |
113 |
Correct |
94 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
17752 KB |
Output is correct |
2 |
Incorrect |
27 ms |
17880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
3 ms |
9564 KB |
Output is correct |
3 |
Correct |
3 ms |
9560 KB |
Output is correct |
4 |
Correct |
2 ms |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9764 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
3 ms |
9560 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9564 KB |
Output is correct |
30 |
Correct |
3 ms |
9904 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Incorrect |
2 ms |
9564 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
26756 KB |
Output is correct |
2 |
Correct |
61 ms |
26196 KB |
Output is correct |
3 |
Correct |
36 ms |
23128 KB |
Output is correct |
4 |
Correct |
8 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
11096 KB |
Output is correct |
6 |
Correct |
12 ms |
13352 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
24 ms |
16900 KB |
Output is correct |
9 |
Correct |
39 ms |
20308 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
52 ms |
22100 KB |
Output is correct |
12 |
Correct |
57 ms |
24404 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
3 ms |
9564 KB |
Output is correct |
16 |
Correct |
3 ms |
9560 KB |
Output is correct |
17 |
Correct |
2 ms |
9560 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
3 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9764 KB |
Output is correct |
32 |
Correct |
2 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
9564 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
39 |
Correct |
69 ms |
26724 KB |
Output is correct |
40 |
Correct |
66 ms |
26316 KB |
Output is correct |
41 |
Correct |
46 ms |
22796 KB |
Output is correct |
42 |
Correct |
8 ms |
12124 KB |
Output is correct |
43 |
Correct |
2 ms |
9564 KB |
Output is correct |
44 |
Correct |
2 ms |
9560 KB |
Output is correct |
45 |
Correct |
2 ms |
9564 KB |
Output is correct |
46 |
Correct |
2 ms |
9564 KB |
Output is correct |
47 |
Correct |
2 ms |
9564 KB |
Output is correct |
48 |
Correct |
2 ms |
9564 KB |
Output is correct |
49 |
Correct |
3 ms |
9560 KB |
Output is correct |
50 |
Correct |
2 ms |
9564 KB |
Output is correct |
51 |
Correct |
2 ms |
9560 KB |
Output is correct |
52 |
Correct |
2 ms |
9564 KB |
Output is correct |
53 |
Correct |
2 ms |
9564 KB |
Output is correct |
54 |
Correct |
2 ms |
9564 KB |
Output is correct |
55 |
Correct |
2 ms |
9564 KB |
Output is correct |
56 |
Correct |
2 ms |
9564 KB |
Output is correct |
57 |
Correct |
72 ms |
23120 KB |
Output is correct |
58 |
Correct |
63 ms |
23028 KB |
Output is correct |
59 |
Correct |
70 ms |
23160 KB |
Output is correct |
60 |
Correct |
64 ms |
23076 KB |
Output is correct |
61 |
Correct |
71 ms |
23128 KB |
Output is correct |
62 |
Correct |
73 ms |
22868 KB |
Output is correct |
63 |
Correct |
60 ms |
22860 KB |
Output is correct |
64 |
Correct |
77 ms |
22872 KB |
Output is correct |
65 |
Correct |
63 ms |
22868 KB |
Output is correct |
66 |
Correct |
72 ms |
23056 KB |
Output is correct |
67 |
Correct |
73 ms |
23124 KB |
Output is correct |
68 |
Correct |
77 ms |
23020 KB |
Output is correct |
69 |
Correct |
70 ms |
23044 KB |
Output is correct |
70 |
Correct |
73 ms |
23004 KB |
Output is correct |
71 |
Correct |
2 ms |
9564 KB |
Output is correct |
72 |
Correct |
3 ms |
10076 KB |
Output is correct |
73 |
Correct |
4 ms |
10072 KB |
Output is correct |
74 |
Correct |
3 ms |
10072 KB |
Output is correct |
75 |
Correct |
4 ms |
10076 KB |
Output is correct |
76 |
Correct |
4 ms |
10076 KB |
Output is correct |
77 |
Correct |
3 ms |
10076 KB |
Output is correct |
78 |
Correct |
4 ms |
10076 KB |
Output is correct |
79 |
Correct |
3 ms |
9932 KB |
Output is correct |
80 |
Correct |
92 ms |
22960 KB |
Output is correct |
81 |
Correct |
88 ms |
23144 KB |
Output is correct |
82 |
Correct |
65 ms |
22868 KB |
Output is correct |
83 |
Correct |
73 ms |
22864 KB |
Output is correct |
84 |
Correct |
3 ms |
9816 KB |
Output is correct |
85 |
Correct |
3 ms |
9820 KB |
Output is correct |
86 |
Correct |
3 ms |
9820 KB |
Output is correct |
87 |
Correct |
4 ms |
9820 KB |
Output is correct |
88 |
Correct |
4 ms |
9820 KB |
Output is correct |
89 |
Correct |
4 ms |
9820 KB |
Output is correct |
90 |
Correct |
3 ms |
9820 KB |
Output is correct |
91 |
Correct |
3 ms |
10076 KB |
Output is correct |
92 |
Correct |
3 ms |
9820 KB |
Output is correct |
93 |
Correct |
3 ms |
9820 KB |
Output is correct |
94 |
Correct |
3 ms |
9564 KB |
Output is correct |
95 |
Correct |
2 ms |
9564 KB |
Output is correct |
96 |
Correct |
2 ms |
9564 KB |
Output is correct |
97 |
Correct |
2 ms |
9564 KB |
Output is correct |
98 |
Correct |
2 ms |
9564 KB |
Output is correct |
99 |
Correct |
2 ms |
9564 KB |
Output is correct |
100 |
Correct |
2 ms |
9564 KB |
Output is correct |
101 |
Correct |
2 ms |
9560 KB |
Output is correct |
102 |
Correct |
2 ms |
9564 KB |
Output is correct |
103 |
Correct |
2 ms |
9560 KB |
Output is correct |
104 |
Correct |
7 ms |
11100 KB |
Output is correct |
105 |
Correct |
13 ms |
13276 KB |
Output is correct |
106 |
Correct |
2 ms |
9564 KB |
Output is correct |
107 |
Correct |
26 ms |
16852 KB |
Output is correct |
108 |
Correct |
35 ms |
20316 KB |
Output is correct |
109 |
Correct |
2 ms |
9564 KB |
Output is correct |
110 |
Correct |
61 ms |
22092 KB |
Output is correct |
111 |
Correct |
58 ms |
24148 KB |
Output is correct |
112 |
Correct |
2 ms |
9816 KB |
Output is correct |
113 |
Correct |
94 ms |
33116 KB |
Output is correct |
114 |
Correct |
31 ms |
17752 KB |
Output is correct |
115 |
Incorrect |
27 ms |
17880 KB |
Output isn't correct |
116 |
Halted |
0 ms |
0 KB |
- |