#include <bits/stdc++.h>
#include "dreaming.h"
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int NN=1e5+5;
int l[NN],d[NN],p[NN];
vector <int> gr[NN];
vector <pair <int,int> >g[NN];
int find_set(int v){
if(l[v]==v)return v;
return l[v]=find_set(l[v]);
}
void union_set(int u,int v){
u=find_set(u);
v=find_set(v);
if(u!=v){
if(gr[v].size()<gr[u].size())swap(u,v);
l[u]=v;
for(auto x : gr[u])gr[v].pb(x);
gr[u].clear();
}
}
void bfs(int v){
queue <int> q;
q.push(v);
while(!q.empty()){
int v=q.front();
q.pop();
for(auto to : g[v]){
if(d[to.ff]==-1){
p[to.ff]=v;
d[to.ff]=d[v]+to.ss;
q.push(to.ff);
}
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i=0;i<N;i++){
l[i]=i;
gr[i].pb(i);
}
for(int i=0;i<M;i++){
union_set(A[i],B[i]);
g[A[i]].pb({B[i],T[i]});
g[B[i]].pb({A[i],T[i]});
}
vector <int> len;
for(int i=0;i<N;i++)d[i]=-1;
for(int i=0;i<N;i++){
if(gr[i].size()!=0){
d[gr[i][0]]=0;
bfs(gr[i][0]);
int mx=0,node=gr[i][0];
for(auto x : gr[i]){
if(d[x]>mx){
mx=d[x];
node=x;
}
d[x]=-1;
p[x]=0;
}
d[node]=0;
bfs(node);
mx=0;
int s=node;
for(auto x : gr[i]){
if(d[x]>mx){
mx=d[x];
node=x;
}
}
vector <int> path;
path.pb(node);
while(node!=s){
node=p[node];
path.pb(node);
}
int mn=1e9+5;
for(auto x : path){
mn=min(max(mx-d[x],d[x]),mn);
}
len.pb(mn);
for(auto x : gr[i]){
d[x]=-1;
p[x]=0;
}
}
}
int ans=0;
sort(all(len));
for(auto x : len)ans=max(ans,x);
if(len.size()>=2)ans=max(ans,L+len[len.size()-1]+len[len.size()-2]);
if(len.size()>=3)ans=max(ans,2*L+len[len.size()-2]+len[len.size()-3]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
14036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
14036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
12632 KB |
Output is correct |
2 |
Correct |
30 ms |
12636 KB |
Output is correct |
3 |
Correct |
27 ms |
12636 KB |
Output is correct |
4 |
Correct |
26 ms |
12636 KB |
Output is correct |
5 |
Correct |
31 ms |
12592 KB |
Output is correct |
6 |
Correct |
36 ms |
13016 KB |
Output is correct |
7 |
Correct |
34 ms |
12892 KB |
Output is correct |
8 |
Correct |
27 ms |
12496 KB |
Output is correct |
9 |
Correct |
30 ms |
12504 KB |
Output is correct |
10 |
Correct |
33 ms |
12760 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
15 ms |
10972 KB |
Output is correct |
13 |
Correct |
15 ms |
10972 KB |
Output is correct |
14 |
Correct |
16 ms |
10972 KB |
Output is correct |
15 |
Correct |
15 ms |
10972 KB |
Output is correct |
16 |
Correct |
15 ms |
10972 KB |
Output is correct |
17 |
Correct |
20 ms |
11324 KB |
Output is correct |
18 |
Correct |
15 ms |
10968 KB |
Output is correct |
19 |
Correct |
17 ms |
11016 KB |
Output is correct |
20 |
Correct |
2 ms |
6488 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
20 ms |
11068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
14036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |