#include "dreaming.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 100000 + 10;
const int INF = 1e9;
int n, m;
int dp[MAXN];
int dp2[MAXN];
int next[MAXN];
bool vis[MAXN];
std::vector <std::pair <int,int>> g[MAXN];
std::vector <int> dists;
void dfsDown(int node, int p)
{
dp[node] = dp2[node] = 0;
for (const auto &[u, d] : g[node])
{
if (u == p)
{
continue;
}
dfsDown(u, node);
if (dp[u] + d > dp[node])
{
dp2[node] = dp[node];
dp[node] = dp[u] + d;
next[node] = u;
} else if (dp[u] + d > dp2[node])
{
dp2[node] = dp[u] + d;
}
}
}
int dfsUp(int node, int p, int edge)
{
vis[node] = true;
if (p != 0)
{
if (next[p] != node)
{
if (dp[p] + edge > dp[node])
{
dp2[node] = dp[node];
dp[node] = dp[p] + edge;
next[node] = p;
} else if (dp[p] + edge > dp2[node])
{
dp2[node] = dp[p] + edge;
}
} else
{
if (dp2[p] + edge > dp[node])
{
dp2[node] = dp[node];
dp[node] = dp2[p] + edge;
next[node] = p;
} else if (dp2[p] + edge > dp2[node])
{
dp2[node] = dp2[p] + edge;
}
}
}
int ans = dp[node];
for (const auto &[u, d] : g[node])
{
if (u != p)
{
ans = std::min(ans, dfsUp(u, node, d));
}
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
n = N;
m = M;
for (int i = 0 ; i < m ; ++i)
{
g[A[i] + 1].push_back({B[i] + 1, T[i]});
g[B[i] + 1].push_back({A[i] + 1, T[i]});
}
for (int i = 1 ; i <= n ; ++i)
{
if (vis[i])
{
continue;
}
dfsDown(i, 0);
dists.push_back(dfsUp(i, 0, 0));
}
std::sort(dists.begin(), dists.end());
if (dists.size() == 1) return dists[0];
int ans = dists[dists.size() - 2] + dists[dists.size() - 1] + L;
if (dists.size() >= 3) ans = std::max(ans, dists[dists.size() - 3] + dists[dists.size() - 2] + 2*L);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
11636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
11636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
6488 KB |
Output is correct |
2 |
Correct |
13 ms |
6524 KB |
Output is correct |
3 |
Correct |
14 ms |
6992 KB |
Output is correct |
4 |
Correct |
14 ms |
6944 KB |
Output is correct |
5 |
Correct |
21 ms |
6996 KB |
Output is correct |
6 |
Correct |
19 ms |
7488 KB |
Output is correct |
7 |
Correct |
20 ms |
7164 KB |
Output is correct |
8 |
Correct |
13 ms |
6892 KB |
Output is correct |
9 |
Correct |
13 ms |
6840 KB |
Output is correct |
10 |
Correct |
14 ms |
7052 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
4584 KB |
Output is correct |
13 |
Correct |
4 ms |
4560 KB |
Output is correct |
14 |
Correct |
4 ms |
4560 KB |
Output is correct |
15 |
Correct |
4 ms |
4560 KB |
Output is correct |
16 |
Correct |
4 ms |
4584 KB |
Output is correct |
17 |
Correct |
4 ms |
4176 KB |
Output is correct |
18 |
Correct |
4 ms |
4560 KB |
Output is correct |
19 |
Correct |
4 ms |
4560 KB |
Output is correct |
20 |
Correct |
1 ms |
2624 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
4560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
11636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |