#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
vector <pair<ll,ll>> grafo[100005];
bool v[100005],flag;
ll dist=10000000000;
pair <ll,ll> ld(ll nodo, ll p) {
pair <ll,ll> ans = {-1,-1}, aux;
for (auto it=grafo[nodo].begin();it!=grafo[nodo].end();it++) {
if (it->f!=p) {
aux = ld(it->f,nodo);
if (ans.f==-1 || ans.s<aux.s+it->s) {
ans = aux;
ans.s += it->s;
}
}
}
if (ans.f == -1) ans = {nodo,0};
return ans;
}
void mid(ll v1, ll v2, ll total, ll p, ll sum) {
if (v1==v2) flag = true;
for (auto it=grafo[v1].begin();it!=grafo[v1].end();it++) {
if (it->f!=p && flag==false) {
mid(it->f,v2,total,v1,sum+it->s);
}
}
if (flag==true) {
dist = min(dist,max(sum,total-sum));
}
return;
}
void dfs(ll nodo, ll p) {
v[nodo] = true;
for (auto it=grafo[nodo].begin();it!=grafo[nodo].end();it++) {
if (it->f!=p) dfs(it->f,nodo);
}
return;
}
void solve(ll nodo) {
pair <ll,ll> v1,v2;
v1 = ld(nodo,-1);
v2 = ld(v1.f,-1);
mid(v1.f,v2.f,v2.s,-1,0);
dfs(nodo,-1);
return;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
ll i,resp;
vector <ll> vec;
for (i=0;i<M;i++) {
grafo[A[i]].push_back({B[i],T[i]});
grafo[B[i]].push_back({A[i],T[i]});
}
for (i=0;i<N;i++) {
if (v[i]==false) {
solve(i);
vec.push_back(dist);
dist=10000000000;
flag = false;
}
}
sort(vec.begin(),vec.end());
if (vec.size()>=3) resp = max(vec[vec.size()-1]+vec[vec.size()-2]+L,vec[vec.size()-2]+vec[vec.size()-3]+2*L);
else if (vec.size()>=2) resp = vec[vec.size()-1]+vec[vec.size()-2]+L;
else resp = vec[vec.size()-1];
return resp;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
16460 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 |
46 ms |
16460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
6036 KB |
Output is correct |
2 |
Correct |
20 ms |
6152 KB |
Output is correct |
3 |
Correct |
19 ms |
6132 KB |
Output is correct |
4 |
Correct |
20 ms |
6068 KB |
Output is correct |
5 |
Correct |
21 ms |
6136 KB |
Output is correct |
6 |
Correct |
19 ms |
6764 KB |
Output is correct |
7 |
Correct |
24 ms |
6164 KB |
Output is correct |
8 |
Correct |
18 ms |
6104 KB |
Output is correct |
9 |
Correct |
22 ms |
5972 KB |
Output is correct |
10 |
Correct |
21 ms |
6224 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
5 ms |
3916 KB |
Output is correct |
13 |
Correct |
5 ms |
3916 KB |
Output is correct |
14 |
Correct |
6 ms |
3920 KB |
Output is correct |
15 |
Correct |
6 ms |
3916 KB |
Output is correct |
16 |
Correct |
5 ms |
3932 KB |
Output is correct |
17 |
Correct |
5 ms |
3916 KB |
Output is correct |
18 |
Correct |
5 ms |
3916 KB |
Output is correct |
19 |
Correct |
5 ms |
3936 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2660 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
6 ms |
3928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
16460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |