답안 #713607

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
713607 2023-03-22T16:00:13 Z Spade1 공장들 (JOI14_factories) C++14
0 / 100
12 ms 12756 KB
#include <bits/stdc++.h>
#include "factories.h"
//#include "grader.cpp"
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define INF 1e18
using namespace std;

const int maxN = 500050;
const int L = log2(maxN) + 10;

vector<pll> adj[maxN];
bool mark[maxN];
ll sz[maxN], mn[maxN], up[maxN], dist[maxN][L], lvl[maxN], P[maxN][L], updist[maxN][L], vs[maxN], now=-1;

void dfs2(int a, int prt, ll cur) {
    lvl[a] = lvl[prt]+1;
    P[a][0] = prt;
    updist[a][0] = cur;
    for (int i = 1; i < L; ++i) {
        P[a][i] = P[P[a][i-1]][i-1];
        updist[a][i] = updist[P[a][i-1]][i-1] + updist[a][i-1];
    }
    for (auto [b, w] : adj[a]) if (b != prt) dfs2(b, a, w);
}

long long lca(int a, int b) {
    if (lvl[a] < lvl[b]) swap(a, b);
    int j = lvl[a] - lvl[b];
    ll dis = 0;
    for (int i = 0; i < L; ++i) {
        if (j & (1<<i)) {
            dis += updist[a][i];
            a = P[a][i];
        }
    }
    if (a==b) return dis;
    for (int i = L-1; i >= 0; --i) {
        if (P[a][i] != P[b][i]) {
            dis += (updist[a][i] + updist[b][i]);
            a = P[a][i]; b = P[b][i];
        }
    }
    return dis + updist[a][0] + updist[b][0];
}

void computedist(int n) {
    for (int i = 0; i < n; ++i) {
        int cnt = 0;
        for (int j = i; j != -1; j = up[j]) {
            dist[i][cnt++] = lca(i, j);
        }
    }
}

int dfs(int i, int prt=-1) {
    sz[i] = 1;
    for (auto [j, w] : adj[i]) {
        if (j == prt || mark[j]) continue;
        sz[i] += dfs(j, i);
    }
    return sz[i];
}

int find_centroid(int i, int m, int prt=-1) {
    for (auto [j, w] : adj[i]) {
        if (j == prt || mark[j]) continue;
        if (sz[j] > m/2) return find_centroid(j, m, i);
    }
    return i;
}

void decom(int x=0, int prt=-1) {
    int cen = find_centroid(x, dfs(x));
    mark[cen] = 1;

    up[cen] = prt;
    for (auto [j, w] : adj[cen]) {
        if (mark[j]) continue;
        decom(j);
    }
}

void update(int v) {
    int cur = v, cnt = 0;
    while (cur != -1) {
        if (vs[cur] != now) {
            vs[cur] = now;
            mn[cur] = INF;
        }
        mn[cur] = min(mn[cur], dist[v][cnt]);
        cur = up[cur];
        cnt++;
    }
}

ll query(int v) {
    ll ans = INF, cur = v, cnt = 0;
    while (cur != -1) {
        if (vs[cur] != now) {
            vs[cur] = now;
            mn[cur] = INF;
        }
        ans = min(ans, mn[cur] + dist[v][cnt]);
        cur = up[cur];
        cnt++;
    }
    return ans;
}

void Init(int N, int A[], int B[], int D[]) {
    for (int i = 0; i < N-1; ++i) {
        adj[A[i]].pb({B[i], D[i]});
        adj[B[i]].pb({A[i], D[i]});
    }
    decom();
    dfs2(0, -1, 0);
    computedist(N);
}

ll Query(int S, int X[], int T, int Y[]) {
    for (int i = 0; i < S; ++i) update(X[i]);
    ll ans = INF;
    for (int i = 0; i < T; ++i) ans = min(ans, query(Y[i]));
    now++;
    return ans;
}

Compilation message

factories.cpp: In function 'void dfs2(int, int, long long int)':
factories.cpp:28:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   28 |     for (auto [b, w] : adj[a]) if (b != prt) dfs2(b, a, w);
      |               ^
factories.cpp: In function 'int dfs(int, int)':
factories.cpp:62:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   62 |     for (auto [j, w] : adj[i]) {
      |               ^
factories.cpp: In function 'int find_centroid(int, int, int)':
factories.cpp:70:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |     for (auto [j, w] : adj[i]) {
      |               ^
factories.cpp: In function 'void decom(int, int)':
factories.cpp:82:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |     for (auto [j, w] : adj[cen]) {
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 12756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 12500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 12756 KB Output isn't correct
2 Halted 0 ms 0 KB -