#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mxN = (int)1e5+10;
const int INF = (int)1e9;
int n, m, l,CC=1,ans2;
vector<int> path, v;
int vis[mxN], dis[mxN];
vector<pair<int,int>> adj[mxN];
bool dfs2(int s, int p, int t){
if(s==t) return 1;
for(auto x : adj[s]){
int u = x.fi, w = x.se;
if(u==p) continue;
if(dfs2(u,s,t)){ path.pb(w); return 1; }
}
return 0;
}
void dfs(int s, int p){
vis[s]=CC;
for(auto x : adj[s]){
int u = x.fi, w = x.se;
if(u==p) continue;
dis[u] = dis[s]+w; dfs(u,s);
}
}
void calcDiam(int s){
int a, b, mx = 0;
dis[s]=0; dfs(s,-1);
for(int i = 1; i <= n; i++)
if(vis[i]==CC and mx<=dis[i]) mx=dis[i],a=i;
dis[a]=0; mx = 0; dfs(a,-1);
for(int i = 1; i <= n; i++)
if(vis[i]==CC and mx<=dis[i]) mx=dis[i],b=i;
path.clear(); dfs2(a,-1,b);
int dis1 = accumulate(all(path),0), dis2 = 0;
int ans = dis1; ans2 = max(ans2, dis1);
for(auto u : path){
dis1-=u, dis2+=u;
ans = min(ans, max(dis1,dis2));
}
v.pb(ans);
if(sz(v)==4){
for(int i = sz(v)-2; i>=0; i--)
if(v[i]<v[i+1])swap(v[i],v[i+1]);
v.pop_back();
}
}
int travelTime(int N, int M, int L, int a[], int b[], int c[]) {
n = N; m = M; l = L;
for(int i = 0; i < m; i++){
a[i]++, b[i]++;
adj[a[i]].pb({b[i],c[i]});
adj[b[i]].pb({a[i],c[i]});
}
for(int i = 1; i <= n; i++)
if(!vis[i]) calcDiam(i),CC++;
int ans = INF, ans2=0;
if(sz(v)==1) return ans2;
if(sz(v)>=2) ans = v[sz(v)-1]+l+v[sz(v)-2];
if(sz(v)>=3) ans = max(ans,v[sz(v)-2]+l*2+v[sz(v)-3]);
return max(ans, ans2);
}
Compilation message
dreaming.cpp: In function 'void calcDiam(int)':
dreaming.cpp:43:23: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
43 | path.clear(); dfs2(a,-1,b);
| ~~~~^~~~~~~~
dreaming.cpp:43:23: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
15056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
15056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1058 ms |
5600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
15056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |