#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int maxn = 1e5 + 5;
int dist[maxn];
vector<int> vis;
vector<pair<int, int>> graph[maxn];
void dfs(int u, int p, int d) {
dist[u] = d;
vis.push_back(u);
for (auto& [v, w] : graph[u]) {
if (v != p) {
dfs(v, u, d + w);
}
}
}
void dfs(int u) {
vis.clear();
dfs(u, -1, 0);
}
pair<int, int> farthest() {
pair<int, int> opt {-1, 0};
for (auto& a : vis) {
opt = max(opt, pair<int, int> {dist[a], a});
}
return opt;
}
extern "C" int travelTime(int n,
int m,
int k,
int arru[],
int arrv[],
int arrw[]) {
for (int i = 0; i < m; i++) {
int u = arru[i], v = arrv[i], w = arrw[i];
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
int ans = 0;
bool cvis[n] {};
int d1[n], d2[n];
vector<int> comps;
for (int i = 0; i < n; i++) {
if (cvis[i]) {
continue;
}
dfs(i);
int u = farthest().second;
for (auto& a : vis) {
cvis[a] = true;
}
dfs(u);
auto [diam, v] = farthest();
ans = max(ans, diam);
for (auto& a : vis) {
d1[a] = dist[a];
}
dfs(v);
for (auto& a : vis) {
d2[a] = dist[a];
}
int mn = 2e9;
for (auto& a : vis) {
mn = min(mn, max(d1[a], d2[a]));
}
comps.push_back(mn);
}
return max(ans, comps[0] + comps[1] + k);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
15052 KB |
Output is correct |
2 |
Correct |
44 ms |
15048 KB |
Output is correct |
3 |
Correct |
30 ms |
11040 KB |
Output is correct |
4 |
Correct |
7 ms |
4564 KB |
Output is correct |
5 |
Correct |
7 ms |
3664 KB |
Output is correct |
6 |
Correct |
16 ms |
5492 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
22 ms |
7124 KB |
Output is correct |
9 |
Correct |
27 ms |
8908 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
42 ms |
10956 KB |
Output is correct |
12 |
Correct |
49 ms |
12876 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2660 KB |
Output is correct |
13 |
Correct |
2 ms |
2664 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2664 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2632 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
15052 KB |
Output is correct |
2 |
Correct |
44 ms |
15048 KB |
Output is correct |
3 |
Correct |
30 ms |
11040 KB |
Output is correct |
4 |
Correct |
7 ms |
4564 KB |
Output is correct |
5 |
Correct |
7 ms |
3664 KB |
Output is correct |
6 |
Correct |
16 ms |
5492 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
22 ms |
7124 KB |
Output is correct |
9 |
Correct |
27 ms |
8908 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
42 ms |
10956 KB |
Output is correct |
12 |
Correct |
49 ms |
12876 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2664 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2664 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 |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2660 KB |
Output is correct |
26 |
Correct |
2 ms |
2664 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
1 ms |
2664 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2632 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
2 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
43 ms |
15180 KB |
Output is correct |
40 |
Correct |
45 ms |
15064 KB |
Output is correct |
41 |
Correct |
28 ms |
10952 KB |
Output is correct |
42 |
Correct |
7 ms |
4564 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
1 ms |
2644 KB |
Output is correct |
46 |
Correct |
2 ms |
2664 KB |
Output is correct |
47 |
Correct |
3 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2644 KB |
Output is correct |
49 |
Correct |
2 ms |
2644 KB |
Output is correct |
50 |
Correct |
2 ms |
2644 KB |
Output is correct |
51 |
Correct |
1 ms |
2644 KB |
Output is correct |
52 |
Correct |
2 ms |
2644 KB |
Output is correct |
53 |
Correct |
2 ms |
2664 KB |
Output is correct |
54 |
Correct |
2 ms |
2644 KB |
Output is correct |
55 |
Correct |
1 ms |
2644 KB |
Output is correct |
56 |
Correct |
1 ms |
2644 KB |
Output is correct |
57 |
Correct |
68 ms |
10952 KB |
Output is correct |
58 |
Correct |
59 ms |
10768 KB |
Output is correct |
59 |
Correct |
58 ms |
10948 KB |
Output is correct |
60 |
Correct |
65 ms |
10700 KB |
Output is correct |
61 |
Correct |
61 ms |
11064 KB |
Output is correct |
62 |
Correct |
56 ms |
10792 KB |
Output is correct |
63 |
Correct |
50 ms |
10700 KB |
Output is correct |
64 |
Correct |
51 ms |
10760 KB |
Output is correct |
65 |
Correct |
56 ms |
11036 KB |
Output is correct |
66 |
Correct |
54 ms |
11016 KB |
Output is correct |
67 |
Correct |
62 ms |
10908 KB |
Output is correct |
68 |
Correct |
65 ms |
10884 KB |
Output is correct |
69 |
Correct |
59 ms |
10996 KB |
Output is correct |
70 |
Correct |
64 ms |
10956 KB |
Output is correct |
71 |
Correct |
1 ms |
2644 KB |
Output is correct |
72 |
Correct |
3 ms |
2900 KB |
Output is correct |
73 |
Correct |
3 ms |
2900 KB |
Output is correct |
74 |
Correct |
3 ms |
2924 KB |
Output is correct |
75 |
Correct |
3 ms |
2900 KB |
Output is correct |
76 |
Correct |
3 ms |
2884 KB |
Output is correct |
77 |
Correct |
3 ms |
2932 KB |
Output is correct |
78 |
Correct |
3 ms |
2900 KB |
Output is correct |
79 |
Correct |
3 ms |
2900 KB |
Output is correct |
80 |
Correct |
63 ms |
10956 KB |
Output is correct |
81 |
Correct |
58 ms |
10952 KB |
Output is correct |
82 |
Correct |
55 ms |
11016 KB |
Output is correct |
83 |
Correct |
54 ms |
11036 KB |
Output is correct |
84 |
Correct |
2 ms |
2800 KB |
Output is correct |
85 |
Correct |
3 ms |
2772 KB |
Output is correct |
86 |
Correct |
3 ms |
2772 KB |
Output is correct |
87 |
Correct |
3 ms |
2772 KB |
Output is correct |
88 |
Correct |
3 ms |
2800 KB |
Output is correct |
89 |
Correct |
2 ms |
2832 KB |
Output is correct |
90 |
Correct |
3 ms |
2772 KB |
Output is correct |
91 |
Correct |
3 ms |
2900 KB |
Output is correct |
92 |
Correct |
3 ms |
2884 KB |
Output is correct |
93 |
Correct |
3 ms |
2804 KB |
Output is correct |
94 |
Correct |
2 ms |
2644 KB |
Output is correct |
95 |
Correct |
2 ms |
2588 KB |
Output is correct |
96 |
Correct |
2 ms |
2644 KB |
Output is correct |
97 |
Correct |
1 ms |
2576 KB |
Output is correct |
98 |
Correct |
2 ms |
2644 KB |
Output is correct |
99 |
Correct |
1 ms |
2656 KB |
Output is correct |
100 |
Correct |
2 ms |
2660 KB |
Output is correct |
101 |
Correct |
1 ms |
2644 KB |
Output is correct |
102 |
Correct |
2 ms |
2644 KB |
Output is correct |
103 |
Correct |
2 ms |
2644 KB |
Output is correct |
104 |
Correct |
6 ms |
3688 KB |
Output is correct |
105 |
Correct |
12 ms |
5468 KB |
Output is correct |
106 |
Correct |
2 ms |
2644 KB |
Output is correct |
107 |
Correct |
22 ms |
7108 KB |
Output is correct |
108 |
Correct |
31 ms |
8908 KB |
Output is correct |
109 |
Correct |
2 ms |
2644 KB |
Output is correct |
110 |
Correct |
41 ms |
10904 KB |
Output is correct |
111 |
Correct |
47 ms |
12940 KB |
Output is correct |
112 |
Correct |
2 ms |
2772 KB |
Output is correct |
113 |
Correct |
77 ms |
16544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2660 KB |
Output is correct |
13 |
Correct |
2 ms |
2664 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2664 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2632 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2672 KB |
Output is correct |
27 |
Correct |
2 ms |
2700 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Incorrect |
2 ms |
2672 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
15052 KB |
Output is correct |
2 |
Correct |
44 ms |
15048 KB |
Output is correct |
3 |
Correct |
30 ms |
11040 KB |
Output is correct |
4 |
Correct |
7 ms |
4564 KB |
Output is correct |
5 |
Correct |
7 ms |
3664 KB |
Output is correct |
6 |
Correct |
16 ms |
5492 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
22 ms |
7124 KB |
Output is correct |
9 |
Correct |
27 ms |
8908 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
42 ms |
10956 KB |
Output is correct |
12 |
Correct |
49 ms |
12876 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2664 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2664 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 |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2660 KB |
Output is correct |
26 |
Correct |
2 ms |
2664 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
1 ms |
2664 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2632 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
2 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
43 ms |
15180 KB |
Output is correct |
40 |
Correct |
45 ms |
15064 KB |
Output is correct |
41 |
Correct |
28 ms |
10952 KB |
Output is correct |
42 |
Correct |
7 ms |
4564 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
1 ms |
2644 KB |
Output is correct |
46 |
Correct |
2 ms |
2664 KB |
Output is correct |
47 |
Correct |
3 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2644 KB |
Output is correct |
49 |
Correct |
2 ms |
2644 KB |
Output is correct |
50 |
Correct |
2 ms |
2644 KB |
Output is correct |
51 |
Correct |
1 ms |
2644 KB |
Output is correct |
52 |
Correct |
2 ms |
2644 KB |
Output is correct |
53 |
Correct |
2 ms |
2664 KB |
Output is correct |
54 |
Correct |
2 ms |
2644 KB |
Output is correct |
55 |
Correct |
1 ms |
2644 KB |
Output is correct |
56 |
Correct |
1 ms |
2644 KB |
Output is correct |
57 |
Correct |
68 ms |
10952 KB |
Output is correct |
58 |
Correct |
59 ms |
10768 KB |
Output is correct |
59 |
Correct |
58 ms |
10948 KB |
Output is correct |
60 |
Correct |
65 ms |
10700 KB |
Output is correct |
61 |
Correct |
61 ms |
11064 KB |
Output is correct |
62 |
Correct |
56 ms |
10792 KB |
Output is correct |
63 |
Correct |
50 ms |
10700 KB |
Output is correct |
64 |
Correct |
51 ms |
10760 KB |
Output is correct |
65 |
Correct |
56 ms |
11036 KB |
Output is correct |
66 |
Correct |
54 ms |
11016 KB |
Output is correct |
67 |
Correct |
62 ms |
10908 KB |
Output is correct |
68 |
Correct |
65 ms |
10884 KB |
Output is correct |
69 |
Correct |
59 ms |
10996 KB |
Output is correct |
70 |
Correct |
64 ms |
10956 KB |
Output is correct |
71 |
Correct |
1 ms |
2644 KB |
Output is correct |
72 |
Correct |
3 ms |
2900 KB |
Output is correct |
73 |
Correct |
3 ms |
2900 KB |
Output is correct |
74 |
Correct |
3 ms |
2924 KB |
Output is correct |
75 |
Correct |
3 ms |
2900 KB |
Output is correct |
76 |
Correct |
3 ms |
2884 KB |
Output is correct |
77 |
Correct |
3 ms |
2932 KB |
Output is correct |
78 |
Correct |
3 ms |
2900 KB |
Output is correct |
79 |
Correct |
3 ms |
2900 KB |
Output is correct |
80 |
Correct |
63 ms |
10956 KB |
Output is correct |
81 |
Correct |
58 ms |
10952 KB |
Output is correct |
82 |
Correct |
55 ms |
11016 KB |
Output is correct |
83 |
Correct |
54 ms |
11036 KB |
Output is correct |
84 |
Correct |
2 ms |
2800 KB |
Output is correct |
85 |
Correct |
3 ms |
2772 KB |
Output is correct |
86 |
Correct |
3 ms |
2772 KB |
Output is correct |
87 |
Correct |
3 ms |
2772 KB |
Output is correct |
88 |
Correct |
3 ms |
2800 KB |
Output is correct |
89 |
Correct |
2 ms |
2832 KB |
Output is correct |
90 |
Correct |
3 ms |
2772 KB |
Output is correct |
91 |
Correct |
3 ms |
2900 KB |
Output is correct |
92 |
Correct |
3 ms |
2884 KB |
Output is correct |
93 |
Correct |
3 ms |
2804 KB |
Output is correct |
94 |
Correct |
2 ms |
2644 KB |
Output is correct |
95 |
Correct |
2 ms |
2588 KB |
Output is correct |
96 |
Correct |
2 ms |
2644 KB |
Output is correct |
97 |
Correct |
1 ms |
2576 KB |
Output is correct |
98 |
Correct |
2 ms |
2644 KB |
Output is correct |
99 |
Correct |
1 ms |
2656 KB |
Output is correct |
100 |
Correct |
2 ms |
2660 KB |
Output is correct |
101 |
Correct |
1 ms |
2644 KB |
Output is correct |
102 |
Correct |
2 ms |
2644 KB |
Output is correct |
103 |
Correct |
2 ms |
2644 KB |
Output is correct |
104 |
Correct |
6 ms |
3688 KB |
Output is correct |
105 |
Correct |
12 ms |
5468 KB |
Output is correct |
106 |
Correct |
2 ms |
2644 KB |
Output is correct |
107 |
Correct |
22 ms |
7108 KB |
Output is correct |
108 |
Correct |
31 ms |
8908 KB |
Output is correct |
109 |
Correct |
2 ms |
2644 KB |
Output is correct |
110 |
Correct |
41 ms |
10904 KB |
Output is correct |
111 |
Correct |
47 ms |
12940 KB |
Output is correct |
112 |
Correct |
2 ms |
2772 KB |
Output is correct |
113 |
Correct |
77 ms |
16544 KB |
Output is correct |
114 |
Incorrect |
19 ms |
6904 KB |
Output isn't correct |
115 |
Halted |
0 ms |
0 KB |
- |