#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);
cout << dfs_up(i, -1, 0) << endl;
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;
}
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)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
13236 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 |
33 ms |
13236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
83 ms |
6916 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 |
33 ms |
13236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |