#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN=100005;
bool vst[MAXN], chk[MAXN];
vector< pair<int, int> > edge[MAXN];
int dep[MAXN], lng[MAXN], radious;
vector<int> rad;
void dfs1(int nod){
int i;
vst[nod]=1, chk[nod]=1;
for(i=0; i<edge[nod].size(); i++) if(!vst[edge[nod][i].first])
dfs1(edge[nod][i].first);
for(i=0; i<edge[nod].size(); i++) if(!vst[edge[nod][i].first])
dep[nod]=max(dep[nod], dep[edge[nod][i].first]+edge[nod][i].second);
vst[nod]=0;
}
void dfs2(int nod, int rt){
int i, dis, mx1=0, mx2=0, idx1;
vst[nod]=1;
chk[nod]=1;
for(i=0; i<edge[nod].size(); i++)if(!vst[edge[nod][i].first]){
dis=dep[edge[nod][i].first]+edge[nod][i].second;
if(dis>mx1)
mx1=dis, idx1=i;
else if(dis>mx2)
mx2=dis;
}
lng[nod]=mx1+max(rt, mx2);
if(max(mx1, rt)<radious)
radious=max(mx1, rt);
for(i=0; i<edge[nod].size(); i++)if(!vst[edge[nod][i].first]){
if(i==idx1)
dfs2(edge[nod][i].first, max(rt, mx2)+edge[nod][i].second);
else
dfs2(edge[nod][i].first, max(rt, mx1)+edge[nod][i].second);
}
vst[nod]=0;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int i, dia=0;
for(i=0; i<M; i++){
edge[A[i]].push_back(make_pair(B[i], T[i]));
edge[B[i]].push_back(make_pair(A[i], T[i]));
}
for(i=0; i<N; i++) if(!chk[i])
dfs1(i);
memset(chk, 0, sizeof chk);
for(i=0; i<N; i++) if(!chk[i]){
radious=2e9;
dfs2(i, 0);
rad.push_back(radious);
}
sort(rad.begin(), rad.end(), greater<int>());
for(i=0; i<N; i++) dia=max(dia, lng[i]);
if(rad.size()==1)
return dia;
else if(rad.size()==2)
return max(dia, rad[0]+rad[1]+L);
else
return max(dia, max(rad[0]+rad[1]+L, rad[1]+rad[2]+L*2));
}
Compilation message
dreaming.cpp: In function 'void dfs1(int)':
dreaming.cpp:13:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[nod].size(); i++) if(!vst[edge[nod][i].first])
~^~~~~~~~~~~~~~~~~
dreaming.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[nod].size(); i++) if(!vst[edge[nod][i].first])
~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int)':
dreaming.cpp:24:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[nod].size(); i++)if(!vst[edge[nod][i].first]){
~^~~~~~~~~~~~~~~~~
dreaming.cpp:34:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[nod].size(); i++)if(!vst[edge[nod][i].first]){
~^~~~~~~~~~~~~~~~~
dreaming.cpp:35:9: warning: 'idx1' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(i==idx1)
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
10616 KB |
Output is correct |
2 |
Correct |
69 ms |
10412 KB |
Output is correct |
3 |
Correct |
48 ms |
9080 KB |
Output is correct |
4 |
Correct |
11 ms |
3968 KB |
Output is correct |
5 |
Correct |
10 ms |
3472 KB |
Output is correct |
6 |
Correct |
16 ms |
4480 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
10616 KB |
Output is correct |
2 |
Correct |
69 ms |
10412 KB |
Output is correct |
3 |
Correct |
48 ms |
9080 KB |
Output is correct |
4 |
Correct |
11 ms |
3968 KB |
Output is correct |
5 |
Correct |
10 ms |
3472 KB |
Output is correct |
6 |
Correct |
16 ms |
4480 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
10616 KB |
Output is correct |
2 |
Correct |
69 ms |
10412 KB |
Output is correct |
3 |
Correct |
48 ms |
9080 KB |
Output is correct |
4 |
Correct |
11 ms |
3968 KB |
Output is correct |
5 |
Correct |
10 ms |
3472 KB |
Output is correct |
6 |
Correct |
16 ms |
4480 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
6272 KB |
Output is correct |
2 |
Correct |
28 ms |
6400 KB |
Output is correct |
3 |
Correct |
26 ms |
6272 KB |
Output is correct |
4 |
Correct |
28 ms |
6400 KB |
Output is correct |
5 |
Correct |
30 ms |
6324 KB |
Output is correct |
6 |
Correct |
38 ms |
6784 KB |
Output is correct |
7 |
Correct |
31 ms |
6520 KB |
Output is correct |
8 |
Correct |
28 ms |
6264 KB |
Output is correct |
9 |
Correct |
28 ms |
6272 KB |
Output is correct |
10 |
Correct |
29 ms |
6400 KB |
Output is correct |
11 |
Correct |
6 ms |
2816 KB |
Output is correct |
12 |
Correct |
8 ms |
4348 KB |
Output is correct |
13 |
Correct |
8 ms |
4348 KB |
Output is correct |
14 |
Correct |
8 ms |
4220 KB |
Output is correct |
15 |
Correct |
8 ms |
4220 KB |
Output is correct |
16 |
Correct |
8 ms |
4220 KB |
Output is correct |
17 |
Correct |
8 ms |
3964 KB |
Output is correct |
18 |
Correct |
8 ms |
4348 KB |
Output is correct |
19 |
Correct |
12 ms |
4220 KB |
Output is correct |
20 |
Correct |
5 ms |
2816 KB |
Output is correct |
21 |
Correct |
4 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2816 KB |
Output is correct |
23 |
Correct |
8 ms |
4220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
10616 KB |
Output is correct |
2 |
Correct |
69 ms |
10412 KB |
Output is correct |
3 |
Correct |
48 ms |
9080 KB |
Output is correct |
4 |
Correct |
11 ms |
3968 KB |
Output is correct |
5 |
Correct |
10 ms |
3472 KB |
Output is correct |
6 |
Correct |
16 ms |
4480 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
10616 KB |
Output is correct |
2 |
Correct |
69 ms |
10412 KB |
Output is correct |
3 |
Correct |
48 ms |
9080 KB |
Output is correct |
4 |
Correct |
11 ms |
3968 KB |
Output is correct |
5 |
Correct |
10 ms |
3472 KB |
Output is correct |
6 |
Correct |
16 ms |
4480 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |