#include <bits/stdc++.h>
#include "dreaming.h"
#define edge pair<int, int>
#define to first
#define cost second
using namespace std;
const int MAX_N = 123456;
vector<edge> adj[MAX_N]; vector<int> R;
bool visited[MAX_N];
int maxdist, point;
edge parent[MAX_N];
void dfs1(int u, int par, int val) {
int i, mx = 0;
if(val > maxdist) {
maxdist = val;
point = u;
}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(v.to == par) continue;
dfs1(v.to, u, v.cost + val);
}
}
void dfs2(int u, int par, int val) {
visited[u] = 1;
int i, mx = 0;
if(val > maxdist) {
maxdist = val;
point = u;
}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(v.to == par) continue;
parent[v.to] = edge(u, v.cost);
dfs2(v.to, u, val+v.cost);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int i, ans = 0, j;
for(i = 0; i < M; i++) {
adj[A[i]].push_back(edge(B[i], T[i]));
adj[B[i]].push_back(edge(A[i], T[i]));
}
for(i = 0; i < N; i++) {
if(visited[i]) continue;
maxdist = -1; dfs1(i, i, 0); int one = point;
maxdist = -1; dfs2(one, one, 0); int two = point;
ans = max(ans, maxdist);
int able = maxdist/2, d = 0;
for(j = two; j != one; j = parent[i].to) {
if(d + parent[j].cost > able) {
d = min(d + parent[j].cost, maxdist-d);
break;
} d += parent[j].cost;
} R.push_back(d);
} sort(R.begin(), R.end(), greater<int>());
if(R.size() > 1) ans = max(ans, R[0]+L+R[1]);
if(R.size() > 2) ans = max(ans, R[1]+L+L+R[2]);
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs1(int, int, int)':
dreaming.cpp:16:12: warning: unused variable 'mx' [-Wunused-variable]
int i, mx = 0;
^~
dreaming.cpp: In function 'void dfs2(int, int, int)':
dreaming.cpp:33:12: warning: unused variable 'mx' [-Wunused-variable]
int i, mx = 0;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
12280 KB |
Output is correct |
2 |
Correct |
65 ms |
12272 KB |
Output is correct |
3 |
Correct |
40 ms |
9208 KB |
Output is correct |
4 |
Correct |
11 ms |
4608 KB |
Output is correct |
5 |
Correct |
10 ms |
3968 KB |
Output is correct |
6 |
Correct |
17 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
3328 KB |
Output is correct |
8 |
Correct |
29 ms |
6520 KB |
Output is correct |
9 |
Correct |
34 ms |
7680 KB |
Output is correct |
10 |
Correct |
4 ms |
3328 KB |
Output is correct |
11 |
Correct |
51 ms |
9188 KB |
Output is correct |
12 |
Correct |
55 ms |
10616 KB |
Output is correct |
13 |
Incorrect |
5 ms |
3328 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
12280 KB |
Output is correct |
2 |
Correct |
65 ms |
12272 KB |
Output is correct |
3 |
Correct |
40 ms |
9208 KB |
Output is correct |
4 |
Correct |
11 ms |
4608 KB |
Output is correct |
5 |
Correct |
10 ms |
3968 KB |
Output is correct |
6 |
Correct |
17 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
3328 KB |
Output is correct |
8 |
Correct |
29 ms |
6520 KB |
Output is correct |
9 |
Correct |
34 ms |
7680 KB |
Output is correct |
10 |
Correct |
4 ms |
3328 KB |
Output is correct |
11 |
Correct |
51 ms |
9188 KB |
Output is correct |
12 |
Correct |
55 ms |
10616 KB |
Output is correct |
13 |
Incorrect |
5 ms |
3328 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
12280 KB |
Output is correct |
2 |
Correct |
65 ms |
12272 KB |
Output is correct |
3 |
Correct |
40 ms |
9208 KB |
Output is correct |
4 |
Correct |
11 ms |
4608 KB |
Output is correct |
5 |
Correct |
10 ms |
3968 KB |
Output is correct |
6 |
Correct |
17 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
3328 KB |
Output is correct |
8 |
Correct |
29 ms |
6520 KB |
Output is correct |
9 |
Correct |
34 ms |
7680 KB |
Output is correct |
10 |
Correct |
4 ms |
3328 KB |
Output is correct |
11 |
Correct |
51 ms |
9188 KB |
Output is correct |
12 |
Correct |
55 ms |
10616 KB |
Output is correct |
13 |
Incorrect |
5 ms |
3328 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
6788 KB |
Output is correct |
2 |
Correct |
29 ms |
6780 KB |
Output is correct |
3 |
Correct |
30 ms |
6784 KB |
Output is correct |
4 |
Correct |
29 ms |
6784 KB |
Output is correct |
5 |
Correct |
25 ms |
6784 KB |
Output is correct |
6 |
Correct |
37 ms |
7216 KB |
Output is correct |
7 |
Correct |
33 ms |
6904 KB |
Output is correct |
8 |
Correct |
27 ms |
6784 KB |
Output is correct |
9 |
Correct |
27 ms |
6652 KB |
Output is correct |
10 |
Correct |
28 ms |
6900 KB |
Output is correct |
11 |
Correct |
4 ms |
3200 KB |
Output is correct |
12 |
Correct |
8 ms |
4476 KB |
Output is correct |
13 |
Correct |
7 ms |
4476 KB |
Output is correct |
14 |
Correct |
8 ms |
4476 KB |
Output is correct |
15 |
Correct |
8 ms |
4476 KB |
Output is correct |
16 |
Correct |
8 ms |
4476 KB |
Output is correct |
17 |
Correct |
7 ms |
4092 KB |
Output is correct |
18 |
Correct |
8 ms |
4604 KB |
Output is correct |
19 |
Correct |
8 ms |
4476 KB |
Output is correct |
20 |
Correct |
4 ms |
3200 KB |
Output is correct |
21 |
Correct |
4 ms |
3200 KB |
Output is correct |
22 |
Correct |
5 ms |
3300 KB |
Output is correct |
23 |
Correct |
7 ms |
4476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
12280 KB |
Output is correct |
2 |
Correct |
65 ms |
12272 KB |
Output is correct |
3 |
Correct |
40 ms |
9208 KB |
Output is correct |
4 |
Correct |
11 ms |
4608 KB |
Output is correct |
5 |
Correct |
10 ms |
3968 KB |
Output is correct |
6 |
Correct |
17 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
3328 KB |
Output is correct |
8 |
Correct |
29 ms |
6520 KB |
Output is correct |
9 |
Correct |
34 ms |
7680 KB |
Output is correct |
10 |
Correct |
4 ms |
3328 KB |
Output is correct |
11 |
Correct |
51 ms |
9188 KB |
Output is correct |
12 |
Correct |
55 ms |
10616 KB |
Output is correct |
13 |
Incorrect |
5 ms |
3328 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
12280 KB |
Output is correct |
2 |
Correct |
65 ms |
12272 KB |
Output is correct |
3 |
Correct |
40 ms |
9208 KB |
Output is correct |
4 |
Correct |
11 ms |
4608 KB |
Output is correct |
5 |
Correct |
10 ms |
3968 KB |
Output is correct |
6 |
Correct |
17 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
3328 KB |
Output is correct |
8 |
Correct |
29 ms |
6520 KB |
Output is correct |
9 |
Correct |
34 ms |
7680 KB |
Output is correct |
10 |
Correct |
4 ms |
3328 KB |
Output is correct |
11 |
Correct |
51 ms |
9188 KB |
Output is correct |
12 |
Correct |
55 ms |
10616 KB |
Output is correct |
13 |
Incorrect |
5 ms |
3328 KB |
Output isn't correct |