#include "dreaming.h"
#include <vector>
#include <algorithm>
#include <functional>
struct E
{
public:
int n, w;
bool operator < (E o) const {return w < o.w || !(o.w < w) && n < o.n;}
};
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
std::vector<std::vector<E> > a(N, std::vector<E>(0));
std::vector<bool> vis(N, 0);
for(int i = 0;i < M;++i)
a[A[i]].push_back({B[i], T[i]}), a[B[i]].push_back({A[i], T[i]});
std::function<E(int, int)> dfs = [&](int n, int p) -> E
{
vis[n] = 1;
E ret = {n, 0};
for(E x : a[n])
if(x.n != p)
{
E cmp = dfs(x.n, n);
cmp.w += x.w;
if(ret < cmp)
ret = cmp;
}
return ret;
};
std::function<int(int, int, int, int*)> get_dia = [&](int n, int p, int d, int *m) -> int
{
int md = 0;
for(E x : a[n])
if(x.n != p)
md = std::max(md, x.w + get_dia(x.n, n, d+x.w, m));
*m = std::min(*m, std::max(md, d));
return md;
};
std::vector<int> lis(0);
int ldia = 0;
for(int i = 1;i <= N;++i)
if(!vis[i])
{
E t = dfs(i, -1);
int val = N*L;
int dia = get_dia(t.n, -1, 0, &val);
lis.push_back(val);
ldia = std::max(ldia, dia);
}
std::sort(lis.begin(), lis.end(), std::greater<int>());
if(lis.size() == 1)
return ldia;
else if(lis.size() == 2)
return std::max(ldia, lis[0]+lis[1]+L);
else
return std::max({ldia, lis[0]+lis[1]+L, lis[1]+lis[2]+2*L});
return 42;
}
Compilation message
dreaming.cpp: In member function 'bool E::operator<(E) const':
dreaming.cpp:11:60: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
bool operator < (E o) const {return w < o.w || !(o.w < w) && n < o.n;}
~~~~~~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
17912 KB |
Output is correct |
2 |
Correct |
61 ms |
17912 KB |
Output is correct |
3 |
Correct |
45 ms |
11896 KB |
Output is correct |
4 |
Correct |
12 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
1840 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
17912 KB |
Output is correct |
2 |
Correct |
61 ms |
17912 KB |
Output is correct |
3 |
Correct |
45 ms |
11896 KB |
Output is correct |
4 |
Correct |
12 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
1840 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
17912 KB |
Output is correct |
2 |
Correct |
61 ms |
17912 KB |
Output is correct |
3 |
Correct |
45 ms |
11896 KB |
Output is correct |
4 |
Correct |
12 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
1840 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
5376 KB |
Output is correct |
2 |
Correct |
27 ms |
5376 KB |
Output is correct |
3 |
Correct |
31 ms |
5504 KB |
Output is correct |
4 |
Correct |
33 ms |
5632 KB |
Output is correct |
5 |
Correct |
28 ms |
5504 KB |
Output is correct |
6 |
Correct |
30 ms |
6008 KB |
Output is correct |
7 |
Correct |
29 ms |
5664 KB |
Output is correct |
8 |
Correct |
28 ms |
5376 KB |
Output is correct |
9 |
Correct |
33 ms |
5376 KB |
Output is correct |
10 |
Correct |
33 ms |
5632 KB |
Output is correct |
11 |
Runtime error |
5 ms |
384 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
17912 KB |
Output is correct |
2 |
Correct |
61 ms |
17912 KB |
Output is correct |
3 |
Correct |
45 ms |
11896 KB |
Output is correct |
4 |
Correct |
12 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
1840 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
17912 KB |
Output is correct |
2 |
Correct |
61 ms |
17912 KB |
Output is correct |
3 |
Correct |
45 ms |
11896 KB |
Output is correct |
4 |
Correct |
12 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
1840 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |