제출 #555955

#제출 시각아이디문제언어결과실행 시간메모리
555955aryan12도로 폐쇄 (APIO21_roads)C++17
0 / 100
2096 ms21760 KiB
#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()); long long gg = diff.size(); for(long long i = diff.size() - 1; i >= 0; i--) { if(diff[i] < 0) { gg = i; } } // 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 >= min(gg, 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 >= min(k, gg); 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 min(dp[0][0], 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:91:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for(long long i = 0; i < U.size(); i++)
      |                          ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...