This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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... |