#include "dreaming.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
vector<pair<int, int>> graph[100001];
bool visited[100001];
struct Node {
int path, child, edge;
} dp[100001];
int curr_max = -1, curr_max1_c = -1, curr_max2_c = -1, curr_max_node = -1,
curr_max1_edge = 0, curr_max2_edge = 0;
int r1 = -1, r2 = -1, r3 = -1;
void dfs(int node) {
visited[node] = true;
int max1 = 0, max2 = 0, max1_c = -1, max2_c = -1, max1_edge = 0,
max2_edge = 0;
for (auto& i : graph[node]) {
if (visited[i.first]) continue;
dfs(i.first);
if (dp[i.first].path + i.second >= max1) {
max2 = max1;
max2_c = max1_c;
max2_edge = max1_edge;
max1 = dp[i.first].path + i.second;
max1_c = i.first;
max1_edge = i.second;
} else if (dp[i.first].path + i.second >= max2) {
max2 = dp[i.first].path + i.second;
max2_c = i.first;
max2_edge = i.second;
}
}
dp[node] = {max1, max1_c, max1_edge};
if (max1 + max2 > curr_max) {
curr_max = max1 + max2;
curr_max1_c = max1_c;
curr_max2_c = max2_c;
curr_max_node = node;
curr_max1_edge = max1_edge;
curr_max2_edge = max2_edge;
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
fill(visited, visited + N + 1, false);
FOR(i, 0, M) {
graph[A[i]].push_back({B[i], T[i]});
graph[B[i]].push_back({A[i], T[i]});
}
FOR(i, 0, N) {
if (!visited[i]) {
curr_max = -1, curr_max1_c = -1, curr_max2_c = -1,
curr_max_node = -1, curr_max1_edge = 0, curr_max2_edge = 0;
dfs(i);
int x = dp[curr_max1_c].path + curr_max1_edge;
int y = (curr_max2_c == -1 ? 0 : dp[curr_max2_c].path + curr_max2_edge);
// cout << curr_max << ' ' << curr_max1_c << ' ' << curr_max2_c
// << ' ' << curr_max_node << ' ' << curr_max1_edge << ' ' << curr_max2_edge << '\n';
while (x > y && y + dp[curr_max_node].edge < x) {
y += dp[curr_max_node].edge;
x -= dp[curr_max_node].edge;
curr_max_node = dp[curr_max_node].child;
}
int r = max(y, x);
if (r > r1) {
r3 = r2;
r2 = r1;
r1 = r;
} else if (r > r2) {
r3 = r2;
r2 = r;
} else if (r > r3) {
r3 = r;
}
}
}
// cout << r1 << ' ' << r2 << ' ' << r3 << '\n';
return max(r1 + r2 + (r2 != -1) * L,
r2 + r3 + 2 * (r2 != -1 && r3 != -1) * L);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
6144 KB |
Output is correct |
2 |
Correct |
29 ms |
6392 KB |
Output is correct |
3 |
Correct |
32 ms |
6272 KB |
Output is correct |
4 |
Correct |
22 ms |
6272 KB |
Output is correct |
5 |
Correct |
23 ms |
6272 KB |
Output is correct |
6 |
Correct |
27 ms |
6392 KB |
Output is correct |
7 |
Correct |
25 ms |
6400 KB |
Output is correct |
8 |
Correct |
23 ms |
6144 KB |
Output is correct |
9 |
Correct |
25 ms |
6144 KB |
Output is correct |
10 |
Correct |
27 ms |
6400 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
3968 KB |
Output is correct |
13 |
Correct |
6 ms |
3968 KB |
Output is correct |
14 |
Correct |
5 ms |
4096 KB |
Output is correct |
15 |
Correct |
6 ms |
3968 KB |
Output is correct |
16 |
Correct |
6 ms |
3968 KB |
Output is correct |
17 |
Correct |
5 ms |
3968 KB |
Output is correct |
18 |
Correct |
6 ms |
3968 KB |
Output is correct |
19 |
Correct |
6 ms |
3968 KB |
Output is correct |
20 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |