#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define MP make_pair
typedef pair<int, int> pii;
const int inf = 1e9 + 10;
const int mxn = 1e5 + 10;
vector<pii> adj[mxn];
int m1[mxn], m2[mxn];
void update(int &M1, int &M2, int sus) {
if(sus > M1) {
M2 = M1;
M1 = sus;
}
else if(sus > M2) {
M2 = sus;
}
}
void maxer(int &MAX, int CMP) { if(MAX < CMP) MAX = CMP; }
int max_dfs(int u, int par = -1) {
if(adj[u].size() == 1)
m1[u] = 0;
for(pii e : adj[u]) {
int v = e.ff;
int c = e.ss;
if(v == par) continue;
if(m1[v] > -1) continue;
int t = max_dfs(v, u) + c;
update(m1[u], m2[u], t);
}
// cout << u << ": " << m1[u] << ' ' << m2[u] << endl;
return m1[u];
}
int fix_dfs(int u, int prv = -1) {
int opt = -1;
int sus = m1[u];
int len = -1;
for(pii e : adj[u]) {
int v = e.ff;
int c = e.ss;
if(v == prv) continue;
if(m1[v] + c < m1[u]) continue;
int NEW = max(m1[v], m2[u] + c);
if(NEW < sus) {
sus = NEW;
opt = v;
len = c;
}
}
if(opt == -1) return u;
update(m1[opt], m2[opt], sus);
return fix_dfs(opt, u);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++) {
int u = A[i];
int v = B[i];
int c = T[i];
// cout << u << ' ' << v << ": " << c << endl;
adj[u].push_back(MP(v, c));
adj[v].push_back(MP(u, c));
}
for(int i = 0; i < N; i++) {
m1[i] = m2[i] = -1;
}
vector<int> upt;
for(int i = 0; i < N; i++) {
if(m1[i] > -1) continue;
// cout << "root = " << i << endl;
max_dfs(i);
// cout << "------\n";
upt.push_back(i);
}
int ans = 0;
vector<int> roots;
for(int x : upt) {
int y = fix_dfs(x);
roots.push_back(y);
// cout << x << ": " << y << endl;
maxer(ans, m1[y] + m2[y]);
}
if(roots.size() == 1)
return ans;
pii M1 = MP(-inf, -1);
pii M2 = MP(-inf, -1);
pii M3 = MP(-inf, -1);
for(int u : roots) {
pii cur = MP(m1[u], u);
if(cur > M1) {
M3 = M2;
M2 = M1;
M1 = cur;
}
else if(cur > M2) {
M3 = M2;
M2 = cur;
}
else if(cur > M3) {
M3 = cur;
}
}
maxer(ans, M1.ff + M2.ff + L);
maxer(ans, M2.ff + M3.ff + 2 * L);
return ans;
}
Compilation message
dreaming.cpp: In function 'int fix_dfs(int, int)':
dreaming.cpp:49:9: warning: variable 'len' set but not used [-Wunused-but-set-variable]
49 | int len = -1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
7688 KB |
Output is correct |
2 |
Correct |
12 ms |
7632 KB |
Output is correct |
3 |
Correct |
13 ms |
7664 KB |
Output is correct |
4 |
Correct |
13 ms |
7636 KB |
Output is correct |
5 |
Correct |
14 ms |
7632 KB |
Output is correct |
6 |
Correct |
15 ms |
8144 KB |
Output is correct |
7 |
Correct |
15 ms |
7760 KB |
Output is correct |
8 |
Correct |
14 ms |
7636 KB |
Output is correct |
9 |
Correct |
21 ms |
7604 KB |
Output is correct |
10 |
Correct |
14 ms |
7756 KB |
Output is correct |
11 |
Correct |
2 ms |
4432 KB |
Output is correct |
12 |
Correct |
3 ms |
5692 KB |
Output is correct |
13 |
Correct |
4 ms |
5576 KB |
Output is correct |
14 |
Correct |
5 ms |
5716 KB |
Output is correct |
15 |
Correct |
4 ms |
5680 KB |
Output is correct |
16 |
Correct |
4 ms |
5576 KB |
Output is correct |
17 |
Correct |
4 ms |
5576 KB |
Output is correct |
18 |
Correct |
5 ms |
5576 KB |
Output is correct |
19 |
Correct |
4 ms |
5576 KB |
Output is correct |
20 |
Correct |
2 ms |
4432 KB |
Output is correct |
21 |
Incorrect |
1 ms |
4600 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |