#include "dreaming.h"
#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,int>
using namespace std;
const int N=1e5+1;
int compo;
ll a,adis,b,bdis,c,cdis;
ll dis[N];
bool chk;
bitset<N>vis;
vector<pii>adj[N];
void dfs1(int u,int p,bool boo){
vis[u]=true;
if(!boo&&dis[u]>adis){
a=u;
adis=dis[u];
}
else if(boo&&dis[u]>bdis){
b=u;
bdis=dis[u];
}
//if(boo)cout << u << ',' << dis[u] << ' ';
for(pii wv:adj[u]){
ll w=wv.first;
int v=wv.second;
if(v==p)continue;
dis[v]=dis[u]+w;
dfs1(v,u,boo);
}
}
void dfs2(int u,int p){
if(u==a){
chk=true;
c=u;
cdis=bdis;
return ;
}
for(pii wv:adj[u]){
int v=wv.second;
if(v==p)continue;
dfs2(v,u);
if(chk){
if(max(dis[u],bdis-dis[u])<cdis){
c=u;
cdis=max(dis[u],bdis-dis[u]);
}
return ;
}
}
}
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({T[i],B[i]});
adj[B[i]].push_back({T[i],A[i]});
}
ll mx=0,smx=0,tmx=0,mxb=0;
for(int i=0; i<N; i++){
if(vis[i])continue;
compo++;
a=i;
adis=0,bdis=0;
chk=false;
dis[i]=0;
dfs1(i,-1,0);
dis[a]=0;
b=a,c=a;
dfs1(a,-1,1);
dfs2(b,-1);
if(cdis>=mx){tmx=smx;smx=mx;mx=cdis;}
else if(cdis>=smx){tmx=smx;smx=cdis;}
else if(cdis>tmx)tmx=cdis;
mxb=max(bdis,mxb);
}
if(compo>2)return max(mx+smx+L,smx+L+L+tmx);
else if(compo>1)return mx+smx+L;
else return mxb;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
15908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
15908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
6492 KB |
Output is correct |
2 |
Correct |
12 ms |
6400 KB |
Output is correct |
3 |
Correct |
12 ms |
6964 KB |
Output is correct |
4 |
Correct |
12 ms |
7000 KB |
Output is correct |
5 |
Correct |
12 ms |
7004 KB |
Output is correct |
6 |
Correct |
16 ms |
7004 KB |
Output is correct |
7 |
Correct |
18 ms |
7164 KB |
Output is correct |
8 |
Correct |
12 ms |
6748 KB |
Output is correct |
9 |
Correct |
13 ms |
6764 KB |
Output is correct |
10 |
Correct |
16 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
4444 KB |
Output is correct |
13 |
Correct |
3 ms |
4528 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
4696 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
3 ms |
4556 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
15908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |