//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];
int dist3[mxN], dist4[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> vv;
int ans = 0;
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;
}
}
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];
}
}
ans = max(ans, mx);
vv.push_back(max(krc, mx - krc));
}
}
sort(rall(vv));
if (vv.size() == 1) {
ans = max(ans, vv[0]);
} else if (vv.size() == 2) {
ans = max(ans, vv[0] + vv[1] + L);
}
ans = max(ans, max(vv[0] + vv[1] + L, vv[1] + vv[2] + 2 * 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:76:36: warning: variable 'cen' set but not used [-Wunused-but-set-variable]
76 | int diff = (int)(2e9), cen = 0, krc = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15500 KB |
Output is correct |
2 |
Correct |
30 ms |
15568 KB |
Output is correct |
3 |
Correct |
26 ms |
12100 KB |
Output is correct |
4 |
Correct |
9 ms |
7256 KB |
Output is correct |
5 |
Correct |
4 ms |
6492 KB |
Output is correct |
6 |
Correct |
10 ms |
8024 KB |
Output is correct |
7 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15500 KB |
Output is correct |
2 |
Correct |
30 ms |
15568 KB |
Output is correct |
3 |
Correct |
26 ms |
12100 KB |
Output is correct |
4 |
Correct |
9 ms |
7256 KB |
Output is correct |
5 |
Correct |
4 ms |
6492 KB |
Output is correct |
6 |
Correct |
10 ms |
8024 KB |
Output is correct |
7 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8280 KB |
Output is correct |
2 |
Correct |
18 ms |
8300 KB |
Output is correct |
3 |
Correct |
14 ms |
8232 KB |
Output is correct |
4 |
Correct |
15 ms |
8284 KB |
Output is correct |
5 |
Correct |
13 ms |
8216 KB |
Output is correct |
6 |
Correct |
18 ms |
8604 KB |
Output is correct |
7 |
Correct |
19 ms |
8368 KB |
Output is correct |
8 |
Correct |
16 ms |
8244 KB |
Output is correct |
9 |
Correct |
17 ms |
8152 KB |
Output is correct |
10 |
Correct |
14 ms |
8360 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
4 ms |
6412 KB |
Output is correct |
13 |
Correct |
4 ms |
6360 KB |
Output is correct |
14 |
Correct |
5 ms |
6360 KB |
Output is correct |
15 |
Correct |
4 ms |
6360 KB |
Output is correct |
16 |
Correct |
4 ms |
6360 KB |
Output is correct |
17 |
Correct |
4 ms |
6360 KB |
Output is correct |
18 |
Correct |
4 ms |
6360 KB |
Output is correct |
19 |
Correct |
5 ms |
6356 KB |
Output is correct |
20 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15500 KB |
Output is correct |
2 |
Correct |
30 ms |
15568 KB |
Output is correct |
3 |
Correct |
26 ms |
12100 KB |
Output is correct |
4 |
Correct |
9 ms |
7256 KB |
Output is correct |
5 |
Correct |
4 ms |
6492 KB |
Output is correct |
6 |
Correct |
10 ms |
8024 KB |
Output is correct |
7 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |