이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <tuple>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define piii pair<int, pii>
#define plll pair<long long, pll>
#define tiii tuple<int, int, int>
#define tiiii tuple<int, int, int, int>
#define tlll tuple<long long, long long, long long>
#define tllll tuple<long long, long long, long long, long long>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss
#define DEBUG
using namespace std;
vector<tiii> gph[202020];
long long mx;
long long sz[202020];
void dfs(int x, int p)
{
for(auto [y, a, b] : gph[x]) if(y != p) dfs(y, x), sz[x] += b + sz[y];
}
void dfs2(int x, int p, long long d)
{
mx = max(mx, d);
for(auto [y, a, b] : gph[x]) if(y != p) dfs2(y, x, d + a - b);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
long long ans = 0;
for(int i = 1; i < n; ++i)
{
int x, y, a, b; cin >> x >> y >> a >> b; --x; --y;
gph[x].push_back({y, a, b});
gph[y].push_back({x, b, a});
ans += a, ans += b;
}
dfs(0, -1);
dfs2(0, -1, sz[0]);
// cout << sz[0] << endl;
int T; cin >> T;
while(T--)
{
int x; cin >> x;
cout << ans - mx;
}
}
# | 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... |