제출 #1201387

#제출 시각아이디문제언어결과실행 시간메모리
1201387Rasoul006자매 도시 (APIO20_swap)C++17
0 / 100
2099 ms589824 KiB
#include "swap.h"

#include <bits/stdc++.h>

#define endl "\n"

#define F first

#define S second

#define pb push_back

#define all(x) x.begin() , x.end()

#define mm(dp , val) memset (dp , val , sizeof dp)

#define mid ((r+l)>>1)

#define lx (n<<1)

#define rx ((n<<1)|1)

#define low (i&(-i))

#define lb lower_bound

#define ub upper_bound

#define no void (cout << "NO" << endl)

#define one void (cout << "-1" << endl)

#define zer void (cout << "0" << endl)

#define yes void (cout << "YES" << endl)

typedef long long ll;

using namespace std;

const int logn = 26 ;

const int N = 2e5+5;

const int mod = 1e9+7;

const long long oo = 4557430888798830399 ;

ll dx[] = {0 , 0 , 1 , -1};
ll dy[] = {1 , -1 , 0 , 0};

ll t , n , m , a[N] , lvl[N] , dp[N][logn] , s[N][logn] , mn[N][logn] ;

set <pair<ll,ll>> v[N] ;

void dfs (ll u , ll p , ll cst)
{
    lvl[u] = lvl[p]+1 ; dp[u][0] = p ; s[u][0] = cst ;

    for (auto it : v[p]) if (it.S != u && it.S != dp[p][0]) mn[u][0] = min(mn[u][0] , it.F) ;

    for (auto it : v[u]) if (it.S != p)  dfs(it.S , u , it.F);

    return ;

}

void init(int n , int m , vector<int> x , vector<int> y , vector<int> w)
{
    for (int i = 0 ; i < m ; i++)
        v[x[i]].insert({w[i],y[i]}) , v[y[i]].insert({w[i] , x[i]}) ;

    for (int i = 0 ; i <= n ; i++)
        for (int j = 0 ; j < logn ; j++)
            mn[i][j] = oo , s[i][j] = 0 , dp[i][j] = 0 ;

    dfs (0 , 0 , 0) ;

    for (int i = 0 ; i < n ; i++) v[i].insert({oo,oo}) ;

    for (int i = 1 ; i < logn ; i++)
    {
        for (int u = 0 ; u < n ; u++)
        {
            dp[u][i] = dp[dp[u][i-1]][i-1] ;
            s[u][i] = s[u][i-1] + s[dp[u][i-1]][i-1] ;
            mn[u][i] = min(mn[u][i-1] , mn[dp[u][i-1]][i-1]) ;
        }
    }
}

int getMinimumFuelCapacity(int x , int y)
{
    ll mni = oo , ans = 0 , a = x , b = y ;

    if (lvl[a] > lvl[b]) swap(a , b) , swap(x , y);

    ll dif = lvl[b] - lvl[a] ;
    for (int i = logn ; i >= 0 ; i--)
    {
        if ((1ll << i) & dif)
        {
            ans += s[b][i] ;
            mni = min(mni , mn[b][i]) ;
            b = dp[b][i] ;
        }
    }

    v[y].erase({s[y][0] , dp[y][0]}) ;
    auto it = *v[y].begin() ;
    mni = min (mni , it.F) ;
    v[y].insert({s[y][0] , dp[y][0]}) ;

    for (int i = logn-1 ; i >= 0 ; i--)
    {
        if (dp[a][i] == dp[b][i]) continue ;

        ans += s[a][i] + s[b][i] ;
        mni = min({mni , mn[a][i] , mn[b][i]}) ;
        a = dp[a][i] , b = dp[b][i] ;
    }

    if (a != b)
    {
        v[x].erase({s[x][0] , dp[x][0]}) ;
        auto it = *v[x].begin() ;
        mni = min (mni  , it.S) ;
        v[x].insert({s[x][0] , dp[x][0]}) ;

        ans += s[a][0] + s[b][0] ;

        ll lca = dp[a][0] ;
        v[lca].erase({s[a][0] , a}) ; v[lca].erase({s[b][0] , b}) ;
        it = *v[lca].begin();
        mni = min(mni , it.F) ;
        v[lca].insert({s[a][0], a}) ; v[lca].insert({s[b][0] , b}) ;
    }
    else mni = min(mni , s[a][0] + oo*(!a)) ;

    return (mni == oo ? -1 : 2*(ans + mni)) ;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...