#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mxN = (int)1e5+10;
const int INF = (int)2e9;
int n, m, l;
vector<int> path;
vector<pair<int,int>> v, adj[mxN];
int vis[mxN];
int dis[mxN];
int CC=1;
bool dfs2(int s, int p, int t){
if(s==t) return 1;
for(auto x : adj[s]){
int u = x.fi, w = x.se;
if(u==p) continue;
if(dfs2(u,s,t)){ path.pb(w); return 1; }
}
return 0;
}
void dfs(int s, int p){
vis[s]=CC;
for(auto x : adj[s]){
int u = x.fi, w = x.se;
if(u==p) continue;
dis[u] = dis[s]+w;
dfs(u,s);
}
}
void calcDiam(int s){
int a, b, mx = 0;
dis[s]=0; dfs(s,-1);
for(int i = 1; i <= n; i++)
if(vis[i]==CC and mx<=dis[i]) mx=dis[i],a=i;
dis[a]=0; mx = 0; dfs(a,-1);
for(int i = 1; i <= n; i++)
if(vis[i]==CC and mx<=dis[i]) mx=dis[i],b=i;
path.clear(); dfs2(a,-1,b);
int dis1 = accumulate(all(path),0), dis2 = 0;
int ans = dis1, diam=dis1;
for(auto u : path){
dis1-=u, dis2+=u;
ans = min(ans, max(dis1,dis2));
}
v.pb({ans,diam});
}
int travelTime(int N, int M, int L, int a[], int b[], int c[]) {
n = N; m = M; l = L;
for(int i = 0; i < m; i++){
a[i]++, b[i]++;
adj[a[i]].pb({b[i],c[i]});
adj[b[i]].pb({a[i],c[i]});
}
for(int i = 1; i <= n; i++)
if(!vis[i]) calcDiam(i),CC++;
int ans = INF, ans2=0;
sort(all(v));
for(int i = 0; i < sz(v); i++)
ans2 = max(ans2, v[i].se);
if(sz(v)==1) return ans2;
if(sz(v)>=2) ans = v[sz(v)-1].fi+l+v[sz(v)-2].fi;
if(sz(v)>=3) ans = max(ans,v[sz(v)-2].fi+l+v[sz(v)-3].fi);
return max(ans, ans2);
}
Compilation message
dreaming.cpp: In function 'void calcDiam(int)':
dreaming.cpp:46:23: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
46 | path.clear(); dfs2(a,-1,b);
| ~~~~^~~~~~~~
dreaming.cpp:46:23: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15040 KB |
Output is correct |
2 |
Correct |
53 ms |
14964 KB |
Output is correct |
3 |
Correct |
31 ms |
10664 KB |
Output is correct |
4 |
Correct |
7 ms |
4436 KB |
Output is correct |
5 |
Correct |
9 ms |
3540 KB |
Output is correct |
6 |
Correct |
16 ms |
5340 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
26 ms |
6680 KB |
Output is correct |
9 |
Correct |
31 ms |
8664 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
48 ms |
10264 KB |
Output is correct |
12 |
Correct |
71 ms |
12780 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2608 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15040 KB |
Output is correct |
2 |
Correct |
53 ms |
14964 KB |
Output is correct |
3 |
Correct |
31 ms |
10664 KB |
Output is correct |
4 |
Correct |
7 ms |
4436 KB |
Output is correct |
5 |
Correct |
9 ms |
3540 KB |
Output is correct |
6 |
Correct |
16 ms |
5340 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
26 ms |
6680 KB |
Output is correct |
9 |
Correct |
31 ms |
8664 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
48 ms |
10264 KB |
Output is correct |
12 |
Correct |
71 ms |
12780 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2680 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2608 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
1 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
2 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
51 ms |
15064 KB |
Output is correct |
40 |
Correct |
45 ms |
15028 KB |
Output is correct |
41 |
Correct |
33 ms |
10772 KB |
Output is correct |
42 |
Correct |
7 ms |
4436 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2644 KB |
Output is correct |
46 |
Correct |
1 ms |
2644 KB |
Output is correct |
47 |
Correct |
1 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2644 KB |
Output is correct |
49 |
Correct |
1 ms |
2644 KB |
Output is correct |
50 |
Correct |
2 ms |
2644 KB |
Output is correct |
51 |
Correct |
1 ms |
2644 KB |
Output is correct |
52 |
Correct |
2 ms |
2644 KB |
Output is correct |
53 |
Correct |
2 ms |
2644 KB |
Output is correct |
54 |
Correct |
2 ms |
2644 KB |
Output is correct |
55 |
Correct |
1 ms |
2644 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
62 ms |
8484 KB |
Output is correct |
58 |
Correct |
57 ms |
8588 KB |
Output is correct |
59 |
Correct |
77 ms |
8496 KB |
Output is correct |
60 |
Correct |
72 ms |
8448 KB |
Output is correct |
61 |
Correct |
78 ms |
8320 KB |
Output is correct |
62 |
Correct |
74 ms |
8332 KB |
Output is correct |
63 |
Correct |
64 ms |
8188 KB |
Output is correct |
64 |
Correct |
57 ms |
8260 KB |
Output is correct |
65 |
Correct |
55 ms |
8248 KB |
Output is correct |
66 |
Correct |
56 ms |
8272 KB |
Output is correct |
67 |
Correct |
63 ms |
8504 KB |
Output is correct |
68 |
Correct |
54 ms |
8524 KB |
Output is correct |
69 |
Correct |
57 ms |
8472 KB |
Output is correct |
70 |
Correct |
73 ms |
8524 KB |
Output is correct |
71 |
Correct |
1 ms |
2644 KB |
Output is correct |
72 |
Correct |
3 ms |
2772 KB |
Output is correct |
73 |
Correct |
3 ms |
2772 KB |
Output is correct |
74 |
Correct |
3 ms |
2772 KB |
Output is correct |
75 |
Correct |
4 ms |
2772 KB |
Output is correct |
76 |
Correct |
3 ms |
2772 KB |
Output is correct |
77 |
Correct |
3 ms |
2772 KB |
Output is correct |
78 |
Correct |
3 ms |
2772 KB |
Output is correct |
79 |
Correct |
3 ms |
2772 KB |
Output is correct |
80 |
Correct |
70 ms |
8512 KB |
Output is correct |
81 |
Correct |
57 ms |
8508 KB |
Output is correct |
82 |
Correct |
52 ms |
8412 KB |
Output is correct |
83 |
Correct |
62 ms |
8452 KB |
Output is correct |
84 |
Correct |
3 ms |
2772 KB |
Output is correct |
85 |
Correct |
2 ms |
2772 KB |
Output is correct |
86 |
Correct |
2 ms |
2772 KB |
Output is correct |
87 |
Correct |
2 ms |
2772 KB |
Output is correct |
88 |
Correct |
3 ms |
2772 KB |
Output is correct |
89 |
Correct |
2 ms |
2772 KB |
Output is correct |
90 |
Correct |
3 ms |
2728 KB |
Output is correct |
91 |
Correct |
3 ms |
2772 KB |
Output is correct |
92 |
Correct |
3 ms |
2772 KB |
Output is correct |
93 |
Correct |
3 ms |
2772 KB |
Output is correct |
94 |
Correct |
2 ms |
2644 KB |
Output is correct |
95 |
Correct |
2 ms |
2644 KB |
Output is correct |
96 |
Correct |
2 ms |
2644 KB |
Output is correct |
97 |
Correct |
2 ms |
2644 KB |
Output is correct |
98 |
Correct |
2 ms |
2644 KB |
Output is correct |
99 |
Correct |
2 ms |
2644 KB |
Output is correct |
100 |
Correct |
2 ms |
2644 KB |
Output is correct |
101 |
Correct |
2 ms |
2644 KB |
Output is correct |
102 |
Correct |
2 ms |
2644 KB |
Output is correct |
103 |
Correct |
2 ms |
2644 KB |
Output is correct |
104 |
Correct |
7 ms |
3540 KB |
Output is correct |
105 |
Correct |
12 ms |
5436 KB |
Output is correct |
106 |
Correct |
2 ms |
2644 KB |
Output is correct |
107 |
Correct |
22 ms |
6704 KB |
Output is correct |
108 |
Correct |
29 ms |
8636 KB |
Output is correct |
109 |
Correct |
2 ms |
2644 KB |
Output is correct |
110 |
Correct |
49 ms |
10300 KB |
Output is correct |
111 |
Correct |
61 ms |
12728 KB |
Output is correct |
112 |
Correct |
2 ms |
2772 KB |
Output is correct |
113 |
Correct |
102 ms |
16144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1084 ms |
6080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2608 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
4 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15040 KB |
Output is correct |
2 |
Correct |
53 ms |
14964 KB |
Output is correct |
3 |
Correct |
31 ms |
10664 KB |
Output is correct |
4 |
Correct |
7 ms |
4436 KB |
Output is correct |
5 |
Correct |
9 ms |
3540 KB |
Output is correct |
6 |
Correct |
16 ms |
5340 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
26 ms |
6680 KB |
Output is correct |
9 |
Correct |
31 ms |
8664 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
48 ms |
10264 KB |
Output is correct |
12 |
Correct |
71 ms |
12780 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2680 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2608 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
1 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
2 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
51 ms |
15064 KB |
Output is correct |
40 |
Correct |
45 ms |
15028 KB |
Output is correct |
41 |
Correct |
33 ms |
10772 KB |
Output is correct |
42 |
Correct |
7 ms |
4436 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2644 KB |
Output is correct |
46 |
Correct |
1 ms |
2644 KB |
Output is correct |
47 |
Correct |
1 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2644 KB |
Output is correct |
49 |
Correct |
1 ms |
2644 KB |
Output is correct |
50 |
Correct |
2 ms |
2644 KB |
Output is correct |
51 |
Correct |
1 ms |
2644 KB |
Output is correct |
52 |
Correct |
2 ms |
2644 KB |
Output is correct |
53 |
Correct |
2 ms |
2644 KB |
Output is correct |
54 |
Correct |
2 ms |
2644 KB |
Output is correct |
55 |
Correct |
1 ms |
2644 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
62 ms |
8484 KB |
Output is correct |
58 |
Correct |
57 ms |
8588 KB |
Output is correct |
59 |
Correct |
77 ms |
8496 KB |
Output is correct |
60 |
Correct |
72 ms |
8448 KB |
Output is correct |
61 |
Correct |
78 ms |
8320 KB |
Output is correct |
62 |
Correct |
74 ms |
8332 KB |
Output is correct |
63 |
Correct |
64 ms |
8188 KB |
Output is correct |
64 |
Correct |
57 ms |
8260 KB |
Output is correct |
65 |
Correct |
55 ms |
8248 KB |
Output is correct |
66 |
Correct |
56 ms |
8272 KB |
Output is correct |
67 |
Correct |
63 ms |
8504 KB |
Output is correct |
68 |
Correct |
54 ms |
8524 KB |
Output is correct |
69 |
Correct |
57 ms |
8472 KB |
Output is correct |
70 |
Correct |
73 ms |
8524 KB |
Output is correct |
71 |
Correct |
1 ms |
2644 KB |
Output is correct |
72 |
Correct |
3 ms |
2772 KB |
Output is correct |
73 |
Correct |
3 ms |
2772 KB |
Output is correct |
74 |
Correct |
3 ms |
2772 KB |
Output is correct |
75 |
Correct |
4 ms |
2772 KB |
Output is correct |
76 |
Correct |
3 ms |
2772 KB |
Output is correct |
77 |
Correct |
3 ms |
2772 KB |
Output is correct |
78 |
Correct |
3 ms |
2772 KB |
Output is correct |
79 |
Correct |
3 ms |
2772 KB |
Output is correct |
80 |
Correct |
70 ms |
8512 KB |
Output is correct |
81 |
Correct |
57 ms |
8508 KB |
Output is correct |
82 |
Correct |
52 ms |
8412 KB |
Output is correct |
83 |
Correct |
62 ms |
8452 KB |
Output is correct |
84 |
Correct |
3 ms |
2772 KB |
Output is correct |
85 |
Correct |
2 ms |
2772 KB |
Output is correct |
86 |
Correct |
2 ms |
2772 KB |
Output is correct |
87 |
Correct |
2 ms |
2772 KB |
Output is correct |
88 |
Correct |
3 ms |
2772 KB |
Output is correct |
89 |
Correct |
2 ms |
2772 KB |
Output is correct |
90 |
Correct |
3 ms |
2728 KB |
Output is correct |
91 |
Correct |
3 ms |
2772 KB |
Output is correct |
92 |
Correct |
3 ms |
2772 KB |
Output is correct |
93 |
Correct |
3 ms |
2772 KB |
Output is correct |
94 |
Correct |
2 ms |
2644 KB |
Output is correct |
95 |
Correct |
2 ms |
2644 KB |
Output is correct |
96 |
Correct |
2 ms |
2644 KB |
Output is correct |
97 |
Correct |
2 ms |
2644 KB |
Output is correct |
98 |
Correct |
2 ms |
2644 KB |
Output is correct |
99 |
Correct |
2 ms |
2644 KB |
Output is correct |
100 |
Correct |
2 ms |
2644 KB |
Output is correct |
101 |
Correct |
2 ms |
2644 KB |
Output is correct |
102 |
Correct |
2 ms |
2644 KB |
Output is correct |
103 |
Correct |
2 ms |
2644 KB |
Output is correct |
104 |
Correct |
7 ms |
3540 KB |
Output is correct |
105 |
Correct |
12 ms |
5436 KB |
Output is correct |
106 |
Correct |
2 ms |
2644 KB |
Output is correct |
107 |
Correct |
22 ms |
6704 KB |
Output is correct |
108 |
Correct |
29 ms |
8636 KB |
Output is correct |
109 |
Correct |
2 ms |
2644 KB |
Output is correct |
110 |
Correct |
49 ms |
10300 KB |
Output is correct |
111 |
Correct |
61 ms |
12728 KB |
Output is correct |
112 |
Correct |
2 ms |
2772 KB |
Output is correct |
113 |
Correct |
102 ms |
16144 KB |
Output is correct |
114 |
Execution timed out |
1084 ms |
6080 KB |
Time limit exceeded |
115 |
Halted |
0 ms |
0 KB |
- |