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 pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
struct Edge{
ll cost[2];
int to;
Edge(){}
Edge(int a,int b,int c){
to = a,cost[0] = b,cost[1] = c;
}
};
const int mxn = 2e5+10;
vector<Edge> tree[mxn];
ll bit[mxn];
pll val[mxn];
ll add[mxn],del[mxn];
int dep[mxn],idx[mxn],link_top[mxn],sz[mxn],par[mxn],bs[mxn],cnt;
int n;
void modify(int p,int v){
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
ll getval(int p){
ll re= 0;
for(;p>0;p-= p&-p)re += bit[p];
return re;
}
void dfs1(int now){
sz[now] = 1;
for(auto nxt:tree[now]){
if(nxt.to == par[now])continue;
par[nxt.to] = now;
dep[nxt.to] = dep[now]+1;
val[nxt.to] = {nxt.cost[0],nxt.cost[1]};
dfs1(nxt.to);
if(!bs[now]||sz[bs[now]]<sz[nxt.to])bs[now] = nxt.to;
sz[now] += sz[nxt.to];
}
return;
}
void dfs2(int now,int top){
link_top[now] = top;
idx[now] = ++cnt;
if(bs[now])dfs2(bs[now],top);
for(auto nxt:tree[now]){
if(nxt.to == bs[now]||nxt.to == par[now])continue;
dfs2(nxt.to,nxt.to);
}
return;
}
void addval(int a,int b,int c){
int ta = link_top[a],tb = link_top[b];
while(ta != tb){
if(dep[ta]<dep[tb]){
swap(ta,tb);
swap(a,b);
}
modify(idx[ta],c);
modify(idx[a]+1,-c);
a = par[ta];ta = link_top[a];
}
if(dep[a]>dep[b])swap(a,b);
if(a != b){
modify(idx[a]+1,c);
modify(idx[b]+1,-c);
}
return;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 1;i<n;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
tree[a].push_back(Edge(b,c,d));
tree[b].push_back(Edge(a,c,d));
}
par[1] = 0;
dep[1] = 1;
dfs1(1);
dfs2(1,1);
for(int i = 2;i<=n;i++){
addval(i-1,i,1);
}
ll ans = 0;
for(int i = 2;i<=n;i++){
ans += min(getval(idx[i])*val[i].fs,val[i].sc);
}
cout<<ans;
}
Compilation message (stderr)
putovanje.cpp:83:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
83 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |