#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
using ll = long long;
ll n, m, q, k, x, y, a, b, c, l;
vector< pair<ll, ll> > G[100000];
ll mx[100000], mxu[100000];
bool vis[100000];
void dfs(ll x, ll p) {
vis[x] = 1;
for (auto i : G[x]) {
if (i.first != p) {
dfs(i.first, x);
mx[x] = max(mx[x], mx[i.first] + i.second);
}
}
}
void dfsu(ll x, ll p) {
vector< pair<ll, ll> > v;
for (auto i : G[x]) {
if (i.first != p) {
v.push_back({mx[i.first] + i.second, i.first});
}
}
sort(v.rbegin(), v.rend());
for (auto i : G[x]) {
if (i.first != p) {
mxu[i.first] = mxu[x] + i.second;
if (v.size() != 1) {
if (v[0].second != i.first) {
mxu[i.first] = max(mxu[i.first], v[0].first + i.second);
}
else {
mxu[i.first] = max(mxu[i.first], v[1].first + i.second);
}
}
dfsu(i.first, x);
}
}
// cout << x << ' ' << mx[x] << ' ' << mxu[x] << '\n';
}
pair<int, int> dfsp(ll x, ll p) {
pair<int, int> pr = {max(mx[x], mxu[x]), x};
for (auto i : G[x]) {
if (i.first != p) {
pr = min(pr, dfsp(i.first, x));
}
}
return pr;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
m = M;
l = L;
for (int i = 0; i < m; i++) {
G[A[i]].push_back({B[i], T[i]});
G[B[i]].push_back({A[i], T[i]});
}
vector< pair<int, int> > vp;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i, -1);
dfsu(i, -1);
vp.push_back(dfsp(i, -1));
}
}
// for (auto i : vp) {
// cout << i.first << ' ' << i.second << '\n';
// }
sort(vp.rbegin(), vp.rend());
int ans = vp[0].first + vp[1].first + L;
if (vp.size() > 2) {
ans = max(ans, vp[1].first + vp[2].first + L * 2);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
19540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
19540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
7656 KB |
Output is correct |
2 |
Correct |
18 ms |
7640 KB |
Output is correct |
3 |
Correct |
18 ms |
7640 KB |
Output is correct |
4 |
Correct |
24 ms |
7700 KB |
Output is correct |
5 |
Correct |
31 ms |
7636 KB |
Output is correct |
6 |
Correct |
31 ms |
8472 KB |
Output is correct |
7 |
Correct |
32 ms |
7844 KB |
Output is correct |
8 |
Correct |
24 ms |
7432 KB |
Output is correct |
9 |
Correct |
26 ms |
7500 KB |
Output is correct |
10 |
Correct |
22 ms |
7780 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
11 ms |
4820 KB |
Output is correct |
13 |
Correct |
8 ms |
5076 KB |
Output is correct |
14 |
Correct |
10 ms |
4820 KB |
Output is correct |
15 |
Correct |
13 ms |
5076 KB |
Output is correct |
16 |
Correct |
8 ms |
4816 KB |
Output is correct |
17 |
Correct |
13 ms |
4304 KB |
Output is correct |
18 |
Correct |
9 ms |
5208 KB |
Output is correct |
19 |
Correct |
9 ms |
4816 KB |
Output is correct |
20 |
Incorrect |
2 ms |
2788 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
19540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |