# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1240271 | hainam2k9 | Putovanje (COCI20_putovanje) | C++20 | 1097 ms | 19776 KiB |
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 2e5+5;
const string NAME = "";
struct Edge{
int to,cost[2];
Edge(int a, int b, int c){
to=a, cost[0]=b, cost[1]=c;
}
};
int n,depth[MAXN],par[MAXN][20],diff[MAXN],diff2[MAXN];
ll rs=0;
vector<Edge> adj[MAXN];
void dfs(int u){
for(Edge& e : adj[u]){
if(e.to==par[u][0]) continue;
depth[e.to]=depth[u]+1, par[e.to][0]=u;
for(int i = 1; i<=__lg(n); ++i)
par[e.to][i]=par[par[e.to][i-1]][i-1];
dfs(e.to);
}
}
void dfs2(int u){
for(Edge& e : adj[u]){
if(e.to==par[u][0]) continue;
diff[e.to]+=diff[u];
rs+=min(1ll*diff[e.to]*e.cost[0],1ll*e.cost[1]);
diff[e.to]+=diff2[e.to];
dfs2(e.to);
}
}
inline int lift(int x, int k){
for(int i = 0; i<=__lg(k); ++i)
if((k>>i)&1) x=par[x][i];
return x;
}
int LCA(int x, int y){
if(depth[x]!=depth[y]){
if(depth[x]<depth[y]) swap(x,y);
x=lift(x,depth[x]-depth[y]);
}
if(x==y) return x;
for(int i = __lg(n); i>=0; --i)
if(par[x][i]!=par[y][i]) x=par[x][i], y=par[y][i];
return par[x][0];
}
void upd(int l, int r){
if(l==r) return;
--diff2[r];
r=lift(r,depth[r]-depth[l]-1);
++diff[r];
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<n; ++i){
int a,b,c,d;
cin >> a >> b >> c >> d;
adj[a].pb(Edge(b,c,d)), adj[b].pb(Edge(a,c,d));
}
dfs(1);
for(int i = 2; i<=n; ++i){
int lca=LCA(i,i-1);
upd(lca,i), upd(lca,i-1);
}
dfs2(1);
cout << rs;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |