#include "bits/stdc++.h"
#include "dreaming.h"
using namespace std;
const int sz = 1e5 + 5;
vector <vector <int>> aj[sz];
int f[sz], s[sz], c[sz];
bool us[sz];
int mn, cn;
void dfs1(int v, int p) {
us[v] = true;
for (auto& u : aj[v]) {
if (u[0] == p) continue;
dfs1(u[0], v);
if (f[u[0]] + u[1] > f[v]) {
s[v] = f[v];
f[v] = f[u[0]] + u[1];
c[v] = u[0];
} else {
s[v] = max(s[v], f[u[0]] + u[1]);
}
}
}
void dfs2(int v, int p) {
if (f[v] < mn) {
mn = f[v];
cn = v;
}
for (auto& u : aj[v]) {
if (u[0] == p) continue;
if (c[v] == u[0]) {
if (s[v] + u[1] > f[u[0]]) {
s[u[0]] = f[u[0]];
f[u[0]] = s[v] + u[1];
c[u[0]] = v;
} else {
s[u[0]] = max(s[u[0]], s[v] + u[1]);
}
} else {
s[u[0]] = f[u[0]];
f[u[0]] = f[v] + u[1];
c[u[0]] = v;
}
dfs2(u[0], v);
}
}
void dfs3(int v, int p) {
f[v] = s[v] = -1;
for (auto& u : aj[v]) {
if (u[0] == p) continue;
dfs3(u[0], v);
if (f[u[0]] + u[1] > f[v]) {
s[v] = f[v];
f[v] = f[u[0]] + u[1];
} else {
s[v] = max(s[v], f[u[0]] + u[1]);
}
}
s[v] = f[v] + s[v];
mn = max({mn, f[v], s[v]});
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i ++) {
aj[A[i]].push_back({B[i], T[i]});
aj[B[i]].push_back({A[i], T[i]});
}
vector <vector <int>> rs;
for (int i = 0; i < N; i ++) {
if (!us[i]) {
mn = INT_MAX;
dfs1(i, 0);
dfs2(i, 0);
rs.push_back({mn, cn});
}
}
int mx = rs.back()[1];
for (int i = 0; i < (int) rs.size() - 1; i ++) {
int cur = rs[i][1];
aj[mx].push_back({cur, L});
aj[cur].push_back({mx, L});
}
mn = 0;
dfs3(0, -1);
return mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
20844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
20844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
19972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
20844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |