#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
const int N = 1e5 + 10;
vector<int> adj[N], cost[N], t;
int vis[N], ans, r, f[N], g[N];
void dfs(int u, int par) {
vis[u] = 1;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
dfs(v, u);
f[u] = max(f[u], f[v] + c);
}
}
void dfs2(int u, int par, int D) {
int m1 = D, m2 = -1e9;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
m2 = max(m2, f[v] + c);
if(m2 > m1) swap(m1, m2);
}
g[u] = m1;
ans = max(ans, g[u]);
r = min(r, g[u]);
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
dfs2(v, u, (f[v] + c == m1 ? m2 : m1) + c);
}
}
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], v = B[i], c = T[i];
adj[u].push_back(v);
adj[v].push_back(u);
cost[u].push_back(c);
cost[v].push_back(c);
}
vector<int> R;
for(int i = 0; i < N; i++) if(!vis[i]) {
r = N;
dfs(i, -1);
dfs2(i, -1, 0);
R.push_back(r);
}
sort(R.begin(), R.end(), greater<int>());
if(R.size() > 1) ans = max(ans, R[0] + R[1] + L);
if(R.size() > 2) ans = max(ans, R[1] + R[2] + 2*L);
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:11:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int, int)':
dreaming.cpp:20:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
dreaming.cpp:29:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
16912 KB |
Output is correct |
2 |
Correct |
101 ms |
16520 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
16 ms |
6784 KB |
Output is correct |
5 |
Correct |
14 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
42 ms |
9976 KB |
Output is correct |
9 |
Correct |
56 ms |
12468 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
89 ms |
13688 KB |
Output is correct |
12 |
Correct |
111 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
16912 KB |
Output is correct |
2 |
Correct |
101 ms |
16520 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
16 ms |
6784 KB |
Output is correct |
5 |
Correct |
14 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
42 ms |
9976 KB |
Output is correct |
9 |
Correct |
56 ms |
12468 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
89 ms |
13688 KB |
Output is correct |
12 |
Correct |
111 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5248 KB |
Output is correct |
14 |
Correct |
5 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
5120 KB |
Output is correct |
16 |
Correct |
6 ms |
4992 KB |
Output is correct |
17 |
Incorrect |
6 ms |
4992 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
16912 KB |
Output is correct |
2 |
Correct |
101 ms |
16520 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
16 ms |
6784 KB |
Output is correct |
5 |
Correct |
14 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
42 ms |
9976 KB |
Output is correct |
9 |
Correct |
56 ms |
12468 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
89 ms |
13688 KB |
Output is correct |
12 |
Correct |
111 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5248 KB |
Output is correct |
14 |
Correct |
5 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
5120 KB |
Output is correct |
16 |
Correct |
6 ms |
4992 KB |
Output is correct |
17 |
Incorrect |
6 ms |
4992 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
10708 KB |
Output is correct |
2 |
Correct |
50 ms |
10744 KB |
Output is correct |
3 |
Correct |
41 ms |
10744 KB |
Output is correct |
4 |
Correct |
54 ms |
10872 KB |
Output is correct |
5 |
Correct |
51 ms |
10744 KB |
Output is correct |
6 |
Correct |
50 ms |
11380 KB |
Output is correct |
7 |
Correct |
51 ms |
10996 KB |
Output is correct |
8 |
Correct |
49 ms |
10612 KB |
Output is correct |
9 |
Correct |
33 ms |
10616 KB |
Output is correct |
10 |
Correct |
43 ms |
11000 KB |
Output is correct |
11 |
Incorrect |
5 ms |
5120 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
16912 KB |
Output is correct |
2 |
Correct |
101 ms |
16520 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
16 ms |
6784 KB |
Output is correct |
5 |
Correct |
14 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
42 ms |
9976 KB |
Output is correct |
9 |
Correct |
56 ms |
12468 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
89 ms |
13688 KB |
Output is correct |
12 |
Correct |
111 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5248 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5248 KB |
Output is correct |
16 |
Correct |
7 ms |
5248 KB |
Output is correct |
17 |
Incorrect |
6 ms |
5120 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
16912 KB |
Output is correct |
2 |
Correct |
101 ms |
16520 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
16 ms |
6784 KB |
Output is correct |
5 |
Correct |
14 ms |
6144 KB |
Output is correct |
6 |
Correct |
22 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
42 ms |
9976 KB |
Output is correct |
9 |
Correct |
56 ms |
12468 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
89 ms |
13688 KB |
Output is correct |
12 |
Correct |
111 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5248 KB |
Output is correct |
14 |
Correct |
5 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
5120 KB |
Output is correct |
16 |
Correct |
6 ms |
4992 KB |
Output is correct |
17 |
Incorrect |
6 ms |
4992 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |