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 sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
const int N = 2e5+1;
vi edges[N],v(N),w(N),pass(N,0);
set<int> sts[N];
map<pii,int> edg;
int n;
void dfs(int node,int p) {
for (auto it : edges[node]) if (it != p) dfs(it,node);
for (auto it : edges[node]) {
if (it == p) continue;
for (auto itt : sts[it]) {
int c = 2;
c-=(itt==1 || sts[it].count(itt-1));
c-=(itt==n || sts[it].count(itt+1));
pass[edg[{node,it}]]+=c;
}
}
sts[node].insert(node);
for (auto it : edges[node]) {
if (it == p) continue;
if (sts[it].size() > sts[node].size()) sts[node].swap(sts[it]);
for (auto itt : sts[it]) sts[node].insert(itt);
}
}
void solve() {
cin >> n;
F(i,n-1) {
int a,b;
cin >> a >> b >> v[i] >> w[i];
edges[a].push_back(b);
edges[b].push_back(a);
edg[{a,b}] = edg[{b,a}] = i;
}
dfs(1,1);
int ans = 0;
F(i,n-1) {
if (pass[i]*v[i] <= w[i]) ans+=pass[i]*v[i];
else ans+=w[i];
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |