#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;
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);
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) {
r2 = r1;
r1 = r;
} else if (r > r2) {
r2 = r;
}
}
}
return r1 + r2 + L;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
6136 KB |
Output is correct |
2 |
Incorrect |
23 ms |
6272 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |