Submission #425608

#TimeUsernameProblemLanguageResultExecution timeMemory
425608tht2005Factories (JOI14_factories)C++17
15 / 100
8103 ms109720 KiB
/*
 *  Written by
 *       ______  _   _  ______  ____  ____  ____  ____
 *      |_    _|| |_| ||_    _||_   || __ || __ |/  _/
 *        |  |  |  _  |  |  |    / / ||  ||||  ||| |__
 *        |  |  | | | |  |  |   | |_ ||__||||__||\__  \
 *        |__|  |_| |_|  |__|   |___||____||____|/____/
 */

#include "factories.h"
#include <bits/stdc++.h>
using namespace std;

#define nmax 500050
#define ll long long
#define ii pair<int, int>

const ll inf = (ll)1e15;
int n, sz[nmax], p[nmax], r[nmax], parent[nmax][25], depth[nmax];
ll ans[nmax], sum[nmax];
vector<ii> adj[nmax];

/* // delete before submit
 * int q, s, t, x[nmax], y[nmax], a[nmax], b[nmax], d[nmax];
 */

int dfs(int u, int p = -1)
{
    sz[u] = 1;
    for(auto [C, v]: adj[u]) {
        if(v != p && !r[v]) sz[u] += dfs(v, u);
    }
    return sz[u];
}

int find_centroid(int m, int u, int p = -1)
{
    for(auto [C, v]: adj[u]) {
        if(v != p && !r[v] && 2 * sz[v] > m)
            return find_centroid(m, v, u);
    }
    return u;
}

int centroid(int u = 0)
{
    u = find_centroid(dfs(u), u);
    r[u] = 1;
    p[u] = -1;
    for(auto [C, v]: adj[u]) {
        if(!r[v]) p[centroid(v)] = u;
    }
    return u;
}

void dfs2(int u, int p = -1)
{
    for(int j = 1; j <= 19; ++j) {
        parent[u][j] = parent[u][j - 1] != -1 ? parent[parent[u][j - 1]][j - 1] : -1;
    }
    for(auto [C, v]: adj[u]) {
        if(v == p) continue;
        parent[v][0] = u;
        depth[v] = depth[u] + 1;
        sum[v] = sum[u] + C;
        dfs2(v, u);
    }
}

int lca(int x, int y)
{
    if(depth[x] > depth[y])
        swap(x, y);
    for(int j = 19; j >= 0; --j) {
        if(parent[y][j] != -1 && depth[x] <= depth[parent[y][j]])
            y = parent[y][j];
    }
    if(x == y)
        return x;
    for(int j = 19; j >= 0; --j) {
        if(parent[x][j] != parent[y][j])
            x = parent[x][j], y = parent[y][j];
    }
    return parent[x][0];
}

inline ll dist(int x, int y)
{
    return sum[x] + sum[y] - 2 * sum[lca(x, y)];
}

void Init(int N, int A[], int B[], int D[])
{
    n = N;
    for(int i = 0; i < n - 1; ++i) {
        adj[A[i]].push_back(ii(D[i], B[i]));
        adj[B[i]].push_back(ii(D[i], A[i]));
    }
    centroid();
    for(int i = 0; i < n; ++i)
    	ans[i] = inf;
    sum[0] = depth[0] = 0;
    parent[0][0] = -1;
    dfs2(0);
}

void update(int y, bool del)
{
    for(int x = y; x != -1; x = p[x]) {
        if(del) ans[x] = inf;
        else ans[x] = min(ans[x], dist(x, y));
    }
}

ll get(int y)
{
    ll res = inf;
    for(int x = y; x != -1; x = p[x])
        res = min(res, ans[x] + dist(x, y));
    return res;
}

ll Query(int S, int X[], int T, int Y[])
{
    ll res = inf;
    for(int i = 0; i < S; ++i) {
        update(X[i], 0);
    }
    for(int i = 0; i < T; ++i) {
        res = min(res, get(Y[i]));
    }
    for(int i = 0; i < S; ++i) {
        update(X[i], 1);
    }
    return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...