#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> ii;
int ans, far;
vector<int> dis, sz;
vector<vector<ii>> adj;
void dfs(int cur, int par) {
if (dis[cur] > dis[far]) far = cur;
sz[cur] = 1;
ans = max(ans,dis[cur]);
for (auto nx : adj[cur]) {
if (nx.fi == par) continue;
dis[nx.fi] = dis[cur]+nx.se;
dfs(nx.fi,cur);
sz[cur] += sz[nx.fi];
}
}
int centroid(int cur, int par, int sub) {
for (auto nx : adj[cur]) {
if (nx.fi == par) continue;
if (sz[nx.fi]*2 > sub) return centroid(nx.fi,cur,sub);
}
return cur;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj = vector<vector<ii>>(N);
for (int i = 0; i < M; i++) {
adj[A[i]].push_back(ii(B[i],T[i]));
adj[B[i]].push_back(ii(A[i],T[i]));
}
dis = sz = vector<int>(N,0);
vector<int> com;
for (int i = 0; i < N; i++) {
if (!sz[i]) {
far = i;
dfs(i,-1);
dis[far] = 0; dfs(far,-1);
dis[i] = 0; dfs(i,-1);
int cent = centroid(i,-1,sz[i]);
dis[cent] = 0; dfs(cent,-1);
com.push_back(dis[far]);
// cout << dis[far] << " ";
}
}
// cout << '\n';
sort(com.rbegin(),com.rend());
if (com.size() >= 2) ans = max(ans,com[0]+com[1]+L);
if (com.size() >= 3) ans = max(ans,com[1]+com[2]+2*L);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
12740 KB |
Output is correct |
2 |
Correct |
72 ms |
13976 KB |
Output is correct |
3 |
Correct |
47 ms |
9420 KB |
Output is correct |
4 |
Correct |
11 ms |
2308 KB |
Output is correct |
5 |
Correct |
7 ms |
1484 KB |
Output is correct |
6 |
Correct |
16 ms |
3276 KB |
Output is correct |
7 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
12740 KB |
Output is correct |
2 |
Correct |
72 ms |
13976 KB |
Output is correct |
3 |
Correct |
47 ms |
9420 KB |
Output is correct |
4 |
Correct |
11 ms |
2308 KB |
Output is correct |
5 |
Correct |
7 ms |
1484 KB |
Output is correct |
6 |
Correct |
16 ms |
3276 KB |
Output is correct |
7 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
5832 KB |
Output is correct |
2 |
Correct |
27 ms |
5980 KB |
Output is correct |
3 |
Correct |
33 ms |
5908 KB |
Output is correct |
4 |
Correct |
45 ms |
5956 KB |
Output is correct |
5 |
Correct |
25 ms |
5952 KB |
Output is correct |
6 |
Correct |
29 ms |
6472 KB |
Output is correct |
7 |
Correct |
26 ms |
6204 KB |
Output is correct |
8 |
Correct |
28 ms |
5856 KB |
Output is correct |
9 |
Correct |
31 ms |
5740 KB |
Output is correct |
10 |
Correct |
37 ms |
6108 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
4040 KB |
Output is correct |
13 |
Correct |
9 ms |
4076 KB |
Output is correct |
14 |
Correct |
8 ms |
4040 KB |
Output is correct |
15 |
Correct |
8 ms |
4012 KB |
Output is correct |
16 |
Correct |
8 ms |
4040 KB |
Output is correct |
17 |
Correct |
8 ms |
4040 KB |
Output is correct |
18 |
Correct |
8 ms |
4040 KB |
Output is correct |
19 |
Correct |
8 ms |
4040 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
8 ms |
4040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
12740 KB |
Output is correct |
2 |
Correct |
72 ms |
13976 KB |
Output is correct |
3 |
Correct |
47 ms |
9420 KB |
Output is correct |
4 |
Correct |
11 ms |
2308 KB |
Output is correct |
5 |
Correct |
7 ms |
1484 KB |
Output is correct |
6 |
Correct |
16 ms |
3276 KB |
Output is correct |
7 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |