이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=3e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll P[maxN][20],h[maxN];
vector<pli>g[maxN];
ll n,id[maxN];
void dfs(ll u=1,ll p=1)
{
P[u][0]=p;
for(int i=1;i<=18;i++) P[u][i]=P[P[u][i-1]][i-1];
for(auto vv:g[u])
{
ll v=vv.fi;
ll e=vv.se;
if(v!=p)
{
h[v]=h[u]+1;
dfs(v,u);
id[e]=v;
}
}
}
ll lca(ll u,ll v)
{
if(h[u]<h[v]) swap(u,v);
for(int i=18;i>=0;i--)if(h[u]-(1<<i)>=h[v]) u=P[u][i];
if(u==v) return u;
for(int i=18;i>=0;i--) if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i];
return P[u][0];
}
ll cnt[maxN];
void dfs1(ll u=1,ll p=1)
{
for(auto vv:g[u])
{
if(vv.fi!=p)
{
dfs1(vv.fi,u);
cnt[u]+=cnt[vv.fi];
}
}
}
ll a[maxN],b[maxN],ans=0;
void solve()
{
cin >> n;
for(int i=1;i<n;i++)
{
ll u,v;
cin >> u >> v;
g[u].pb({v,i});
g[v].pb({u,i});
cin >> a[i] >> b[i];
}
dfs();
for(int i=2;i<=n;i++)
{
ll x=lca(i,i-1);
cnt[i]++;
cnt[i-1]++;
cnt[x]-=2;
}
dfs1();
for(int i=1;i<n;i++)
{
ll u=id[i];
ans+=min(cnt[u]*a[i],b[i]);
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
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... |