Submission #1240282

#TimeUsernameProblemLanguageResultExecution timeMemory
1240282hainam2k9Putovanje (COCI20_putovanje)C++20
110 / 110
74 ms22600 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];
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;
        dfs2(e.to);
        rs+=min(1ll*diff[e.to]*e.cost[0],1ll*e.cost[1]);
        diff[u]+=diff[e.to];
    }
}
inline int lift(int x, int k){
    for(int i = 0; i<=__lg(n); ++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;
    --diff[l], ++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)

putovanje.cpp: In function 'int main()':
putovanje.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
putovanje.cpp:70:45: note: in expansion of macro 'fo'
   70 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
putovanje.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
putovanje.cpp:70:45: note: in expansion of macro 'fo'
   70 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...