#include "dreaming.h"
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
bool byGreater(int a,int b)
{
return a>b;
}
struct edge
{
int from,to;
int w;
};
vector<edge> adjList[100005];
int Far[100005]; //Stores the minimum longest distance for each tree
int color[100005];
int d[2][100005];
queue<int> Q;
int bfs(int s,int op,int c)
{
color[s]=c;
d[op][s]=0;
Q.push(s);
int farthest_node;
int maximum=0;
while(Q.size()){
int u=Q.front();Q.pop();
for(edge e: adjList[u]){
int v=e.to;
if(color[v]!=c){
color[v]=c;
d[op][v]=d[op][u]+e.w;
if(d[op][v]>maximum){
maximum=d[op][v];
farthest_node=v;
}
Q.push(v);
}
}
}
return farthest_node;
}
int getOptimumDistance(int s,int c)
{
color[s]=c;
int minimumD=1000000000;
Q.push(s);
while(Q.size()){
int u=Q.front();Q.pop();
if(max(d[0][u],d[1][u])<minimumD){
minimumD=max(d[0][u],d[1][u]);
}
for(edge e: adjList[u]){
int v=e.to;
if(color[v]!=c){
color[v]=c;
Q.push(v);
}
}
}
return minimumD;
}
int far[5];
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i=0;i<M;i++){
edge e={A[i],B[i],T[i]};
adjList[e.from].push_back(e);
swap(e.from,e.to);
adjList[e.from].push_back(e);
}
int answer=0;
int max_diameter=0;
int c=1;
int trees=0;
for(int u=0;u<N;u++){
if(color[u]!=0) continue;
int a=bfs(u,1,c++);
int b=bfs(a,0,c++);
bfs(b,1,c++);
max_diameter=max(max_diameter,d[1][a]);
Far[trees++]=getOptimumDistance(u,c++);
}
for(int i=0;i<trees;i++){
if(Far[i]>far[0] || Far[i]>far[1] || Far[i]>far[2]){
far[3]=Far[i];
sort(far,far+4,byGreater);
}
}
//cout<<far[0]<<" "<<far[1]<<" "<<far[2]<<endl;
if(trees==1){
answer=max_diameter;
}
else if(trees==2){
answer=max(max_diameter,far[0]+L+far[1]); //Path joining yourself with another tree
}
else{
answer=max(max_diameter,
max(far[0]+L+far[1],far[1]+L+L+far[2])); //Path joining yourself with another tree
//and joining other two trees whose path passes for you
}
return answer;
}
Compilation message
dreaming.cpp: In function 'int bfs(int, int, int)':
dreaming.cpp:43:12: warning: 'farthest_node' may be used uninitialized in this function [-Wmaybe-uninitialized]
return farthest_node;
^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
7800 KB |
Output is correct |
2 |
Correct |
67 ms |
8952 KB |
Output is correct |
3 |
Correct |
45 ms |
6904 KB |
Output is correct |
4 |
Correct |
12 ms |
3448 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
20 ms |
4216 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
35 ms |
5496 KB |
Output is correct |
9 |
Correct |
46 ms |
6064 KB |
Output is correct |
10 |
Correct |
5 ms |
2680 KB |
Output is correct |
11 |
Correct |
87 ms |
6648 KB |
Output is correct |
12 |
Correct |
71 ms |
8332 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
7800 KB |
Output is correct |
2 |
Correct |
67 ms |
8952 KB |
Output is correct |
3 |
Correct |
45 ms |
6904 KB |
Output is correct |
4 |
Correct |
12 ms |
3448 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
20 ms |
4216 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
35 ms |
5496 KB |
Output is correct |
9 |
Correct |
46 ms |
6064 KB |
Output is correct |
10 |
Correct |
5 ms |
2680 KB |
Output is correct |
11 |
Correct |
87 ms |
6648 KB |
Output is correct |
12 |
Correct |
71 ms |
8332 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
4 ms |
2680 KB |
Output is correct |
24 |
Correct |
4 ms |
2680 KB |
Output is correct |
25 |
Correct |
4 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2752 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
7800 KB |
Output is correct |
2 |
Correct |
67 ms |
8952 KB |
Output is correct |
3 |
Correct |
45 ms |
6904 KB |
Output is correct |
4 |
Correct |
12 ms |
3448 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
20 ms |
4216 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
35 ms |
5496 KB |
Output is correct |
9 |
Correct |
46 ms |
6064 KB |
Output is correct |
10 |
Correct |
5 ms |
2680 KB |
Output is correct |
11 |
Correct |
87 ms |
6648 KB |
Output is correct |
12 |
Correct |
71 ms |
8332 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
4 ms |
2680 KB |
Output is correct |
24 |
Correct |
4 ms |
2680 KB |
Output is correct |
25 |
Correct |
4 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2752 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
12956 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
7800 KB |
Output is correct |
2 |
Correct |
67 ms |
8952 KB |
Output is correct |
3 |
Correct |
45 ms |
6904 KB |
Output is correct |
4 |
Correct |
12 ms |
3448 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
20 ms |
4216 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
35 ms |
5496 KB |
Output is correct |
9 |
Correct |
46 ms |
6064 KB |
Output is correct |
10 |
Correct |
5 ms |
2680 KB |
Output is correct |
11 |
Correct |
87 ms |
6648 KB |
Output is correct |
12 |
Correct |
71 ms |
8332 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2940 KB |
Output is correct |
17 |
Correct |
4 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
6 ms |
2936 KB |
Output is correct |
20 |
Correct |
4 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
6 ms |
2936 KB |
Output is correct |
23 |
Correct |
4 ms |
2680 KB |
Output is correct |
24 |
Correct |
4 ms |
2680 KB |
Output is correct |
25 |
Correct |
4 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
7800 KB |
Output is correct |
2 |
Correct |
67 ms |
8952 KB |
Output is correct |
3 |
Correct |
45 ms |
6904 KB |
Output is correct |
4 |
Correct |
12 ms |
3448 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
20 ms |
4216 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
35 ms |
5496 KB |
Output is correct |
9 |
Correct |
46 ms |
6064 KB |
Output is correct |
10 |
Correct |
5 ms |
2680 KB |
Output is correct |
11 |
Correct |
87 ms |
6648 KB |
Output is correct |
12 |
Correct |
71 ms |
8332 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
4 ms |
2680 KB |
Output is correct |
24 |
Correct |
4 ms |
2680 KB |
Output is correct |
25 |
Correct |
4 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2752 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |