#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);
ans = max(ans, f[u]);
}
}
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:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
dreaming.cpp:30: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 |
91 ms |
16888 KB |
Output is correct |
2 |
Correct |
173 ms |
16504 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
15 ms |
6784 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
21 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
40 ms |
10036 KB |
Output is correct |
9 |
Correct |
61 ms |
12408 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
94 ms |
13688 KB |
Output is correct |
12 |
Correct |
105 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
16888 KB |
Output is correct |
2 |
Correct |
173 ms |
16504 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
15 ms |
6784 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
21 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
40 ms |
10036 KB |
Output is correct |
9 |
Correct |
61 ms |
12408 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
94 ms |
13688 KB |
Output is correct |
12 |
Correct |
105 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 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 |
91 ms |
16888 KB |
Output is correct |
2 |
Correct |
173 ms |
16504 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
15 ms |
6784 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
21 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
40 ms |
10036 KB |
Output is correct |
9 |
Correct |
61 ms |
12408 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
94 ms |
13688 KB |
Output is correct |
12 |
Correct |
105 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 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 |
50 ms |
10732 KB |
Output is correct |
2 |
Correct |
48 ms |
10868 KB |
Output is correct |
3 |
Correct |
51 ms |
10744 KB |
Output is correct |
4 |
Correct |
45 ms |
10744 KB |
Output is correct |
5 |
Correct |
49 ms |
10744 KB |
Output is correct |
6 |
Correct |
57 ms |
11344 KB |
Output is correct |
7 |
Correct |
59 ms |
11124 KB |
Output is correct |
8 |
Correct |
51 ms |
10740 KB |
Output is correct |
9 |
Correct |
42 ms |
10584 KB |
Output is correct |
10 |
Correct |
48 ms |
11000 KB |
Output is correct |
11 |
Incorrect |
6 ms |
4992 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
16888 KB |
Output is correct |
2 |
Correct |
173 ms |
16504 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
15 ms |
6784 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
21 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
40 ms |
10036 KB |
Output is correct |
9 |
Correct |
61 ms |
12408 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
94 ms |
13688 KB |
Output is correct |
12 |
Correct |
105 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 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 |
5376 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 |
91 ms |
16888 KB |
Output is correct |
2 |
Correct |
173 ms |
16504 KB |
Output is correct |
3 |
Correct |
62 ms |
14200 KB |
Output is correct |
4 |
Correct |
15 ms |
6784 KB |
Output is correct |
5 |
Correct |
13 ms |
6144 KB |
Output is correct |
6 |
Correct |
21 ms |
7672 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
40 ms |
10036 KB |
Output is correct |
9 |
Correct |
61 ms |
12408 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
94 ms |
13688 KB |
Output is correct |
12 |
Correct |
105 ms |
15224 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 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 |
- |