This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 longest = 0;
long long dfs_up(int node, int par, long long up)
{
long long result = max(up, dp[node]);
longest = max(longest, dp[node] + up);
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 max(longest, dists[0]);
}
long long ans = max(longest, dists[dists.size() - 1] + dists[dists.size() - 2] + L);
if (dists.size() >= 3)
{
ans = max(ans, dists[dists.size() - 2] + dists[dists[dists.size() - 3]] + 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 (stderr)
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:40: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]
40 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp:61: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]
61 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |