#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
#include "dreaming.h"
long long ww,lv[300005],lv1[300005],mx,vert,fix[300005],x1,x2,x,y;
std::vector <long long> v[300005],vv,ans;
map < pair <long long, long long > , long long > mp;
void dfs(int a, int p)
{
fix[a]=1;
if (ww==1) vv.push_back(a);
if (p!=-1)
lv[a]=lv[p]+mp[{a,p}];
else lv[a]=0;
if (lv[a]>mx)
{
vert=a;
mx=lv[a];
}
for (int i=0; i<v[a].size(); i++)
{
if (v[a][i]!=p)
{
dfs(v[a][i],a);
}
}
}
void dfs1(int a, int p)
{
if (p!=-1)
lv1[a]=lv1[p]+mp[{a,p}];
else lv1[a]=0;
for (int i=0; i<v[a].size(); i++)
{
if (v[a][i]!=p)
{
dfs1(v[a][i],a);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
for (int i=0; i<=1e5; i++)
v[i].clear();
ans.clear();
for (int i=0; i<M; i++)
{
mp[{A[i],B[i]}]=T[i];
mp[{B[i],A[i]}]=T[i];
v[A[i]].push_back(B[i]);
v[B[i]].push_back(A[i]);
}
for (int i=0; i<N; i++)
{
mx=0;
if (!fix[i])
{
vv.clear();
ww=1;
dfs(i,-1);
x1=vert;
ww=0;
mx=0;
dfs(x1,-1);
x2=vert;
dfs1(x2,-1);
x=1e10;
for (int j=0; j<vv.size(); j++)
{
y=max(lv[vv[j]], lv1[vv[j]]);
x=min(x, y);
}
ans.push_back(x);
}
}
sort(ans.begin(), ans.end());
reverse(ans.begin(), ans.end());
if (ans.size()>=3)
{
return max(ans[1]+ans[2]+2*L, ans[0]+ans[1]+L);
}
if (ans.size()>=2)
{
return ans[0]+ans[1]+L;
}
return 0;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:21:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i=0; i<v[a].size(); i++)
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'void dfs1(int, int)':
dreaming.cpp:34:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i=0; i<v[a].size(); i++)
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:70:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int j=0; j<vv.size(); j++)
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
354 ms |
35556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
354 ms |
35556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
354 ms |
35556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
16872 KB |
Output is correct |
2 |
Correct |
98 ms |
16880 KB |
Output is correct |
3 |
Correct |
93 ms |
16872 KB |
Output is correct |
4 |
Correct |
96 ms |
16872 KB |
Output is correct |
5 |
Correct |
92 ms |
16872 KB |
Output is correct |
6 |
Correct |
98 ms |
17892 KB |
Output is correct |
7 |
Correct |
107 ms |
17384 KB |
Output is correct |
8 |
Correct |
93 ms |
16752 KB |
Output is correct |
9 |
Correct |
89 ms |
16616 KB |
Output is correct |
10 |
Correct |
102 ms |
17132 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
17 ms |
10852 KB |
Output is correct |
13 |
Correct |
17 ms |
10988 KB |
Output is correct |
14 |
Correct |
17 ms |
10724 KB |
Output is correct |
15 |
Correct |
17 ms |
10856 KB |
Output is correct |
16 |
Correct |
16 ms |
10596 KB |
Output is correct |
17 |
Correct |
15 ms |
10080 KB |
Output is correct |
18 |
Correct |
18 ms |
10980 KB |
Output is correct |
19 |
Correct |
16 ms |
10724 KB |
Output is correct |
20 |
Correct |
6 ms |
7404 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
22 |
Correct |
6 ms |
7532 KB |
Output is correct |
23 |
Correct |
17 ms |
10724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
354 ms |
35556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
354 ms |
35556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |