#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 = 0, curr_max1_c = 0, curr_max2_c = 0, curr_max_node = 0,
curr_max1_edge = 0, curr_max2_edge = 0;
int r1 = 0, r2 = 0, r3 = 0;
void dfs(int node) {
visited[node] = true;
int max1 = 0, max2 = 0, max1_c = 0, max2_c = 0, 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 = 0, curr_max1_c = 0, curr_max2_c = 0, curr_max_node = 0,
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 == 0 ? 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) {
r3 = r2;
r2 = r1;
r1 = r;
} else if (r > r2) {
r3 = r2;
r2 = r;
} else if (r > r3) {
r3 = r;
}
}
}
return max(r1 + r2 + (r2 != 0) * L, r2 + r3 + 2 * (r2 != 0 && r3 != 0) * L);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6272 KB |
Output is correct |
2 |
Correct |
23 ms |
6272 KB |
Output is correct |
3 |
Correct |
23 ms |
6136 KB |
Output is correct |
4 |
Correct |
23 ms |
6188 KB |
Output is correct |
5 |
Correct |
25 ms |
6272 KB |
Output is correct |
6 |
Correct |
30 ms |
6392 KB |
Output is correct |
7 |
Correct |
28 ms |
6400 KB |
Output is correct |
8 |
Correct |
24 ms |
6136 KB |
Output is correct |
9 |
Correct |
24 ms |
6144 KB |
Output is correct |
10 |
Correct |
24 ms |
6400 KB |
Output is correct |
11 |
Correct |
4 ms |
2688 KB |
Output is correct |
12 |
Correct |
5 ms |
3968 KB |
Output is correct |
13 |
Correct |
5 ms |
3968 KB |
Output is correct |
14 |
Correct |
5 ms |
3968 KB |
Output is correct |
15 |
Correct |
6 ms |
3968 KB |
Output is correct |
16 |
Correct |
5 ms |
3968 KB |
Output is correct |
17 |
Correct |
5 ms |
3968 KB |
Output is correct |
18 |
Correct |
5 ms |
4096 KB |
Output is correct |
19 |
Correct |
5 ms |
3968 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
10616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |