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;
int t = 1;
vi edges[N],v(N),w(N),pass(N,0),tin(N),tout(N),edg(N);
set<int> sts[N];
map<pii,int> edgs;
int n;
void gg(int node,int p) {
edg[node] = edgs[{node,p}];
tin[node] = t++;
for (auto it : edges[node]) if (it != p) gg(it,node);
tout[node] = t++;
}
bool anc(int a,int b) {
return (tin[a] <= tin[b]) && (tout[a] >= tout[b]);
}
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 || anc(it,itt-1));
c-=(itt==n || anc(it,itt+1));
pass[edg[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);
edgs[{a,b}] = edgs[{b,a}] = i;
}
gg(1,1);
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... |