답안 #713610

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
713610 2023-03-22T16:08:36 Z Spade1 공장들 (JOI14_factories) C++14
100 / 100
6546 ms 322864 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) + 1;

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) {
    if (mark[i]) return 0;
    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]) decom(j, cen);
    }
}

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:63:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   63 |     for (auto [j, w] : adj[i]) {
      |               ^
factories.cpp: In function 'int find_centroid(int, int, int)':
factories.cpp:71:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   71 |     for (auto [j, w] : adj[i]) {
      |               ^
factories.cpp: In function 'void decom(int, int)':
factories.cpp:83:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   83 |     for (auto [j, w] : adj[cen]) {
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12628 KB Output is correct
2 Correct 254 ms 22876 KB Output is correct
3 Correct 256 ms 22832 KB Output is correct
4 Correct 269 ms 22904 KB Output is correct
5 Correct 275 ms 23168 KB Output is correct
6 Correct 178 ms 22812 KB Output is correct
7 Correct 266 ms 22844 KB Output is correct
8 Correct 271 ms 22824 KB Output is correct
9 Correct 277 ms 23224 KB Output is correct
10 Correct 192 ms 22824 KB Output is correct
11 Correct 280 ms 22888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12448 KB Output is correct
2 Correct 2391 ms 293760 KB Output is correct
3 Correct 4638 ms 296752 KB Output is correct
4 Correct 762 ms 291352 KB Output is correct
5 Correct 6179 ms 321392 KB Output is correct
6 Correct 4765 ms 298208 KB Output is correct
7 Correct 1000 ms 77092 KB Output is correct
8 Correct 381 ms 76852 KB Output is correct
9 Correct 1204 ms 80980 KB Output is correct
10 Correct 976 ms 78332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12628 KB Output is correct
2 Correct 254 ms 22876 KB Output is correct
3 Correct 256 ms 22832 KB Output is correct
4 Correct 269 ms 22904 KB Output is correct
5 Correct 275 ms 23168 KB Output is correct
6 Correct 178 ms 22812 KB Output is correct
7 Correct 266 ms 22844 KB Output is correct
8 Correct 271 ms 22824 KB Output is correct
9 Correct 277 ms 23224 KB Output is correct
10 Correct 192 ms 22824 KB Output is correct
11 Correct 280 ms 22888 KB Output is correct
12 Correct 10 ms 12448 KB Output is correct
13 Correct 2391 ms 293760 KB Output is correct
14 Correct 4638 ms 296752 KB Output is correct
15 Correct 762 ms 291352 KB Output is correct
16 Correct 6179 ms 321392 KB Output is correct
17 Correct 4765 ms 298208 KB Output is correct
18 Correct 1000 ms 77092 KB Output is correct
19 Correct 381 ms 76852 KB Output is correct
20 Correct 1204 ms 80980 KB Output is correct
21 Correct 976 ms 78332 KB Output is correct
22 Correct 2612 ms 295476 KB Output is correct
23 Correct 2705 ms 297820 KB Output is correct
24 Correct 5019 ms 298564 KB Output is correct
25 Correct 4928 ms 302164 KB Output is correct
26 Correct 4921 ms 298696 KB Output is correct
27 Correct 6546 ms 319648 KB Output is correct
28 Correct 908 ms 320120 KB Output is correct
29 Correct 5072 ms 322864 KB Output is correct
30 Correct 5677 ms 322340 KB Output is correct
31 Correct 5003 ms 322656 KB Output is correct
32 Correct 1150 ms 95840 KB Output is correct
33 Correct 373 ms 91308 KB Output is correct
34 Correct 592 ms 88500 KB Output is correct
35 Correct 625 ms 88572 KB Output is correct
36 Correct 905 ms 89452 KB Output is correct
37 Correct 857 ms 89204 KB Output is correct