#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define f first
#define s second
vector<pair<int,int>> adj[100000];
int dp1[100000],dp2[100000],dp3[100000];
bool marked[100000]={0};
int dfs_sizes1(int v, int pV){
dp1[v]=0;
dp2[v]=0;
marked[v]=1;
for(auto [w,d] : adj[v]){
if(w!=pV){
int sus=dfs_sizes1(w,v)+d;
if(sus>dp1[v]){
dp2[v]=dp1[v];
dp1[v]=sus;
}else if(sus>dp2[v]){
dp2[v]=sus;
}
}
}
return dp1[v];
}
void dfs_sizes2(int v, int pV){
for(auto [w,d] : adj[v]){
if(w!=pV){
if(dp1[w]+d==dp1[v]){
dp3[w]=max(dp2[v],dp3[v])+d;
}else{
dp3[w]=max(dp1[v],dp3[v])+d;
}
dfs_sizes2(w,v);
}
}
}
int dfs_final(int v, int pV){
int ans=max(dp1[v],dp3[v]);
for(auto [w,d] : adj[v]){
if(w!=pV){
ans=min(ans,dfs_final(w,v));
}
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i=0;i<M;i++){
adj[A[i]].push_back({B[i],T[i]});
adj[B[i]].push_back({A[i],T[i]});
}
vector<int> sussy;
for(int i=0;i<N;i++){
if(!marked[i]){
dfs_sizes1(i,i);
dfs_sizes2(i,i);
sussy.push_back(-dfs_final(i,i));
// cout <<i << ' ' << sussy[sussy.size()-1] << '\n';
}
// cout << i << ' ' << dp1[i].first << ' ' << dp1[i].second << '\n';
}
for(int i=0;i<N;i++){
// cout << i << ' ' << dp1[i] << ' ' << dp2[i] << ' ' << dp3[i] << '\n';
}
sort(sussy.begin(),sussy.end());
if(sussy.size()==1)return -sussy[0];
else if(sussy.size()==2)return -sussy[0]-sussy[1]+L;
return max(-sussy[0]-sussy[1]+L,-sussy[2]-sussy[1]+L*2);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
11860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
11860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
7504 KB |
Output is correct |
2 |
Correct |
14 ms |
7516 KB |
Output is correct |
3 |
Correct |
15 ms |
7452 KB |
Output is correct |
4 |
Correct |
14 ms |
7512 KB |
Output is correct |
5 |
Correct |
13 ms |
7476 KB |
Output is correct |
6 |
Correct |
24 ms |
7728 KB |
Output is correct |
7 |
Correct |
18 ms |
7512 KB |
Output is correct |
8 |
Correct |
13 ms |
7392 KB |
Output is correct |
9 |
Correct |
19 ms |
7384 KB |
Output is correct |
10 |
Correct |
15 ms |
7516 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
3 ms |
5592 KB |
Output is correct |
13 |
Correct |
4 ms |
5592 KB |
Output is correct |
14 |
Correct |
4 ms |
5592 KB |
Output is correct |
15 |
Correct |
3 ms |
5592 KB |
Output is correct |
16 |
Correct |
3 ms |
5592 KB |
Output is correct |
17 |
Correct |
4 ms |
5592 KB |
Output is correct |
18 |
Correct |
5 ms |
5588 KB |
Output is correct |
19 |
Correct |
4 ms |
5592 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
3000 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
3 ms |
5592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
11860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |