#include "dreaming.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
using namespace std;
int n, m, l, dis[100001], leaf[100001], len, stp, edp, ans;
vector<pii> v[100001];
vector<int> path, mx;
void dfs(int x, int prev=-1){
dis[x]=0;
leaf[x]=x;
int mx=x, mx2=x;
for (pii i: v[x]){
if (i.f==prev) continue;
dfs(i.f, x);
dis[i.f]+=i.s;
if (dis[i.f]>dis[mx]) mx2=mx, mx=i.f;
else if (dis[i.f]>dis[mx2]) mx2=i.f;
}
if (dis[mx]+dis[mx2]>len) len=dis[mx]+dis[mx2], stp=leaf[mx], edp=leaf[mx2];
dis[x]=dis[mx];
leaf[x]=leaf[mx];
}
bool get_path(int x, int prev=-1){
if (x==edp) return true;
for (pii i: v[x]){
if (i.f==prev) continue;
if (get_path(i.f, x)){
path.push_back(i.s);
return true;
}
}
return false;
}
int max_length(int x){
len=stp=edp=-1;
dfs(x);
path.clear();
get_path(stp);
if (path.empty()) return 0;
for (int i=1; i<path.size(); i++) path[i]+=path[i-1];
int ans=2e9;
for (int i=0; i<path.size(); i++){
int t=path[i]; // 0..i
if (i<path.size()) t=max(t, path[path.size()-1]-path[i]);
ans=min(ans, t);
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N, m=M, l=L;
for (int i=0; i<m; i++){
v[A[i]].push_back({B[i], T[i]});
v[B[i]].push_back({A[i], T[i]});
}
for (int i=0; i<n; i++) dis[i]=-1;
for (int i=0; i<n; i++){
if (dis[i]==-1) mx.push_back(max_length(i));
}
sort(mx.begin(), mx.end(), greater<int>());
ans=mx[0];
if (mx.size()>1) ans=max(ans, mx[0]+mx[1]+l);
if (mx.size()>2) ans=max(ans, mx[1]+mx[2]+l*2);
return ans;
}
Compilation message
dreaming.cpp: In function 'int max_length(int)':
dreaming.cpp:46:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i=1; i<path.size(); i++) path[i]+=path[i-1];
| ~^~~~~~~~~~~~
dreaming.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i=0; i<path.size(); i++){
| ~^~~~~~~~~~~~
dreaming.cpp:50:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if (i<path.size()) t=max(t, path[path.size()-1]-path[i]);
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
16280 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 |
43 ms |
16280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
6488 KB |
Output is correct |
2 |
Correct |
24 ms |
6460 KB |
Output is correct |
3 |
Correct |
21 ms |
6488 KB |
Output is correct |
4 |
Correct |
24 ms |
6500 KB |
Output is correct |
5 |
Correct |
19 ms |
6512 KB |
Output is correct |
6 |
Correct |
20 ms |
7000 KB |
Output is correct |
7 |
Correct |
23 ms |
6636 KB |
Output is correct |
8 |
Correct |
21 ms |
6468 KB |
Output is correct |
9 |
Correct |
17 ms |
6356 KB |
Output is correct |
10 |
Correct |
21 ms |
6612 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
5 ms |
4072 KB |
Output is correct |
13 |
Correct |
5 ms |
4048 KB |
Output is correct |
14 |
Correct |
5 ms |
4048 KB |
Output is correct |
15 |
Correct |
6 ms |
4116 KB |
Output is correct |
16 |
Correct |
6 ms |
4048 KB |
Output is correct |
17 |
Correct |
5 ms |
3944 KB |
Output is correct |
18 |
Correct |
6 ms |
4176 KB |
Output is correct |
19 |
Correct |
5 ms |
4048 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
1 ms |
2664 KB |
Output is correct |
22 |
Correct |
2 ms |
2616 KB |
Output is correct |
23 |
Correct |
4 ms |
4048 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 |
43 ms |
16280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |