#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
struct edge {
int to, cost;
/*edge(int _to, int _cost) {
to = _to;
cost = _cost;
}*/
};
const int MAX_N = 123456;
vector<edge> adj[MAX_N]; vector<int> R;
bool visited[MAX_N];
int maxdist, one, two, point;
edge parent[MAX_N];
void dfs1(int u, int par, int val) {
visited[u] = 1;
int i, mx = 0;
if(u == par && !adj[u].size()) {point = u; return;}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(visited[v.to] || v.to == par) continue;
if(v.cost + val > mx) point = v.to, mx = v.cost + val;
dfs1(v.to, u, v.cost + val);
}
}
void dfs2(int u, int par, int val) {
visited[u] = 0;
int i, mx = 0;
if(u == par && !adj[u].size()) {
point = u; maxdist = 0; edge v;
v.cost = 0; v.to = u; parent[u] = v;
return;}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(!visited[v.to] || v.to == par) continue;
if(v.cost + val > mx) point = v.to, mx = v.cost + val, maxdist = mx;
dfs2(v.to, u, v.cost + val);
parent[v.to] = v; parent[v.to].to = u;
} visited[u] = 1;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int i, ans = 0;
for(i = 0; i < M; i++) {
edge v; v.to = B[i]; v.cost = T[i];
adj[A[i]].push_back(v); v.to = A[i];
adj[B[i]].push_back(v);
}
for(i = 0; i < N; i++) {
if(visited[i]) continue;
dfs1(i, i, 0); one = point;
dfs2(one, one, 0); two = point;
ans = max(ans, maxdist);
int able = maxdist/2, now = 0, d = 0;
for(i = two; i != one; i = parent[i].to) {
if(d + parent[i].cost > able) {
d = min(d + parent[i].cost, maxdist-d);
break;
} d += parent[i].cost;
} R.push_back(d);
} sort(R.begin(), R.end(), greater<int>());
if(!M) return 2*L;
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 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:71:31: warning: unused variable 'now' [-Wunused-variable]
int able = maxdist/2, now = 0, d = 0;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
15096 KB |
Output is correct |
2 |
Correct |
71 ms |
15096 KB |
Output is correct |
3 |
Correct |
43 ms |
11000 KB |
Output is correct |
4 |
Correct |
11 ms |
4992 KB |
Output is correct |
5 |
Correct |
10 ms |
4096 KB |
Output is correct |
6 |
Correct |
22 ms |
5888 KB |
Output is correct |
7 |
Incorrect |
5 ms |
3200 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
15096 KB |
Output is correct |
2 |
Correct |
71 ms |
15096 KB |
Output is correct |
3 |
Correct |
43 ms |
11000 KB |
Output is correct |
4 |
Correct |
11 ms |
4992 KB |
Output is correct |
5 |
Correct |
10 ms |
4096 KB |
Output is correct |
6 |
Correct |
22 ms |
5888 KB |
Output is correct |
7 |
Incorrect |
5 ms |
3200 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
15096 KB |
Output is correct |
2 |
Correct |
71 ms |
15096 KB |
Output is correct |
3 |
Correct |
43 ms |
11000 KB |
Output is correct |
4 |
Correct |
11 ms |
4992 KB |
Output is correct |
5 |
Correct |
10 ms |
4096 KB |
Output is correct |
6 |
Correct |
22 ms |
5888 KB |
Output is correct |
7 |
Incorrect |
5 ms |
3200 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
6716 KB |
Output is correct |
2 |
Correct |
26 ms |
6756 KB |
Output is correct |
3 |
Correct |
26 ms |
6780 KB |
Output is correct |
4 |
Correct |
27 ms |
6784 KB |
Output is correct |
5 |
Correct |
32 ms |
6756 KB |
Output is correct |
6 |
Correct |
31 ms |
7260 KB |
Output is correct |
7 |
Correct |
35 ms |
6876 KB |
Output is correct |
8 |
Correct |
31 ms |
6776 KB |
Output is correct |
9 |
Correct |
26 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 |
4728 KB |
Output is correct |
13 |
Correct |
8 ms |
4728 KB |
Output is correct |
14 |
Correct |
8 ms |
4728 KB |
Output is correct |
15 |
Correct |
8 ms |
4600 KB |
Output is correct |
16 |
Correct |
8 ms |
4728 KB |
Output is correct |
17 |
Correct |
8 ms |
4728 KB |
Output is correct |
18 |
Correct |
9 ms |
4728 KB |
Output is correct |
19 |
Correct |
9 ms |
4600 KB |
Output is correct |
20 |
Incorrect |
4 ms |
3200 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
15096 KB |
Output is correct |
2 |
Correct |
71 ms |
15096 KB |
Output is correct |
3 |
Correct |
43 ms |
11000 KB |
Output is correct |
4 |
Correct |
11 ms |
4992 KB |
Output is correct |
5 |
Correct |
10 ms |
4096 KB |
Output is correct |
6 |
Correct |
22 ms |
5888 KB |
Output is correct |
7 |
Incorrect |
5 ms |
3200 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
15096 KB |
Output is correct |
2 |
Correct |
71 ms |
15096 KB |
Output is correct |
3 |
Correct |
43 ms |
11000 KB |
Output is correct |
4 |
Correct |
11 ms |
4992 KB |
Output is correct |
5 |
Correct |
10 ms |
4096 KB |
Output is correct |
6 |
Correct |
22 ms |
5888 KB |
Output is correct |
7 |
Incorrect |
5 ms |
3200 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |