//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], ps[mxN], ss[mxN];
int current_root = 0, sz = 0;
bool vis[mxN];
vector<int> vec;
void dfs1(int x, int p) {
sz++;
vis[x] = true;
vec.push_back(x);
for (auto ptr: g[x]) {
if (ptr.first != p) {
dfs1(ptr.first, x);
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]});
}
for (int i = 0; i < N; i++) {
dp[i] = 0, dpp[i] = 0;
}
vector<int> diams, anss;
for (int i = 0; i < N; i++) {
if (!vis[i]) {
vec.clear();
sz = 0;
dfs1(i, -1);
if (sz == 1) {
continue;
}
ps[0] = dp[g[i][0].first] + g[i][0].second;
// if (i == 1) dbg(ps[0]);
for (int j = 1; j < g[i].size(); j++) {
ps[j] = max(ps[j - 1], dp[g[i][j].first] + g[i][j].second);
// if (i == 1) dbg(ps[j]);
}
ss[g[i].size() - 1] = dp[g[i].back().first] + g[i].back().second;
for (int j = g[i].size() - 2; j >= 0; j--) {
ss[j] = max(ss[j + 1], dp[g[i][j].first] + g[i][j].second);
}
for (int j = 0; j < g[i].size(); j++) {
dpp[g[i][j].first] = max((j >= 1 ? ps[j - 1] : 0), ss[j + 1]);
}
for (int j = 0; j <= g[i].size() + 5; j++) {
ps[j] = 0, ss[j] = 0;
}
current_root = i;
dfs2(i, -1);
int mn = (int)(2e9), di = 0;
for (int x: vec) {
// cerr << i << ' ' << 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);
}
}
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
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:64: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]
64 | for (int j = 1; j < g[i].size(); j++) {
| ~~^~~~~~~~~~~~~
dreaming.cpp:72: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]
72 | for (int j = 0; j < g[i].size(); j++) {
| ~~^~~~~~~~~~~~~
dreaming.cpp:75: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]
75 | for (int j = 0; j <= g[i].size() + 5; j++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
12840 KB |
Output is correct |
2 |
Correct |
26 ms |
12408 KB |
Output is correct |
3 |
Correct |
22 ms |
11732 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
5980 KB |
Output is correct |
6 |
Correct |
8 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
5468 KB |
Output is correct |
8 |
Correct |
14 ms |
8548 KB |
Output is correct |
9 |
Correct |
20 ms |
10460 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
27 ms |
10672 KB |
Output is correct |
12 |
Correct |
34 ms |
11620 KB |
Output is correct |
13 |
Correct |
1 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
5464 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
12840 KB |
Output is correct |
2 |
Correct |
26 ms |
12408 KB |
Output is correct |
3 |
Correct |
22 ms |
11732 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
5980 KB |
Output is correct |
6 |
Correct |
8 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
5468 KB |
Output is correct |
8 |
Correct |
14 ms |
8548 KB |
Output is correct |
9 |
Correct |
20 ms |
10460 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
27 ms |
10672 KB |
Output is correct |
12 |
Correct |
34 ms |
11620 KB |
Output is correct |
13 |
Correct |
1 ms |
5468 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Incorrect |
1 ms |
5464 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
7772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
5464 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
12840 KB |
Output is correct |
2 |
Correct |
26 ms |
12408 KB |
Output is correct |
3 |
Correct |
22 ms |
11732 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
5980 KB |
Output is correct |
6 |
Correct |
8 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
5468 KB |
Output is correct |
8 |
Correct |
14 ms |
8548 KB |
Output is correct |
9 |
Correct |
20 ms |
10460 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
27 ms |
10672 KB |
Output is correct |
12 |
Correct |
34 ms |
11620 KB |
Output is correct |
13 |
Correct |
1 ms |
5468 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Incorrect |
1 ms |
5464 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |