#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,l;
vector<int>sz;
vector<bool>vis;
vector<vector<pair<int,int>>>v;
int dfs(int x){
sz[x]=0;
vis[x]=true;
for(auto [z,w]:v[x]){
if(!vis[z]){
sz[x]+=w+dfs(z);
}
}
return sz[x];
}
int mn;
void dfs2(int x,int dis){
vis[x]=true;
int mx=0,s=0;
for(auto [z,w]:v[x]){
if(!vis[z]){
mx=max(mx,sz[z]+w);
s+=w+sz[z];
dfs2(z,dis);
}
}
mn=min(mn,max(mx,dis-s));
}
void dfs3(int x,int dis,int p=-1){
vector<int>a;
int s=0;
for(auto [z,w]:v[x]){
if(z!=p){
a.push_back(w+sz[z]);
s+=w+sz[z];
dfs3(z,dis,x);
}
}
a.push_back(dis-s);
sort(a.begin(),a.end());
s=a.back();
a.pop_back();
if(a.size()) s+=a.back();
mn=max(mn,s);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
swap(n,N);
swap(m,M);
swap(l,L);
v.assign(n,{});
for(int i=0;i<m;i++){
v[A[i]].push_back({B[i],T[i]});
v[B[i]].push_back({A[i],T[i]});
}
sz.resize(n);
vis.assign(n,false);
for(int i=0;i<n;i++){
if(!vis[i]){
dfs(i);
}
}
vis.assign(n,false);
vector<int>a;
for(int i=0;i<n;i++){
if(!vis[i]){
mn=(int)1e9;
dfs2(i,sz[i]);
a.push_back(mn);
}
}
if(a.size()==1){
mn=0;
dfs3(0,sz[0]);
return mn;
}
sort(a.begin(),a.end());
return a[a.size()-1]+a[a.size()-2]+L;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
11092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
11092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
6052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
11092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |