#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]) {
// get this component's diameter
dis[i] = 0; far = i; dfs(i,-1);
dis[far] = 0; dfs(far,-1);
// get this component's centroid
dis[i] = 0; far = i; dfs(i,-1);
int cent = centroid(i,-1,sz[i]);
dis[cent] = 0; far = cent; dfs(cent,-1);
com.push_back(dis[far]);
}
}
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 |
55 ms |
12732 KB |
Output is correct |
2 |
Correct |
60 ms |
12664 KB |
Output is correct |
3 |
Correct |
40 ms |
8556 KB |
Output is correct |
4 |
Correct |
8 ms |
2124 KB |
Output is correct |
5 |
Correct |
7 ms |
1356 KB |
Output is correct |
6 |
Correct |
14 ms |
3020 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 |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
12732 KB |
Output is correct |
2 |
Correct |
60 ms |
12664 KB |
Output is correct |
3 |
Correct |
40 ms |
8556 KB |
Output is correct |
4 |
Correct |
8 ms |
2124 KB |
Output is correct |
5 |
Correct |
7 ms |
1356 KB |
Output is correct |
6 |
Correct |
14 ms |
3020 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 |
27 ms |
5840 KB |
Output is correct |
2 |
Correct |
28 ms |
5896 KB |
Output is correct |
3 |
Correct |
25 ms |
5920 KB |
Output is correct |
4 |
Correct |
27 ms |
5904 KB |
Output is correct |
5 |
Correct |
25 ms |
5944 KB |
Output is correct |
6 |
Correct |
28 ms |
6472 KB |
Output is correct |
7 |
Correct |
26 ms |
6120 KB |
Output is correct |
8 |
Correct |
30 ms |
5816 KB |
Output is correct |
9 |
Correct |
24 ms |
5812 KB |
Output is correct |
10 |
Correct |
25 ms |
6092 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
9 ms |
4040 KB |
Output is correct |
13 |
Correct |
8 ms |
4040 KB |
Output is correct |
14 |
Correct |
8 ms |
4044 KB |
Output is correct |
15 |
Correct |
8 ms |
4040 KB |
Output is correct |
16 |
Correct |
8 ms |
4040 KB |
Output is correct |
17 |
Correct |
8 ms |
4040 KB |
Output is correct |
18 |
Correct |
9 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 |
2 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 |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
12732 KB |
Output is correct |
2 |
Correct |
60 ms |
12664 KB |
Output is correct |
3 |
Correct |
40 ms |
8556 KB |
Output is correct |
4 |
Correct |
8 ms |
2124 KB |
Output is correct |
5 |
Correct |
7 ms |
1356 KB |
Output is correct |
6 |
Correct |
14 ms |
3020 KB |
Output is correct |
7 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |