이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
const long long inf = 1e18;
int n, m;
vector< pair<int,int> > G[N];
long long dp[N], tmp[N], dist[N];
int tin[N], tout[N], TIME;
// leaf: n + 1 -> n + m
bool inside(int fixed, int u) {
return (tin[fixed] >= tin[u] && tin[fixed] <= tout[u]);
}
void dfs(int u) {
tin[u] = ++TIME;
for (auto e : G[u]) {
int v = e.second, c = e.first;
dist[v] = dist[u] + c;
dfs(v);
}
tout[u] = TIME;
}
void solve(int u) {
for (auto e : G[u]) {
int v = e.second, c = e.first;
solve(v);
}
for (int fixed = n + 1; fixed <= n + m; ++fixed) tmp[fixed] = 0;
for (int fixed = n + 1; fixed <= n + m; ++fixed) {
if (!inside(fixed, u)) continue;
for (auto e : G[u]) {
int v = e.second;
long long mn = inf;
for (int leaf = n + 1; leaf <= n + m; ++leaf) {
if (!inside(leaf, v)) continue;
long long cur = dp[leaf] + abs(dist[leaf] - dist[fixed]);
mn = min(mn, cur);
}
tmp[fixed] += mn;
}
}
for (int fixed = n + 1; fixed <= n + m; ++fixed) {
if (inside(fixed, u)) dp[fixed] = tmp[fixed];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
int nNode = n + m;
for (int i = 2; i <= nNode; ++i) {
int p, c; cin >> p >> c;
G[p].push_back(make_pair(c, i));
}
dfs(1);
solve(1);
long long ans = inf;
for (int fixed = n + 1; fixed <= n + m; ++fixed) {
ans = min(ans, dp[fixed]);
}
cout << ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
fireworks.cpp: In function 'void solve(int)':
fireworks.cpp:30:21: warning: unused variable 'c' [-Wunused-variable]
int v = e.second, c = e.first;
^
# | 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... |