이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 200000 + 10;
const int INF = 1e9;
int n, q;
struct Edge
{
int u, a, b;
};
llong sumUp;
llong sumOfAll;
llong answer[MAXN];
std::vector <Edge> g[MAXN];
int sz[MAXN];
void sumUpDFS(int node, int par)
{
sz[node] = 1;
for (const auto &[u, a, b] : g[node])
{
if (u == par)
{
continue;
}
sumUp += b;
sumUpDFS(u, node);
sz[node] += sz[u];
}
}
void findOneAnswer(int node, int par, llong sum)
{
answer[1] = std::max(answer[1], sum);
for (const auto &[u, a, b] : g[node])
{
if (u == par)
{
continue;
}
findOneAnswer(u, node, sum + a - b);
}
}
void solve()
{
sumUpDFS(1, 0);
findOneAnswer(1, 0, sumUp);
}
void input()
{
std::cin >> n;
for (int i = 1 ; i < n ; ++i)
{
int u, v, a, b;
std::cin >> u >> v >> a >> b;
g[u].push_back({v, a, b});
g[v].push_back({u, b, a});
sumOfAll += a;
sumOfAll += b;
}
std::cin >> q;
}
void print()
{
for (int i = 1 ; i <= q ; ++i)
{
int cnt;
std::cin >> cnt;
std::cout << sumOfAll - answer[cnt] << '\n';
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
print();
return 0;
}
# | 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... |