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>
#define lcm(a,b) (a/__gcd(a,b))*b
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define F first
#define S second
#define mp make_pair
//salida rapida "\n"
//DECIMALES fixed<<sp(n)<<x<<endl;
//gcd(a,b)= ax + by
//lCB x&-x
//set.erase(it) - ersases the element present at the required index//auto it = s.find(element)
//set.find(element) - iterator pointing to the given element if it is present else return pointer pointing to set.end()
//set.lower_bound(element) - iterator pointing to element greater than or equal to the given element
//set.upper_bound(element) - iterator pointing to element greater than the given element
// | ^
using namespace std;
int n,a,b,c1,c2;
vector<vector<int> >G;
map<pair<int,int>,pair<int,int> >m;
vi v;
void dfs(int u, int padre){
v.pb(u);
for(auto it : G[u]){
if(it!=padre){
dfs(it,u);
}
}
}
int main()
{
cin>>n;
G.assign(n+1,vi());
for(int i=0;i<n-1;i++){
cin>>a>>b>>c1>>c2;
G[a].pb(b);
G[b].pb(a);
m[{a,b}]={c1,c2};
m[{b,a}]={c1,c2};
}
v.pb(0);
for(int i=1;i<=n;i++){
if(G[i].size()==1){
dfs(i,i);
break;
}
}
vi pos(n+1);
for(int i=1;i<=n;i++){
pos[v[i]]=i;
}
vi dp(n+2,0);
for(int i=2;i<=n;i++){
int L=pos[i-1],R=pos[i];
if(L>R)swap(L,R);
dp[L]++;
dp[R]--;
}
int sum=0;
ll res=0;
for(int i=0;i<n;i++){
sum+=dp[i];
res+=min(m[{v[i],v[i+1]}].F*sum,m[{v[i],v[i+1]}].S);
}
cout<<res<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |