#include "dreaming.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define getbit(x, i) (((x) >> (i)) & 1)
#define all(x) x.begin(), x.end()
#define TASK "DREAMING"
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const long long inf = 1e18;
const int mod = 1e9 + 7;
const int N = 1e5 + 7;
long long dp[N][2], g[N], d[N], ans;
pair <long long, int> center_dist;
vector <pair <int, int>> a[N];
bool visited[N];
int n, m, l;
void dfs1(int u, int p) {
visited[u] = 1;
for (auto i : a[u]) {
if (i.fi == p) continue;
int v = i.fi, w = i.se;
dfs1(v, u);
if (dp[u][0] < dp[v][0] + w) {
dp[u][1] = dp[u][0];
dp[u][0] = dp[v][0] + w;
} else if (dp[v][0] + w > dp[u][1])
dp[u][1] = dp[v][0] + w;
}
maxi(ans, dp[u][0] + dp[u][1]);
}
void dfs2(int u, int p, long long w) {
if (p != u) g[u] = max(g[p] + w, (dp[u][0] + w == dp[p][0] ? (dp[p][1] + w) : dp[p][0]));
mini(center_dist, mp(max(g[u], dp[u][0]), u));
for (auto i : a[u]) {
if (i.fi == p) continue;
int v = i.fi, w2 = i.se;
dfs2(v, u, w2);
}
}
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], v = ++B[i], w = T[i];
a[u].push_back({v, w});
a[v].push_back({u, w});
}
vector <pair <int, int>> v;
for (int i = 1; i <= n; ++i) {
if (visited[i] == 1) continue;
dfs1(i, i);
center_dist = mp(1e9 + 9, 0);
dfs2(i, i, 0);
v.push_back(center_dist);
}
sort(all(v), greater <pair <int, int>>());
for (int i = 1; i < v.size(); ++i) {
a[v[0].se].emplace_back(v[i].se, l);
a[v[i].se].emplace_back(v[0].se, l);
}
memset(dp, 0, sizeof dp);
ans = 0;
dfs1(1, 1);
return ans;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 1; i < v.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
14072 KB |
Output is correct |
2 |
Correct |
48 ms |
13776 KB |
Output is correct |
3 |
Correct |
36 ms |
12296 KB |
Output is correct |
4 |
Correct |
8 ms |
5588 KB |
Output is correct |
5 |
Correct |
8 ms |
5104 KB |
Output is correct |
6 |
Correct |
13 ms |
6388 KB |
Output is correct |
7 |
Correct |
3 ms |
4200 KB |
Output is correct |
8 |
Correct |
24 ms |
8292 KB |
Output is correct |
9 |
Correct |
33 ms |
10552 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
73 ms |
11588 KB |
Output is correct |
12 |
Correct |
68 ms |
12688 KB |
Output is correct |
13 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4200 KB |
Output is correct |
3 |
Correct |
2 ms |
4200 KB |
Output is correct |
4 |
Correct |
3 ms |
4200 KB |
Output is correct |
5 |
Correct |
2 ms |
4192 KB |
Output is correct |
6 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
14072 KB |
Output is correct |
2 |
Correct |
48 ms |
13776 KB |
Output is correct |
3 |
Correct |
36 ms |
12296 KB |
Output is correct |
4 |
Correct |
8 ms |
5588 KB |
Output is correct |
5 |
Correct |
8 ms |
5104 KB |
Output is correct |
6 |
Correct |
13 ms |
6388 KB |
Output is correct |
7 |
Correct |
3 ms |
4200 KB |
Output is correct |
8 |
Correct |
24 ms |
8292 KB |
Output is correct |
9 |
Correct |
33 ms |
10552 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
73 ms |
11588 KB |
Output is correct |
12 |
Correct |
68 ms |
12688 KB |
Output is correct |
13 |
Correct |
3 ms |
4308 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4200 KB |
Output is correct |
16 |
Correct |
2 ms |
4200 KB |
Output is correct |
17 |
Correct |
3 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4192 KB |
Output is correct |
19 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
9640 KB |
Output is correct |
2 |
Correct |
44 ms |
9748 KB |
Output is correct |
3 |
Correct |
31 ms |
9728 KB |
Output is correct |
4 |
Correct |
36 ms |
9756 KB |
Output is correct |
5 |
Correct |
36 ms |
9728 KB |
Output is correct |
6 |
Correct |
34 ms |
10572 KB |
Output is correct |
7 |
Correct |
39 ms |
9992 KB |
Output is correct |
8 |
Correct |
39 ms |
9564 KB |
Output is correct |
9 |
Correct |
43 ms |
9592 KB |
Output is correct |
10 |
Correct |
35 ms |
9924 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
17 ms |
9696 KB |
Output is correct |
13 |
Correct |
15 ms |
9636 KB |
Output is correct |
14 |
Correct |
15 ms |
9460 KB |
Output is correct |
15 |
Correct |
16 ms |
9556 KB |
Output is correct |
16 |
Correct |
17 ms |
9416 KB |
Output is correct |
17 |
Correct |
15 ms |
9172 KB |
Output is correct |
18 |
Correct |
21 ms |
9696 KB |
Output is correct |
19 |
Correct |
17 ms |
9392 KB |
Output is correct |
20 |
Correct |
2 ms |
4200 KB |
Output is correct |
21 |
Correct |
2 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4328 KB |
Output is correct |
23 |
Correct |
15 ms |
9412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4200 KB |
Output is correct |
3 |
Correct |
2 ms |
4200 KB |
Output is correct |
4 |
Correct |
3 ms |
4200 KB |
Output is correct |
5 |
Correct |
2 ms |
4192 KB |
Output is correct |
6 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
14072 KB |
Output is correct |
2 |
Correct |
48 ms |
13776 KB |
Output is correct |
3 |
Correct |
36 ms |
12296 KB |
Output is correct |
4 |
Correct |
8 ms |
5588 KB |
Output is correct |
5 |
Correct |
8 ms |
5104 KB |
Output is correct |
6 |
Correct |
13 ms |
6388 KB |
Output is correct |
7 |
Correct |
3 ms |
4200 KB |
Output is correct |
8 |
Correct |
24 ms |
8292 KB |
Output is correct |
9 |
Correct |
33 ms |
10552 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
73 ms |
11588 KB |
Output is correct |
12 |
Correct |
68 ms |
12688 KB |
Output is correct |
13 |
Correct |
3 ms |
4308 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4200 KB |
Output is correct |
16 |
Correct |
2 ms |
4200 KB |
Output is correct |
17 |
Correct |
3 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4192 KB |
Output is correct |
19 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |