Submission #554568

#TimeUsernameProblemLanguageResultExecution timeMemory
554568Zhora_004Road Closures (APIO21_roads)C++17
36 / 100
2091 ms20176 KiB
#include "roads.h" #include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <string> #include <sstream> #include <iomanip> #include <map> #include <unordered_map> #include <stack> #include <cstdio> #include <climits> #include <tuple> #include <ctime> #include <cstring> #include <numeric> #include <functional> #include <chrono> #include <cassert> #include <bitset> #include <fstream> #define sz(a) ((int)((a).size())) using ll = long long; using namespace std; const int inf = 1e9; int n, k; vector<int> a, b, c; vector<vector<pair<int, int>>> tree; vector<vector<ll>> dp; bool task(int id) { if (id == 1) { for (int& i : a) if (i) return 0; return 1; } if (id == 2) { for (int i = 0; i < n - 1; i++) if (a[i] != i || b[i] != i + 1) return 0; return 1; } return 0; } void dfs(int u, int p, int w) { int can = k; if (p != -1) can--; dp[u][0] = 0; priority_queue<pair<ll, int>> pq; for (pair<int, int>& edge : tree[u]) { int v = edge.first, len = edge.second; if (v != p) { dfs(v, u, len); if (dp[v][1] <= dp[v][0]) dp[u][0] += dp[v][1]; else pq.push({ dp[v][1] - dp[v][0], v }); } } int cnt = 0; while (!pq.empty()) { int v = pq.top().second; pq.pop(); if (cnt < can) dp[u][0] += dp[v][0], cnt++; else dp[u][0] += dp[v][1]; } if (p != -1) { can = k; dp[u][1] = w; for (pair<int, int>& edge : tree[u]) { int v = edge.first, len = edge.second; if (v != p) { if (dp[v][1] <= dp[v][0]) dp[u][1] += dp[v][1]; else pq.push({ dp[v][1] - dp[v][0], v }); } } cnt = 0; while (!pq.empty()) { int v = pq.top().second; pq.pop(); if (cnt < can) dp[u][1] += dp[v][0], cnt++; else dp[u][1] += dp[v][1]; } } } std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) { n = N; a = U; b = V; c = W; if (task(1)) { vector<ll> res; ll sum = 0; for (int& i : c) sum += i; res.push_back(sum); sort(c.begin(), c.end()); reverse(c.begin(), c.end()); for (k = 0; k < n - 1; k++) { sum -= c[k]; res.push_back(sum); } return res; } else if (task(2)) { ll sum = 0; dp = vector<vector<ll>>(n, vector<ll>(2)); for (int i = 1; i < n; i++) { dp[i][0] = dp[i - 1][1]; dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + c[i - 1]; sum += c[i - 1]; } vector<ll> res; res.push_back(sum); res.push_back(min(dp[n - 1][0], dp[n - 1][1])); for (k = 2; k < n; k++) res.push_back(0); return res; } else { ll tmp = 0; tree = vector<vector<pair<int, int>>>(n); for (int i = 0; i < n - 1; i++) { int u, v, w; u = a[i], v = b[i], w = c[i]; tree[u].push_back({ v, w }); tree[v].push_back({ u, w }); tmp += w; } vector<ll> res; res.push_back(tmp); for (k = 1; k < n; k++) { dp = vector<vector<ll>>(n, vector<ll>(2)); dfs(0, -1, -1); res.push_back(dp[0][0]); } return res; } }

Compilation message (stderr)

roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:82:24: warning: unused variable 'len' [-Wunused-variable]
   82 |    int v = edge.first, len = edge.second;
      |                        ^~~
#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...