#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector <vector<pair<int, int> > > adj;
vector <int> mxdepth;
bitset <100000> vis;
int mxDia;
void dfs(int node, int ata = -1) {
vis[node] = 1;
int arr[3] = {0};
for(auto &[to, w]:adj[node]) {
if(to == ata) continue;
dfs(to, node);
mxdepth[node] = max(mxdepth[node], mxdepth[to] + w);
arr[2] = mxdepth[to] + w;
for(int i = 1; arr[i] < arr[i + 1] and i; i--) {
swap(arr[i], arr[i + 1]);
}
}
mxDia = max(arr[0] + arr[1], mxDia);
}
int dfs2(int node, int ust = 0, int ata = -1) {
// cout<<"node: "<<node<<" "<<ust<<"\n";
int arr[3] = {0, 0, 0}, brr[3] = {-1, -1, -1};
for(auto &[to, w]:adj[node]) {
if(to == ata) continue;
arr[2] = mxdepth[to] + w;
brr[2] = to;
for(int i = 1; ~i; i--) {
if(arr[i] < arr[i + 1]) {
swap(arr[i], arr[i + 1]);
swap(brr[i], brr[i + 1]);
}
}
}
for(auto &[to, w]:adj[node]) {
if(to == ata) continue;
int othermax = to == brr[0] ? arr[1] : arr[0];
int ifgoust = max(ust + w, othermax + w);
int valifto = max(mxdepth[to], ifgoust);
// cout<<arr[0]<<" "<<arr[1]<<"\n";
// cout<<brr[0]<<" "<<brr[1]<<"\n";
// cout<<to<<" "<<othermax<<" "<<ifgoust<<" "<<valifto<<"\n";
if(max(ust, mxdepth[node]) > valifto) {
return dfs2(to, ifgoust, node);
}
}
return max(ust, mxdepth[node]);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj.assign(N, vector <pair<int, int> > (0));
mxdepth.assign(N, 0);
for(int i = 0; i < M; i++) {
int a = A[i], b = B[i], t = T[i];
adj[a].push_back({b, t});
adj[b].push_back({a, t});
}
vector <int> trees;
for(int i = 0; i < N; i++) {
if(!vis[i]) {
dfs(i);
int depth = dfs2(i);
trees.push_back(depth);
}
}
// for(auto it:trees) {
// cout<<it<<" ";
// }
if(trees.size() == 1) {
return mxDia;
}
sort(trees.begin(), trees.end());
reverse(trees.begin(), trees.end());
int ans = max(mxDia, trees[0] + trees[1] + L);
// cout<<trees[0]<<" "<<trees[1]<<"\n";
if(trees.size() > 2) {
ans = max(ans, trees[1] + trees[2] + 2 * L);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
9808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
9808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
5724 KB |
Output is correct |
2 |
Correct |
14 ms |
5724 KB |
Output is correct |
3 |
Correct |
16 ms |
6124 KB |
Output is correct |
4 |
Correct |
18 ms |
6104 KB |
Output is correct |
5 |
Correct |
16 ms |
6104 KB |
Output is correct |
6 |
Correct |
16 ms |
6624 KB |
Output is correct |
7 |
Correct |
13 ms |
6368 KB |
Output is correct |
8 |
Correct |
13 ms |
5968 KB |
Output is correct |
9 |
Correct |
14 ms |
5980 KB |
Output is correct |
10 |
Correct |
17 ms |
6356 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
3800 KB |
Output is correct |
13 |
Correct |
6 ms |
3800 KB |
Output is correct |
14 |
Correct |
4 ms |
3800 KB |
Output is correct |
15 |
Correct |
3 ms |
3800 KB |
Output is correct |
16 |
Correct |
4 ms |
3800 KB |
Output is correct |
17 |
Correct |
4 ms |
3848 KB |
Output is correct |
18 |
Correct |
6 ms |
4052 KB |
Output is correct |
19 |
Correct |
4 ms |
3800 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
4 ms |
3800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
9808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |