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 <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define pii pair<int, int>
#define pip pair<int, pii>
#define f first
#define s second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define endl '\n'
const ll maxn = 3e5+5;
const ll inf = (1ll<<60);
const ll mod = 1e9+7;
int n;
vector<pip> g[maxn];
int cnt = 0, ans = inf;
void dfs(int x, int prev){
for (auto v:g[x]){
if (v.f == prev) continue;
cnt += v.s.f;
dfs(v.f, x);
}
}
void dfs2(int x, int prev){
ans = min(ans, cnt);
for (auto v:g[x]){
if (v.f == prev) continue;
cnt += v.s.s;
dfs2(v.f, x);
cnt -= v.s.s;
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin>>n;
REP(i, n-1){
int u, v, c, d; cin>>u>>v>>c>>d;
g[u].pb({v, {c, d-c}});
g[v].pb({u, {d, c-d}});
}
dfs(1, -1); dfs2(1, -1);
int q; cin>>q;
cin>>q;
cout<<ans<<endl;
}
# | 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... |