//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 dp[mxN], dpp[mxN], root[mxN], ps[mxN], ss[mxN], sum = 0;
int current_root = 0;
bool vis[mxN];
vector<int> vec;
void dfs1(int x, int p, int we) {
vis[x] = true;
vec.push_back(x);
for (auto ptr: g[x]) {
if (ptr.first != p) {
dfs1(ptr.first, x, ptr.second);
dp[x] = max(dp[x], dp[ptr.first] + ptr.second);
}
}
}
void dfs2(int x, int p) {
for (auto ptr: g[x]) {
if (ptr.first != p) {
if (x == current_root) {
dpp[ptr.first] += ptr.second;
} else {
dpp[ptr.first] = dpp[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]});
}
vector<int> diams, anss;
for (int i = 0; i < N; i++) {
if (!vis[i]) {
sum = 0;
vec.clear();
dfs1(i, -1, 0);
for (int j = 1; j <= g[i].size(); j++) {
ps[j] = max(ps[j - 1], dp[g[i][j - 1].first] + g[i][j - 1].second);
// if (i == 1) {
// dbg(ps[j]);
// }
}
for (int j = g[i].size(); j >= 1; j--) {
ss[j] = max(ss[j + 1], dp[g[i][j - 1].first] + g[i][j - 1].second);
// if (i == 1) {
// dbg(ss[j]);
// }
}
for (int j = 1; j <= g[i].size(); j++) {
dpp[g[i][j - 1].first] = max(ps[j - 1], ss[j + 1]);
}
for (int j = 0; j <= g[i].size() + 1; j++) {
ps[j] = 0, ss[j] = 0;
}
current_root = i;
dfs2(i, -1);
int mn = (int)(2e9), di = 0;
for (int x: vec) {
// if (i == 1) {
// cerr << "zz " << x << ' ' << dp[x] << ' ' << dpp[x] << '\n';
// }
int curr = max(dpp[x], dp[x]);
di = max(di, dpp[x] + dp[x]);
if (mn > curr) {
mn = curr;
}
}
diams.push_back(di);
anss.push_back(mn);
// dbg(root);
}
}
int ans = max(diams[0], diams[1]);
// cerr << diams[0] << ' ' << diams[1] << '\n';
// cerr << anss[0] << ' ' << anss[1] << '\n';
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
12 7 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
*/
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:55:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int j = 1; j <= g[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
dreaming.cpp:67:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int j = 1; j <= g[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
dreaming.cpp:70:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int j = 0; j <= g[i].size() + 1; j++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
14560 KB |
Output is correct |
2 |
Correct |
28 ms |
14184 KB |
Output is correct |
3 |
Correct |
20 ms |
13016 KB |
Output is correct |
4 |
Correct |
6 ms |
7004 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
9 ms |
7772 KB |
Output is correct |
7 |
Correct |
1 ms |
5724 KB |
Output is correct |
8 |
Correct |
20 ms |
9272 KB |
Output is correct |
9 |
Correct |
23 ms |
11484 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
29 ms |
12200 KB |
Output is correct |
12 |
Correct |
40 ms |
13268 KB |
Output is correct |
13 |
Correct |
2 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
2 ms |
5724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
14560 KB |
Output is correct |
2 |
Correct |
28 ms |
14184 KB |
Output is correct |
3 |
Correct |
20 ms |
13016 KB |
Output is correct |
4 |
Correct |
6 ms |
7004 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
9 ms |
7772 KB |
Output is correct |
7 |
Correct |
1 ms |
5724 KB |
Output is correct |
8 |
Correct |
20 ms |
9272 KB |
Output is correct |
9 |
Correct |
23 ms |
11484 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
29 ms |
12200 KB |
Output is correct |
12 |
Correct |
40 ms |
13268 KB |
Output is correct |
13 |
Correct |
2 ms |
5724 KB |
Output is correct |
14 |
Correct |
2 ms |
5720 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Incorrect |
2 ms |
5724 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
8920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
2 ms |
5724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
14560 KB |
Output is correct |
2 |
Correct |
28 ms |
14184 KB |
Output is correct |
3 |
Correct |
20 ms |
13016 KB |
Output is correct |
4 |
Correct |
6 ms |
7004 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
9 ms |
7772 KB |
Output is correct |
7 |
Correct |
1 ms |
5724 KB |
Output is correct |
8 |
Correct |
20 ms |
9272 KB |
Output is correct |
9 |
Correct |
23 ms |
11484 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
29 ms |
12200 KB |
Output is correct |
12 |
Correct |
40 ms |
13268 KB |
Output is correct |
13 |
Correct |
2 ms |
5724 KB |
Output is correct |
14 |
Correct |
2 ms |
5720 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Incorrect |
2 ms |
5724 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |