이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5;
vector<array<long long, 2> > g[MAXN];
long long n, k;
vector<array<long long, 2> > dp(MAXN);
// dp[node][0] -- parent weight not selected
// dp[node][1] -- parent weight selected
void dfs(long long node, long long par, long long par_wt)
{
// if(k == 1) cout << "node = " << node << ", par = " << par << ", par_wt = " << par_wt << "\n";
if(g[node].size() == 1 && par != -1) // base case
{
dp[node][0] = 0;
dp[node][1] = par_wt;
return;
}
long long sum = 0;
vector<long long> diff;
for(auto [to, wt]: g[node])
{
if(to != par)
{
dfs(to, node, wt);
sum += dp[to][0];
diff.push_back(dp[to][1] - dp[to][0]);
}
}
sort(diff.begin(), diff.end());
reverse(diff.begin(), diff.end());
// if(k == 1) cout << "node = " << node << ", diff.size() = " << diff.size() << "\n";
long long children_siz = g[node].size() - 1;
// no. of children + parent <= k
if(children_siz + 1 <= k)
{
dp[node][0] = sum;
dp[node][1] = sum + par_wt;
return;
}
// if(k == 1) cout << "node has reached here\n";
// keeping edges between k - 1 children and parent
long long buffer_value = 0;
for(long long i = diff.size() - 1; i >= k - 1; i--)
{
buffer_value += diff[i];
}
dp[node][0] = sum + buffer_value;
// keeping edges between k children only
buffer_value = par_wt;
for(long long i = diff.size() - 1; i >= k; i--)
{
buffer_value += diff[i];
}
dp[node][1] = sum + buffer_value;
}
long long calculatedp()
{
for(long long i = 0; i <= n; i++)
{
dp[i][0] = 0;
dp[i][1] = 0;
}
dfs(0, -1, 0);
if(k == 1)
{
// cout << "dp values\n";
// for(int i = 0; i < n; i++)
// {
// cout << dp[i][0] << " " << dp[i][1] << "\n";
// }
}
return dp[0][1];
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W)
{
n = N;
long long sum = 0;
for(long long i = 0; i < U.size(); i++)
{
sum += W[i];
// cout << "U[i] = " << U[i] << ", V[i] = " << V[i] << ", W[i] = " << W[i] << "\n";
g[U[i]].push_back({V[i], W[i]});
g[V[i]].push_back({U[i], W[i]});
}
vector<long long> ans;
ans.push_back(sum);
for(long long i = 1; i < N; i++)
{
k = i;
ans.push_back(calculatedp());
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:83:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(long long i = 0; i < U.size(); i++)
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |