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 ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define fi first
#define se second
using namespace std;
const int N = 3e5+5;
const int M = 350;
const ll mod = 1e9+9;
const ll inf = 2e9;
int m, k, t, n;
ll ans;
int a[N], b[N], c[N][2], h[N], d[N], P[N][20];
string s;
vector<int> adj[N], kq;
vector<int> val;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
ll pw(ll k, ll n)
{
ll total =1 ;
for(; n; n >>= 1)
{
if(n&1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
struct node
{
int l, r, val, lazy;
node(){}
node(int _l, int _r, int _val, int _lazy)
{
l = _l;
r = _r;
val = _val;
lazy = _lazy;
}
};
vector<node> st;
int build(int l, int r)
{
if(l == r)
{
st.pb(node(0, 0, b[l], 0));
return st.size()-1;
}
int mid = (l+r)>>1;
node cur;
cur.l = build(l, mid);
cur.r = build(mid+1, r);
cur.lazy = 0;
cur.val = min(st[cur.l].val, st[cur.r].val);
st.pb(cur);
return st.size()-1;
}
struct BIT
{
int n;
vector<int> fe;
BIT(int _n)
{
n = _n;
fe.resize(n+1, 0);
}
void add(int id, int x)
{
for(; id <= n; id += id & -id)fe[id] += x;
}
void add(int l, int r, int x)
{
if(l > r)swap(l, r);
add(l, x);
add(r, -x);
}
int get(int id)
{
int total = 0;
for(; id; id -= id & -id)total += fe[id];
return total;
}
};
void dfs(int u, int p = 0)
{
for(int i: adj[u])
{
int v = a[i]^b[i]^u;
if(v == p)continue;
h[v] = h[u]+1;
P[v][0] = u;
for(int i = 1; i < 19; i ++)P[v][i] = P[P[v][i-1]][i-1];
dfs(v, u);
}
}
int lca(int u, int v)
{
if(h[u] < h[v])swap(u, v);
int log = log2(h[u]);
for(int i = log; i >= 0; i --)if(h[u] >= h[v]+(1<<i))u = P[u][i];
if(u == v)return u;
for(int i = log; i >= 0; i --)
{
if(P[u][i] && P[v][i] != P[u][i])
{
u = P[u][i];
v = P[v][i];
}
}
return P[u][0];
}
void cal(int u, int p = 0)
{
for(int i: adj[u])
{
int v = a[i]^b[i]^u;
if(v == p)continue;
cal(v, u);
d[u] += d[v];
ans += min(c[i][1]*1ll, 1ll*d[v]*c[i][0]);
}
}
void sol(int icase)
{
cin >> n;
for(int i = 1; i < n; i ++)
{
cin >> a[i] >> b[i] >> c[i][0] >> c[i][1];
adj[a[i]].pb(i);
adj[b[i]].pb(i);
}
dfs(1);
for(int i = 1; i < n; i ++)
{
k = lca(i, i+1);
++d[i];
++d[i+1];
d[k] -= 2;
}
cal(1);
cout << ans;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
for(int i = 1; i <= test; i ++)sol(i);
return 0;
}
Compilation message (stderr)
putovanje.cpp: In function 'int main()':
putovanje.cpp:155:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
putovanje.cpp:156:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |