#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[100005];
long long dp[100005][2], limit;
vector<long long> ans;
void dfs(long long u, long long p, long long x)
{
for (long long i=0; i<adj[u].size(); i++)
{
long long v=adj[u][i].first, w=adj[u][i].second;
if (v==p)
continue;
dfs(v, u, w);
}
vector<long long> vec, tmp;
vec.push_back(0);
for (long long i=1; i<=limit; i++)
vec.push_back(1e18);
for (long long i=0; i<adj[u].size(); i++)
{
long long v=adj[u][i].first, w=adj[u][i].second;
if (v==p)
continue;
tmp.push_back(vec[0]+dp[v][0]);
for (int j=1; j<=limit; j++)
tmp.push_back(min(vec[j]+dp[v][0], vec[j-1]+dp[v][1]));
vec=tmp;
tmp.clear();
}
long long mn=1e18;
for (long long i=0; i<limit; i++)
mn=min(mn, vec[i]);
dp[u][0]=min(mn, vec[limit])+x;
dp[u][1]=mn;
}
vector<long long> minimum_closure_costs(int n, vector<int> u, vector<int> v, vector<int> w)
{
for (long long i=0; i<n-1; i++)
{
adj[u[i]].push_back({v[i], w[i]});
adj[v[i]].push_back({u[i], w[i]});
}
for (long long i=0; i<n; i++)
{
limit=i;
dfs(0, 0, 0);
ans.push_back(dp[0][0]);
}
return ans;
}
Compilation message
roads.cpp: In function 'void dfs(long long int, long long int, long long int)':
roads.cpp:9:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for (long long i=0; i<adj[u].size(); i++)
| ~^~~~~~~~~~~~~~
roads.cpp:20:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (long long i=0; i<adj[u].size(); i++)
| ~^~~~~~~~~~~~~~
roads.cpp:22:38: warning: unused variable 'w' [-Wunused-variable]
22 | long long v=adj[u][i].first, w=adj[u][i].second;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
2062 ms |
2816 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
2055 ms |
26696 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
13 ms |
2652 KB |
Output is correct |
5 |
Correct |
27 ms |
2644 KB |
Output is correct |
6 |
Correct |
20 ms |
2680 KB |
Output is correct |
7 |
Correct |
20 ms |
2680 KB |
Output is correct |
8 |
Correct |
26 ms |
2668 KB |
Output is correct |
9 |
Correct |
29 ms |
2680 KB |
Output is correct |
10 |
Correct |
25 ms |
2708 KB |
Output is correct |
11 |
Correct |
25 ms |
2724 KB |
Output is correct |
12 |
Correct |
29 ms |
2720 KB |
Output is correct |
13 |
Correct |
8 ms |
2656 KB |
Output is correct |
14 |
Correct |
22 ms |
2644 KB |
Output is correct |
15 |
Correct |
22 ms |
2788 KB |
Output is correct |
16 |
Correct |
6 ms |
2780 KB |
Output is correct |
17 |
Correct |
29 ms |
2644 KB |
Output is correct |
18 |
Correct |
25 ms |
2688 KB |
Output is correct |
19 |
Correct |
23 ms |
2680 KB |
Output is correct |
20 |
Correct |
28 ms |
2688 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
13 ms |
2652 KB |
Output is correct |
5 |
Correct |
27 ms |
2644 KB |
Output is correct |
6 |
Correct |
20 ms |
2680 KB |
Output is correct |
7 |
Correct |
20 ms |
2680 KB |
Output is correct |
8 |
Correct |
26 ms |
2668 KB |
Output is correct |
9 |
Correct |
29 ms |
2680 KB |
Output is correct |
10 |
Correct |
25 ms |
2708 KB |
Output is correct |
11 |
Correct |
25 ms |
2724 KB |
Output is correct |
12 |
Correct |
29 ms |
2720 KB |
Output is correct |
13 |
Correct |
8 ms |
2656 KB |
Output is correct |
14 |
Correct |
22 ms |
2644 KB |
Output is correct |
15 |
Correct |
22 ms |
2788 KB |
Output is correct |
16 |
Correct |
6 ms |
2780 KB |
Output is correct |
17 |
Correct |
29 ms |
2644 KB |
Output is correct |
18 |
Correct |
25 ms |
2688 KB |
Output is correct |
19 |
Correct |
23 ms |
2680 KB |
Output is correct |
20 |
Correct |
28 ms |
2688 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Execution timed out |
2069 ms |
2916 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2057 ms |
11824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2057 ms |
11824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
2062 ms |
2816 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |