Submission #1271798

#TimeUsernameProblemLanguageResultExecution timeMemory
1271798TymondTraining (IOI07_training)C++20
10 / 100
22 ms16708 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct pair_hash{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

const int MAXN = 1e3 + 7;
const int MAXM = 5e3 + 7;
const int MAXK = 11;
int parent[MAXN];
int jump[MAXK][MAXN];
vector<pair<pii, int>> vec[MAXN];
vector<pii> g[MAXN];
vi child[MAXN];
int dep[MAXN];
int tin[MAXN];
int tout[MAXN];
ll dp[MAXN][(1 << MAXK)];
ll best[MAXN];
int n, m;
int timer = 0;

void dfsInit(int v, int p, int d){
    parent[v] = p;
    dep[v] = d;
    tin[v] = timer++;
    for(auto [u, c] : g[v]){
        if(p != u){
            child[v].pb(u);
            dfsInit(u, v, d + 1);
        }
    }
    tout[v] = timer++;
}

void init(){
    for(int i = 1; i <= n; i++){
        jump[0][i] = parent[i];
    }

    for(int j = 1; j < MAXK; j++){
        for(int i = 1; i <= n; i++){
            jump[j][i] = jump[j - 1][jump[j - 1][i]];
        }
    }
}

int lca(int u, int v){
    if(dep[u] > dep[v]){
        swap(u, v);
    }

    for(int j = MAXK - 1; j >= 0; j--){
        if(dep[v] - dep[u] >= (1 << j)){
            v = jump[j][v];
        }
    }

    if(u == v){
        return u;
    }

    for(int j = MAXK - 1; j >= 0; j--){
        if(jump[j][u] != jump[j][v]){
            v = jump[j][v];
            u = jump[j][u];
        }
    }
    return parent[v];
}

int dist(int u, int v){
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}

int nxt(int v, int u){
    for(int x = 0; x < sz(child[v]); x++){
        if(tin[child[v][x]] <= tin[u] && tout[u] <= tout[child[v][x]]){
            return x;
        }
    }
}

ll calcSingle(int v, int u){
    v = child[v][nxt(v, u)];
    ll ret = 0LL;
    while(u != v){
        int k = nxt(v, u);
        ret = (ll)ret - best[v] + dp[v][(1 << MAXK) - 1 - (1 << k)];
        v = child[v][k];
    }

    return ret;
}

ll calc(int l, int u, int v){
    ll ret = 0LL;
    if(u != l){
        ret += calcSingle(l, u);
    }

    if(v != l){
        ret += calcSingle(l, v);
    }

    return ret;
}

void dfs(int v){
    for(auto c : child[v]){
        dfs(c);
    }

    vll cost(sz(vec[v]), 0LL);
    vector<pii> id(sz(vec[v]));
    for(int j = 0; j < sz(vec[v]); j++){
        cost[j] = calc(v, vec[v][j].fi.fi, vec[v][j].fi.se) + vec[v][j].se;
        if(vec[v][j].fi.fi == v){
            id[j].fi = -1;
        }else{
            id[j].fi = nxt(v, vec[v][j].fi.fi);
        }

        if(vec[v][j].fi.se == v){
            id[j].se = -1;
        }else{
            id[j].se = nxt(v, vec[v][j].fi.se);
        }

        if(id[j].fi > id[j].se){
            swap(id[j].fi, id[j].se);
        }
    }

    for(int msk = 0; msk < (1 << MAXK); msk++){
        for(int j = 0; j < sz(vec[v]); j++){
            if(id[j].fi == -1){
                if(msk & (1 << id[j].se)){
                    dp[v][msk] = max(dp[v][msk], dp[v][msk ^ (1 << id[j].se)] + cost[j]);
                }
            }else{
                if((msk & (1 << id[j].fi)) && (msk & (1 << id[j].se))){
                    dp[v][msk] = max(dp[v][msk], dp[v][msk - (1 << id[j].fi) - (1 << id[j].se)] + cost[j]);
                }
            }
        }

        for(int j = 0; j < sz(child[v]); j++){
            if((msk & (1 << j)) == 0){
                dp[v][msk] += best[child[v][j]];
            }
        }
        best[v] = max(best[v], dp[v][msk]);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    
    ll sumAll = 0LL;
    cin >> n >> m;
    vector<pair<pii, int>> e;
    for(int i = 1; i <= m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        if(c == 0){
            g[a].pb(mp(b, 0));
            g[b].pb(mp(a, 0));
        }else{
            sumAll += c;
            e.pb(mp(mp(a, b), c));
        }
    }

    dfsInit(1, 1, 0);
    init();
    for(int i = 1; i <= n; i++){
        g[i].clear();
    }

    for(auto ele : e){
        if(dist(ele.fi.fi, ele.fi.se) & 1){
            continue;
        }

        vec[lca(ele.fi.fi, ele.fi.se)].pb(ele);
    }

    dfs(1);
    cout << sumAll - best[1] << '\n';

    return 0;
}

Compilation message (stderr)

training.cpp: In function 'int nxt(int, int)':
training.cpp:123:1: warning: control reaches end of non-void function [-Wreturn-type]
  123 | }
      | ^
#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...
#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...