#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
std::vector<std::list<pair<int, int>>> adj_list;
int max_length_path = 0;
void dfs(int root, std::vector<int>& parent, std::vector<std::vector<int>>& pre_long, std::vector<std::vector<int>>& post_long) {
int child = 0;
for(auto& i : adj_list[root])
if(i.first!=parent[root]) {
parent[i.first] = root;
dfs(i.first, parent, pre_long, post_long);
if(child>0)
pre_long[root][child] = max(pre_long[root][child-1], i.second + (adj_list[i.first].size()>1?pre_long[i.first][0]:0));
else
pre_long[root][child] = i.second + (adj_list[i.first].size()>1?pre_long[i.first][0]:0);
post_long[root][child] = i.second + (adj_list[i.first].size()>1?pre_long[i.first][0]:0);
child++;
}
return;
}
int findLongestDistances(int root, std::vector<int>& parent, std::vector<std::vector<int>>& pre_long, std::vector<std::vector<int>>& post_long) {
int child = 0;
for(int i = post_long[root].size()-2; i>=0; i--)
post_long[root][i] = max(post_long[root][i], post_long[root][i+1]);
int min_dist = (adj_list[root].size()>0)?post_long[root][0]:0;
max_length_path = max(max_length_path, min_dist);
for(auto& i : adj_list[root])
if(i.first!=parent[root]) {
post_long[i.first][adj_list[i.first].size()-1] = max(child>0?pre_long[root][child-1]:0, child<(adj_list[root].size()-1)?post_long[root][child+1]:0) + i.second;
if(adj_list[i.first].size()>1)
pre_long[i.first][adj_list[i.first].size()-1] = max(pre_long[i.first][adj_list[i.first].size()-2], post_long[i.first][adj_list[i.first].size()-1]);
else
pre_long[i.first][adj_list[i.first].size()-1] = post_long[i.first][adj_list[i.first].size()-1];
min_dist = min(min_dist, findLongestDistances(i.first, parent, pre_long, post_long));
child++;
}
return min_dist;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj_list.assign(N, std::list<pair<int, int>> ());
max_length_path = 0;
std::vector<std::vector<int>> pre_long(N, std::vector<int> ()), post_long(N, std::vector<int> ());
std::vector<int> parent(N, -1), trees;
for(int i = 0; i<M; i++) {
adj_list[A[i]].push_back({B[i], T[i]});
adj_list[B[i]].push_back({A[i], T[i]});
}
for(int i = 0; i<N; i++) {
pre_long[i].resize(adj_list[i].size());
post_long[i].resize(adj_list[i].size());
}
for(int i = 0; i<N; i++) {
if(parent[i]==-1) {
dfs(i, parent, pre_long, post_long);
int dist = findLongestDistances(i, parent, pre_long, post_long);
trees.push_back(dist);
}
}
sort(trees.begin(), trees.end());
int i = trees.size()-1;
if(trees.size()>1) {
max_length_path = max(max_length_path, trees[i-1]+trees[i]+L);
if(trees.size()>2)
max_length_path = max(max_length_path, trees[i-1]+trees[i-2]+2*L);
}
return max_length_path;
}
Compilation message
dreaming.cpp: In function 'int findLongestDistances(int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
dreaming.cpp:33:98: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::list<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | post_long[i.first][adj_list[i.first].size()-1] = max(child>0?pre_long[root][child-1]:0, child<(adj_list[root].size()-1)?post_long[root][child+1]:0) + i.second;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
25536 KB |
Output is correct |
2 |
Correct |
109 ms |
25136 KB |
Output is correct |
3 |
Correct |
64 ms |
18596 KB |
Output is correct |
4 |
Correct |
11 ms |
3916 KB |
Output is correct |
5 |
Correct |
9 ms |
2736 KB |
Output is correct |
6 |
Correct |
19 ms |
5896 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
47 ms |
11076 KB |
Output is correct |
9 |
Correct |
62 ms |
15216 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
99 ms |
19348 KB |
Output is correct |
12 |
Correct |
131 ms |
22212 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
25536 KB |
Output is correct |
2 |
Correct |
109 ms |
25136 KB |
Output is correct |
3 |
Correct |
64 ms |
18596 KB |
Output is correct |
4 |
Correct |
11 ms |
3916 KB |
Output is correct |
5 |
Correct |
9 ms |
2736 KB |
Output is correct |
6 |
Correct |
19 ms |
5896 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
47 ms |
11076 KB |
Output is correct |
9 |
Correct |
62 ms |
15216 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
99 ms |
19348 KB |
Output is correct |
12 |
Correct |
131 ms |
22212 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
14260 KB |
Output is correct |
2 |
Correct |
46 ms |
14352 KB |
Output is correct |
3 |
Correct |
45 ms |
14264 KB |
Output is correct |
4 |
Correct |
46 ms |
14312 KB |
Output is correct |
5 |
Correct |
55 ms |
14236 KB |
Output is correct |
6 |
Correct |
50 ms |
15280 KB |
Output is correct |
7 |
Correct |
49 ms |
14916 KB |
Output is correct |
8 |
Correct |
44 ms |
14024 KB |
Output is correct |
9 |
Correct |
44 ms |
13892 KB |
Output is correct |
10 |
Correct |
47 ms |
14772 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
8392 KB |
Output is correct |
13 |
Correct |
10 ms |
8500 KB |
Output is correct |
14 |
Correct |
10 ms |
8392 KB |
Output is correct |
15 |
Correct |
11 ms |
8396 KB |
Output is correct |
16 |
Correct |
9 ms |
8364 KB |
Output is correct |
17 |
Correct |
11 ms |
8520 KB |
Output is correct |
18 |
Correct |
12 ms |
8520 KB |
Output is correct |
19 |
Correct |
9 ms |
8376 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 |
460 KB |
Output is correct |
23 |
Correct |
9 ms |
8392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
25536 KB |
Output is correct |
2 |
Correct |
109 ms |
25136 KB |
Output is correct |
3 |
Correct |
64 ms |
18596 KB |
Output is correct |
4 |
Correct |
11 ms |
3916 KB |
Output is correct |
5 |
Correct |
9 ms |
2736 KB |
Output is correct |
6 |
Correct |
19 ms |
5896 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
47 ms |
11076 KB |
Output is correct |
9 |
Correct |
62 ms |
15216 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
99 ms |
19348 KB |
Output is correct |
12 |
Correct |
131 ms |
22212 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |