#include <bits/stdc++.h>
#include "dreaming.h"
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (ll i = x; i < y; i++)
typedef long long ll;
using namespace std;
vector<pair<ll, ll>> graph[100001];
ll len, ans = 0, r[100001], indx = 0, cmp[100001];
pair<ll, ll> dp[100001];
bool visited[100001];
void ff(int node, int component, int parent = -1) {
visited[node] = true;
cmp[node] = component;
for (auto& i : graph[node]) {
if (i.first == parent) continue;
ff(i.first, component, node);
}
}
void dfs(ll node, ll parent = -1) {
int mx1 = 0, mx2 = 0;
for (auto& i : graph[node]) {
if (i.first == parent) continue;
dfs(i.first, node);
int e = dp[i.first].first + i.second;
if (e > mx1) {
mx2 = mx1;
mx1 = e;
} else if (e > mx2)
mx2 = e;
}
dp[node] = {mx1, mx2};
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
FOR(i, 0, M) {
graph[A[i]].push_back({B[i], T[i]});
graph[B[i]].push_back({A[i], T[i]});
}
FOR(i, 0, N) if (!visited[i]) ff(i, indx++);
return 0;
fill(r, r + indx, INT_MAX);
FOR(i, 0, N) {
dfs(i);
r[cmp[i]] = min(r[cmp[i]], dp[i].first);
ans = max(ans, dp[i].first + dp[i].second);
}
sort(r, r + indx, greater<ll>());
ans = max(ans, max((indx > 2 ? r[1] + r[2] + 2 * L : 0),
max((indx > 1 ? r[0] + r[1] + L : 0), r[0])));
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
10232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
10232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
10232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
5888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
10232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
10232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |