//Dedicated to my love, ivaziva
#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
using pii = pair<int, int>;
using ll = int64_t;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define dbg(x) cerr << #x << ": " << x << '\n';
constexpr int mxN = 1e5 + 20;
vector<pii> g[mxN];
int dist1[mxN], dist2[mxN], par[mxN];
bool vis[mxN];
vector<int> vec, dijametar;
void dfs(int x, int p) {
vis[x] = true;
vec.push_back(x);
for (auto ptr: g[x]) {
if (ptr.first != p) {
dist1[ptr.first] = dist1[x] + ptr.second;
dfs(ptr.first, x);
}
}
}
void dfs2(int x, int p) {
par[x] = p;
for (auto ptr: g[x]) {
if (ptr.first != p) {
dist2[ptr.first] = dist2[x] + ptr.second;
dfs2(ptr.first, x);
}
}
}
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++) {
dist1[i] = 0, dist2[i] = 0;
vis[i] = false;
}
vector<int> diams, anss;
for (int i = 0; i < N; i++) {
if (!vis[i]) {
dijametar.clear();
vec.clear();
dfs(i, -1);
int mx = 0, root = 0;
for (int x: vec) {
if (mx < dist1[x]) {
root = x;
mx = dist1[x];
}
}
dfs2(root, -1);
int diamend = 0;
mx = -1;
for (int x: vec) {
if (mx < dist2[x]) {
mx = dist2[x];
diamend = x;
}
}
diams.push_back(mx);
while (diamend != -1) {
dijametar.push_back(diamend);
diamend = par[diamend];
}
int diff = (int)(2e9), cen = 0, krc = 0;
for (int x: dijametar) {
if (diff > abs(mx - 2 * dist2[x])) {
diff = abs(mx - 2 * dist2[x]);
cen = x;
krc = dist2[x];
}
}
anss.push_back(max(krc, mx - krc));
}
}
int ans = max(diams[0], diams[1]);
ans = max(ans, anss[0] + anss[1] + L);
return ans;
}
/*int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cout.tie(nullptr); cerr.tie(nullptr);
int n, m, l;
cin >> n >> m >> l;
int a[m], b[m], t[m];
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> t[i];
}
int ans = travelTime(n, m, l, a, b, t);
cout << ans << '\n';
return 0;
}*/
/*
12 8 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:75:36: warning: variable 'cen' set but not used [-Wunused-but-set-variable]
75 | int diff = (int)(2e9), cen = 0, krc = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
14896 KB |
Output is correct |
2 |
Correct |
30 ms |
14820 KB |
Output is correct |
3 |
Correct |
22 ms |
11432 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
5568 KB |
Output is correct |
6 |
Correct |
8 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
16 ms |
8140 KB |
Output is correct |
9 |
Correct |
23 ms |
9664 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
28 ms |
10900 KB |
Output is correct |
12 |
Correct |
33 ms |
12840 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
5056 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
1 ms |
4956 KB |
Output is correct |
24 |
Correct |
1 ms |
4956 KB |
Output is correct |
25 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
14896 KB |
Output is correct |
2 |
Correct |
30 ms |
14820 KB |
Output is correct |
3 |
Correct |
22 ms |
11432 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
5568 KB |
Output is correct |
6 |
Correct |
8 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
16 ms |
8140 KB |
Output is correct |
9 |
Correct |
23 ms |
9664 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
28 ms |
10900 KB |
Output is correct |
12 |
Correct |
33 ms |
12840 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
1 ms |
4956 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
1 ms |
4956 KB |
Output is correct |
27 |
Correct |
1 ms |
4956 KB |
Output is correct |
28 |
Correct |
1 ms |
4956 KB |
Output is correct |
29 |
Correct |
1 ms |
4956 KB |
Output is correct |
30 |
Correct |
1 ms |
4956 KB |
Output is correct |
31 |
Correct |
1 ms |
4956 KB |
Output is correct |
32 |
Correct |
1 ms |
4956 KB |
Output is correct |
33 |
Correct |
1 ms |
4956 KB |
Output is correct |
34 |
Correct |
1 ms |
5056 KB |
Output is correct |
35 |
Correct |
1 ms |
4956 KB |
Output is correct |
36 |
Correct |
1 ms |
4956 KB |
Output is correct |
37 |
Correct |
1 ms |
4956 KB |
Output is correct |
38 |
Correct |
1 ms |
4956 KB |
Output is correct |
39 |
Correct |
30 ms |
16008 KB |
Output is correct |
40 |
Correct |
34 ms |
16060 KB |
Output is correct |
41 |
Correct |
23 ms |
12428 KB |
Output is correct |
42 |
Correct |
6 ms |
6756 KB |
Output is correct |
43 |
Correct |
1 ms |
5068 KB |
Output is correct |
44 |
Correct |
1 ms |
5220 KB |
Output is correct |
45 |
Correct |
1 ms |
5072 KB |
Output is correct |
46 |
Correct |
2 ms |
5072 KB |
Output is correct |
47 |
Correct |
1 ms |
4968 KB |
Output is correct |
48 |
Correct |
1 ms |
4968 KB |
Output is correct |
49 |
Correct |
1 ms |
4968 KB |
Output is correct |
50 |
Correct |
1 ms |
4968 KB |
Output is correct |
51 |
Correct |
1 ms |
4968 KB |
Output is correct |
52 |
Correct |
1 ms |
5072 KB |
Output is correct |
53 |
Correct |
1 ms |
5072 KB |
Output is correct |
54 |
Correct |
1 ms |
5068 KB |
Output is correct |
55 |
Correct |
1 ms |
4968 KB |
Output is correct |
56 |
Correct |
1 ms |
4968 KB |
Output is correct |
57 |
Correct |
45 ms |
10972 KB |
Output is correct |
58 |
Correct |
38 ms |
10872 KB |
Output is correct |
59 |
Correct |
43 ms |
10960 KB |
Output is correct |
60 |
Correct |
44 ms |
10836 KB |
Output is correct |
61 |
Correct |
37 ms |
11220 KB |
Output is correct |
62 |
Correct |
43 ms |
10864 KB |
Output is correct |
63 |
Correct |
33 ms |
10684 KB |
Output is correct |
64 |
Correct |
38 ms |
10712 KB |
Output is correct |
65 |
Correct |
50 ms |
11028 KB |
Output is correct |
66 |
Correct |
40 ms |
11056 KB |
Output is correct |
67 |
Correct |
46 ms |
11096 KB |
Output is correct |
68 |
Correct |
47 ms |
11080 KB |
Output is correct |
69 |
Correct |
54 ms |
11132 KB |
Output is correct |
70 |
Correct |
40 ms |
11100 KB |
Output is correct |
71 |
Correct |
1 ms |
4956 KB |
Output is correct |
72 |
Correct |
2 ms |
5212 KB |
Output is correct |
73 |
Correct |
3 ms |
5068 KB |
Output is correct |
74 |
Correct |
2 ms |
5212 KB |
Output is correct |
75 |
Correct |
2 ms |
5212 KB |
Output is correct |
76 |
Correct |
2 ms |
5212 KB |
Output is correct |
77 |
Correct |
2 ms |
5212 KB |
Output is correct |
78 |
Correct |
2 ms |
5208 KB |
Output is correct |
79 |
Correct |
2 ms |
5212 KB |
Output is correct |
80 |
Correct |
45 ms |
11108 KB |
Output is correct |
81 |
Correct |
40 ms |
11068 KB |
Output is correct |
82 |
Correct |
39 ms |
11216 KB |
Output is correct |
83 |
Correct |
43 ms |
11216 KB |
Output is correct |
84 |
Correct |
2 ms |
4952 KB |
Output is correct |
85 |
Correct |
2 ms |
4956 KB |
Output is correct |
86 |
Correct |
2 ms |
4956 KB |
Output is correct |
87 |
Correct |
2 ms |
5212 KB |
Output is correct |
88 |
Correct |
2 ms |
5076 KB |
Output is correct |
89 |
Correct |
2 ms |
4956 KB |
Output is correct |
90 |
Correct |
2 ms |
5212 KB |
Output is correct |
91 |
Correct |
2 ms |
5208 KB |
Output is correct |
92 |
Correct |
2 ms |
5212 KB |
Output is correct |
93 |
Correct |
2 ms |
5212 KB |
Output is correct |
94 |
Correct |
2 ms |
4956 KB |
Output is correct |
95 |
Correct |
1 ms |
5064 KB |
Output is correct |
96 |
Correct |
2 ms |
4956 KB |
Output is correct |
97 |
Correct |
1 ms |
5056 KB |
Output is correct |
98 |
Correct |
1 ms |
4956 KB |
Output is correct |
99 |
Correct |
1 ms |
4956 KB |
Output is correct |
100 |
Correct |
1 ms |
4956 KB |
Output is correct |
101 |
Correct |
1 ms |
4956 KB |
Output is correct |
102 |
Correct |
1 ms |
4956 KB |
Output is correct |
103 |
Correct |
1 ms |
4956 KB |
Output is correct |
104 |
Correct |
4 ms |
5724 KB |
Output is correct |
105 |
Correct |
8 ms |
7464 KB |
Output is correct |
106 |
Correct |
1 ms |
4956 KB |
Output is correct |
107 |
Correct |
24 ms |
8708 KB |
Output is correct |
108 |
Correct |
32 ms |
10448 KB |
Output is correct |
109 |
Correct |
1 ms |
4952 KB |
Output is correct |
110 |
Correct |
33 ms |
12036 KB |
Output is correct |
111 |
Correct |
32 ms |
14032 KB |
Output is correct |
112 |
Correct |
1 ms |
4956 KB |
Output is correct |
113 |
Correct |
47 ms |
17240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
7640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
5056 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
1 ms |
4956 KB |
Output is correct |
24 |
Correct |
1 ms |
4956 KB |
Output is correct |
25 |
Correct |
1 ms |
4956 KB |
Output is correct |
26 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
14896 KB |
Output is correct |
2 |
Correct |
30 ms |
14820 KB |
Output is correct |
3 |
Correct |
22 ms |
11432 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
5568 KB |
Output is correct |
6 |
Correct |
8 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
16 ms |
8140 KB |
Output is correct |
9 |
Correct |
23 ms |
9664 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
28 ms |
10900 KB |
Output is correct |
12 |
Correct |
33 ms |
12840 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
1 ms |
4956 KB |
Output is correct |
24 |
Correct |
1 ms |
5212 KB |
Output is correct |
25 |
Correct |
1 ms |
5212 KB |
Output is correct |
26 |
Correct |
1 ms |
4956 KB |
Output is correct |
27 |
Correct |
1 ms |
4956 KB |
Output is correct |
28 |
Correct |
1 ms |
4956 KB |
Output is correct |
29 |
Correct |
1 ms |
4956 KB |
Output is correct |
30 |
Correct |
1 ms |
4956 KB |
Output is correct |
31 |
Correct |
1 ms |
4956 KB |
Output is correct |
32 |
Correct |
1 ms |
4956 KB |
Output is correct |
33 |
Correct |
1 ms |
4956 KB |
Output is correct |
34 |
Correct |
1 ms |
5056 KB |
Output is correct |
35 |
Correct |
1 ms |
4956 KB |
Output is correct |
36 |
Correct |
1 ms |
4956 KB |
Output is correct |
37 |
Correct |
1 ms |
4956 KB |
Output is correct |
38 |
Correct |
1 ms |
4956 KB |
Output is correct |
39 |
Correct |
30 ms |
16008 KB |
Output is correct |
40 |
Correct |
34 ms |
16060 KB |
Output is correct |
41 |
Correct |
23 ms |
12428 KB |
Output is correct |
42 |
Correct |
6 ms |
6756 KB |
Output is correct |
43 |
Correct |
1 ms |
5068 KB |
Output is correct |
44 |
Correct |
1 ms |
5220 KB |
Output is correct |
45 |
Correct |
1 ms |
5072 KB |
Output is correct |
46 |
Correct |
2 ms |
5072 KB |
Output is correct |
47 |
Correct |
1 ms |
4968 KB |
Output is correct |
48 |
Correct |
1 ms |
4968 KB |
Output is correct |
49 |
Correct |
1 ms |
4968 KB |
Output is correct |
50 |
Correct |
1 ms |
4968 KB |
Output is correct |
51 |
Correct |
1 ms |
4968 KB |
Output is correct |
52 |
Correct |
1 ms |
5072 KB |
Output is correct |
53 |
Correct |
1 ms |
5072 KB |
Output is correct |
54 |
Correct |
1 ms |
5068 KB |
Output is correct |
55 |
Correct |
1 ms |
4968 KB |
Output is correct |
56 |
Correct |
1 ms |
4968 KB |
Output is correct |
57 |
Correct |
45 ms |
10972 KB |
Output is correct |
58 |
Correct |
38 ms |
10872 KB |
Output is correct |
59 |
Correct |
43 ms |
10960 KB |
Output is correct |
60 |
Correct |
44 ms |
10836 KB |
Output is correct |
61 |
Correct |
37 ms |
11220 KB |
Output is correct |
62 |
Correct |
43 ms |
10864 KB |
Output is correct |
63 |
Correct |
33 ms |
10684 KB |
Output is correct |
64 |
Correct |
38 ms |
10712 KB |
Output is correct |
65 |
Correct |
50 ms |
11028 KB |
Output is correct |
66 |
Correct |
40 ms |
11056 KB |
Output is correct |
67 |
Correct |
46 ms |
11096 KB |
Output is correct |
68 |
Correct |
47 ms |
11080 KB |
Output is correct |
69 |
Correct |
54 ms |
11132 KB |
Output is correct |
70 |
Correct |
40 ms |
11100 KB |
Output is correct |
71 |
Correct |
1 ms |
4956 KB |
Output is correct |
72 |
Correct |
2 ms |
5212 KB |
Output is correct |
73 |
Correct |
3 ms |
5068 KB |
Output is correct |
74 |
Correct |
2 ms |
5212 KB |
Output is correct |
75 |
Correct |
2 ms |
5212 KB |
Output is correct |
76 |
Correct |
2 ms |
5212 KB |
Output is correct |
77 |
Correct |
2 ms |
5212 KB |
Output is correct |
78 |
Correct |
2 ms |
5208 KB |
Output is correct |
79 |
Correct |
2 ms |
5212 KB |
Output is correct |
80 |
Correct |
45 ms |
11108 KB |
Output is correct |
81 |
Correct |
40 ms |
11068 KB |
Output is correct |
82 |
Correct |
39 ms |
11216 KB |
Output is correct |
83 |
Correct |
43 ms |
11216 KB |
Output is correct |
84 |
Correct |
2 ms |
4952 KB |
Output is correct |
85 |
Correct |
2 ms |
4956 KB |
Output is correct |
86 |
Correct |
2 ms |
4956 KB |
Output is correct |
87 |
Correct |
2 ms |
5212 KB |
Output is correct |
88 |
Correct |
2 ms |
5076 KB |
Output is correct |
89 |
Correct |
2 ms |
4956 KB |
Output is correct |
90 |
Correct |
2 ms |
5212 KB |
Output is correct |
91 |
Correct |
2 ms |
5208 KB |
Output is correct |
92 |
Correct |
2 ms |
5212 KB |
Output is correct |
93 |
Correct |
2 ms |
5212 KB |
Output is correct |
94 |
Correct |
2 ms |
4956 KB |
Output is correct |
95 |
Correct |
1 ms |
5064 KB |
Output is correct |
96 |
Correct |
2 ms |
4956 KB |
Output is correct |
97 |
Correct |
1 ms |
5056 KB |
Output is correct |
98 |
Correct |
1 ms |
4956 KB |
Output is correct |
99 |
Correct |
1 ms |
4956 KB |
Output is correct |
100 |
Correct |
1 ms |
4956 KB |
Output is correct |
101 |
Correct |
1 ms |
4956 KB |
Output is correct |
102 |
Correct |
1 ms |
4956 KB |
Output is correct |
103 |
Correct |
1 ms |
4956 KB |
Output is correct |
104 |
Correct |
4 ms |
5724 KB |
Output is correct |
105 |
Correct |
8 ms |
7464 KB |
Output is correct |
106 |
Correct |
1 ms |
4956 KB |
Output is correct |
107 |
Correct |
24 ms |
8708 KB |
Output is correct |
108 |
Correct |
32 ms |
10448 KB |
Output is correct |
109 |
Correct |
1 ms |
4952 KB |
Output is correct |
110 |
Correct |
33 ms |
12036 KB |
Output is correct |
111 |
Correct |
32 ms |
14032 KB |
Output is correct |
112 |
Correct |
1 ms |
4956 KB |
Output is correct |
113 |
Correct |
47 ms |
17240 KB |
Output is correct |
114 |
Incorrect |
14 ms |
7640 KB |
Output isn't correct |
115 |
Halted |
0 ms |
0 KB |
- |