#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
const int MaxN = 1e5 + 5;
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }
const int Inf = 0x3f3f3f3f;
int n, m, l;
vector<int> pref[MaxN], suff[MaxN];
vector<pair<int, int>> adj[MaxN];
int vis[MaxN];
int dep[MaxN], dp[MaxN];
void dfs(int u, int p) {
vis[u] = true;
for (auto [v, w]: adj[u]) if (v != p) {
dep[v] = dep[u] + w;
dfs(v, u);
maximize(dp[u], dp[v] + w);
}
//cout << "? " << u << " " << dp[u] << endl;
}
int ans;
void reroot(int u, int p) {
//cout << u << " " << dp[u] << endl;
minimize(ans, dp[u]);
for (int i = 0; i < sz(adj[u]); ++i) {
auto [v, w] = adj[u][i];
pref[u].emplace_back(dp[v] + w);
suff[u].emplace_back(dp[v] + w);
}
for (int i = 0; i < sz(adj[u]); ++i) {
auto [v, w] = adj[u][i];
if (i) maximize(pref[u][i], pref[u][i - 1]);
}
for (int i = sz(adj[u]) - 1; i >= 0; --i) {
auto [v, w] = adj[u][i];
if (i + 1 < sz(adj[u])) maximize(suff[u][i], suff[u][i + 1]);
}
for (int i = 0; i < sz(adj[u]); ++i) {
auto [v, w] = adj[u][i];
if (v == p) continue;
int old = dp[v];
int nw = max((i ? pref[u][i - 1] : 0), (i + 1 < sz(adj[u]) ? suff[u][i + 1] : 0));
dp[u] = nw;
dp[v] = max(dp[u] + w, old);
reroot(v, u);
dp[u] = pref[u].back();
dp[v] = old;
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
m = M;
l = L;
for (int i = 0; i < m; ++i) {
int u = A[i];
int v = B[i];
int w = T[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
vector<int> best;
for (int i = 0; i < n; ++i) if (!vis[i]) {
dfs(i, i);
ans = Inf;
reroot(i, i);
best.push_back(ans);
}
sort(best.begin(), best.end(), greater<int>());
return best[0] + best[1] + l;
}
Compilation message
dreaming.cpp: In function 'void reroot(int, int)':
dreaming.cpp:45:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
45 | auto [v, w] = adj[u][i];
| ^~~~~~
dreaming.cpp:50:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
50 | auto [v, w] = adj[u][i];
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
24932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
24932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
15832 KB |
Output is correct |
2 |
Incorrect |
30 ms |
15860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
24932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |