이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
if (id == 5)
{
for (int& i : c) if (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];
}
}
}
void dfs5(int u, int p, vector<int>& par, vector<vector<int>>& gp)
{
par[u] = p;
for (int& v : gp[u]) if (par[v] == -2) dfs5(v, u, par, gp);
}
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 if (task(5))
{
vector<pair<int, int>> edges;
vector<int> degree(n);
vector<int> vec(n);
iota(vec.begin(), vec.end(), 0);
for (int i = 0; i < n - 1; i++)
{
int u = a[i], v = b[i];
edges.push_back({ u, v });
degree[u]++;
degree[v]++;
}
vector<ll> res;
res.push_back(n - 1);
for (int k = 1; k < n; k++)
{
int cnt = 0;
map<int, int> mp;
vector<pair<int, int>> nedges;
vector<int> nvec;
for (pair<int, int>& p : edges)
{
int u = p.first, v = p.second;
if (min(degree[u], degree[v]) > k) nedges.push_back({ u, v });
}
for (int& i : vec) if (degree[i] > k && mp.find(i) == mp.end()) mp[i] = cnt++, nvec.push_back(i);
edges = nedges;
vec = nvec;
vector<vector<int>> gp(cnt);
vector<int> deg(cnt);
for (pair<int, int>& p : edges)
{
int u = mp[p.first], v = mp[p.second];
gp[u].push_back(v);
gp[v].push_back(u);
deg[u]++;
deg[v]++;
}
vector<int> par(cnt, -2);
queue<int> q;
for (int i = 0; i < cnt; i++)
{
if (par[i] == -2) dfs5(i, -1, par, gp);
else if (deg[i] == 1) q.push(i);
}
map<int, int> uff;
for (int& i : vec) uff[i] = degree[i];
int ans = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
ans++;
int p = par[u];
deg[p]--;
if (deg[p] == 1 && par[p] != -1) q.push(p);
uff[u]--;
uff[p]--;
}
for (int& i : vec) ans += max(0, uff[i] - k);
res.push_back(ans);
}
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;
}
}
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:87:24: warning: unused variable 'len' [-Wunused-variable]
87 | int v = edge.first, len = edge.second;
| ^~~
# | 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... |