#include <bits/stdc++.h>
using namespace std;
#include "dreaming.h"
const int N=1e5+4;
vector<array<int,2>>adj[N];
int d[N][2],vis[N];
vector<int>passed,path;
void dfs(int u,bool x){
passed.push_back(u);
vis[u]=1;
for(auto&[v,w]:adj[u]){
if(vis[v])
continue;
d[v][x]=d[u][x]+w;
dfs(v,x);
}
}
bool findpath(int u,int p,int s,int e){
if(u==e){
path.push_back(e);
return 1;
}
for(auto&[v,w]:adj[u]){
if(v==p)
continue;
if(findpath(v,u,s,e)){
path.push_back(v);
return 1;
}
}
return 0;
}
void unq(vector<int>&v){
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
int findmxlen(int node){
d[node][0]=0;
dfs(node,0);
unq(passed);
int mx=-1,kraj1,kraj2;
for(int u:passed){
if(d[u][0]>mx){
mx=d[u][0];
kraj1=u;
}
vis[u]=0;
}
passed.clear();
d[kraj1][0]=0;
dfs(kraj1,0);
unq(passed);
mx=-1;
for(int u:passed){
if(d[u][0]>mx){
mx=d[u][0];
kraj2=u;
}
vis[u]=0;
}
passed.clear();
d[kraj2][1]=0;
dfs(kraj2,1);
unq(passed);
for(int u:passed)
vis[u]=0;
path.clear();
findpath(kraj1,-1,kraj1,kraj2);
unq(passed);
for(int u:passed)
vis[u]=1;
int mnmxlen=2e9;
for(int v:path)
mnmxlen=min(mnmxlen,max(d[v][0],d[v][1]));
passed.clear();
return mnmxlen;
}
int jednostablo(int node=1){
d[node][0]=0;
dfs(node,0);
int mx=-1,kraj1,kraj2;
for(auto&u:passed){
if(d[u][0]>mx){
mx=d[u][0];
kraj1=u;
}
vis[u]=0;
}
passed.clear();
d[kraj1][0]=0;
dfs(kraj1,0);
mx=-1;
for(auto&u:passed)
if(d[u][0]>mx)
mx=d[u][0];
return mx;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int n=N,m=M,l=L;
for(int i=0,u,v,w;i<m;++i){
u=A[i];
v=B[i];
w=T[i];
++u,++v;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
if(m==n-1)
return jednostablo();
vector<int>poludijametri;
for(int i=1;i<=n;++i){
if(vis[i])
continue;
poludijametri.push_back(findmxlen(i));
}
sort(poludijametri.rbegin(),poludijametri.rend());
int ans=poludijametri[0]+poludijametri[1]+l;
if(poludijametri.size()>2)
ans=max(ans,poludijametri[1]+poludijametri[2]+2*l);
return ans;
}
Compilation message
dreaming.cpp: In function 'int jednostablo(int)':
dreaming.cpp:89:21: warning: unused variable 'kraj2' [-Wunused-variable]
89 | int mx=-1,kraj1,kraj2;
| ^~~~~
dreaming.cpp:99:8: warning: 'kraj1' may be used uninitialized in this function [-Wmaybe-uninitialized]
99 | dfs(kraj1,0);
| ~~~^~~~~~~~~
dreaming.cpp: In function 'int findmxlen(int)':
dreaming.cpp:75:13: warning: 'kraj2' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | findpath(kraj1,-1,kraj1,kraj2);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
dreaming.cpp:75:13: warning: 'kraj1' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
15816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
15816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
6480 KB |
Output is correct |
2 |
Correct |
18 ms |
6608 KB |
Output is correct |
3 |
Correct |
18 ms |
6492 KB |
Output is correct |
4 |
Correct |
18 ms |
6616 KB |
Output is correct |
5 |
Correct |
18 ms |
6480 KB |
Output is correct |
6 |
Correct |
19 ms |
7060 KB |
Output is correct |
7 |
Correct |
20 ms |
6696 KB |
Output is correct |
8 |
Correct |
19 ms |
6480 KB |
Output is correct |
9 |
Correct |
17 ms |
6496 KB |
Output is correct |
10 |
Correct |
25 ms |
6672 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
9 ms |
4556 KB |
Output is correct |
13 |
Correct |
9 ms |
4556 KB |
Output is correct |
14 |
Correct |
9 ms |
4448 KB |
Output is correct |
15 |
Correct |
9 ms |
4728 KB |
Output is correct |
16 |
Correct |
10 ms |
4496 KB |
Output is correct |
17 |
Correct |
8 ms |
4300 KB |
Output is correct |
18 |
Correct |
9 ms |
4556 KB |
Output is correct |
19 |
Correct |
9 ms |
4556 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
2 ms |
2640 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
8 ms |
4508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
15816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |