이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (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: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 |
---|
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... |