#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m;
vector<pair<int, int>> g[maxn];
long long dp[maxn];
bool visited[maxn];
void dfs_down(int node, int par)
{
visited[node] = true;
dp[node] = 0;
for (int i = 0; i < g[node].size(); ++i)
{
if (g[node][i].first != par)
{
dfs_down(g[node][i].first, node);
dp[node] = max(dp[node], g[node][i].second + dp[g[node][i].first]);
}
}
}
long long dfs_up(int node, int par, long long up)
{
long long result = max(up, dp[node]);
long long max1 = 0;
long long max2 = 0;
for (int i = 0; i < g[node].size(); ++i)
{
int child = g[node][i].first;
int edge = g[node][i].second;
if (child == par)
{
continue;
}
if (dp[child] + edge > max1)
{
max2 = max1;
max1 = dp[child] + edge;
}
else if (dp[child] + edge > max2)
{
max2 = dp[child] + edge;
}
}
for (int i = 0; i < g[node].size(); ++i)
{
if (g[node][i].first == par)
{
continue;
}
int child = g[node][i].first;
int edge = g[node][i].second;
long long new_up = 0;
if (dp[child] + edge == max1)
{
new_up = max(up, max2) + edge;
}
else
{
new_up = max(up, max1) + edge;
}
result = min(result, dfs_up(child, node, new_up));
}
return result;
}
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)
{
int x = A[i] + 1;
int y = B[i] + 1;
int w = T[i];
g[x].push_back(make_pair(y, w));
g[y].push_back(make_pair(x, w));
}
vector<long long> dists;
for (int i = 1; i <= n; ++i)
{
if (visited[i] == false)
{
dfs_down(i, -1);
dists.push_back(dfs_up(i, -1, 0));
}
}
sort(dists.begin(), dists.end());
if (dists.size() == 1)
{
return dists[dists.size() - 1];
}
long long ans = dists[dists.size() - 1] + dists[dists.size() - 2] + L;
if (dists.size() >= 3)
{
ans = max(ans, dists[dists.size() - 2] + dists[2] + 2 * L);
}
return ans;
}
/*int main()
{
int A[8] = {0, 8, 2, 5, 5, 1, 1, 10};
int B[8] = {8, 2, 7, 11, 1, 3, 9, 6};
int T[8] = {4, 2, 4, 3, 7, 1, 5, 3};
std::cout << travelTime(12, 8, 2, A, B, T) << std::endl;
return 0;
}*/
Compilation message
dreaming.cpp: In function 'void dfs_down(int, int)':
dreaming.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'long long int dfs_up(int, int, long long int)':
dreaming.cpp:37:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
11988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
11988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
6364 KB |
Output is correct |
2 |
Incorrect |
14 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
11988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |