답안 #883874

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883874 2023-12-06T10:32:18 Z chanhchuong123 공장들 (JOI14_factories) C++14
100 / 100
4525 ms 197392 KB
#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
#define task ""
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const long long INF = 1e18 + 7;
const int MAX = 500005;
int n, q;
int timer;
int h[MAX];
int fr[MAX];
int to[MAX];
int co[MAX];
int sz[MAX];
int tin[MAX];
int par[MAX];
bool rem[MAX];
long long dep[MAX];
long long dist[MAX];
int dp[20][MAX << 1];
vector<int> adj[MAX];
#define COMBINE(u, v) (h[(u)] <= h[(v)] ? (u) : (v))

void dfs(int u, int p) {
    dp[0][timer] = u;
    tin[u] = timer++;
    for (int &id: adj[u]) {
        int v = fr[id] ^ to[id] ^ u;
        if (v != p && rem[v] == 0) {
            dep[v] = dep[u] + co[id];
            h[v] = h[u] + 1;
            dfs(v, u);
            dp[0][timer++] = u;
        }
    }
}

int LCA(int u, int v) {
    int l = tin[u], r = tin[v]; if (l > r) swap(l, r);
    int k = 31 - __builtin_clz(r - l + 1);
    return COMBINE(dp[k][l], dp[k][r - (1 << k) + 1]);
}

long long DIST(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
}

int dfsSZ(int u, int p) {
    sz[u] = 1;
    for (int &id: adj[u]) {
        int v = fr[id] ^ to[id] ^ u;
        if (v != p && rem[v] == 0) {
            sz[u] += dfsSZ(v, u);
        }
    }
    return sz[u];
}

int findCentroid(int u, int p, int n) {
    for (int &id: adj[u]) {
        int v = fr[id] ^ to[id] ^ u;
        if (v != p && rem[v] == 0) {
            if ((sz[v] << 1) > n)
                return findCentroid(v, u, n);
        }
    }
    return u;
}

void solve(int u, int p) {
    int c = findCentroid(u, p, dfsSZ(u, p));
    dist[c] = INF;
    rem[c] = true;
    par[c] = p;
    for (int &id: adj[c]) {
        int v = fr[id] ^ to[id] ^ c;
        if (v != p && rem[v] == 0) {
            solve(v, c);
        }
    }
}

void Init(int _n, int _a[], int _b[], int _d[]) {
    n = _n;
    for (int i = 0; i < n - 1; ++i) {
        fr[i] = _a[i];
        to[i] = _b[i];
        co[i] = _d[i];
        adj[fr[i]].push_back(i);
        adj[to[i]].push_back(i);
    }
    dfs(0,   -1);
    for (int j = 1; (1 << j) <= timer; ++j) {
        for (int i = 0; i + (1 << j) - 1 <= timer - 1; ++i)
            dp[j][i] = COMBINE(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
    }
    solve(0, -1);
}

long long Query(int S, int X[], int T, int Y[]) {
    for (int i = 0; i < S; ++i) {
        for (int cur = X[i]; cur != -1; cur = par[cur])
                dist[cur] = min(dist[cur], DIST(X[i], cur));
    }
    long long res = INF;
    for (int i = 0; i < T; ++i) {
        for (int cur = Y[i]; cur != -1; cur = par[cur])
                res = min(res, dist[cur] + DIST(Y[i], cur));
    }
    for (int i = 0; i < S; ++i) {
        for (int cur = X[i]; cur != -1; cur = par[cur])
            dist[cur] = INF;
    }
    return res;
}

Compilation message

factories.cpp: In function 'void Init(int, int*, int*, int*)':
factories.cpp:97:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   97 |             dp[j][i] = COMBINE(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
      |                                                                  ~~^~~
factories.cpp:24:37: note: in definition of macro 'COMBINE'
   24 | #define COMBINE(u, v) (h[(u)] <= h[(v)] ? (u) : (v))
      |                                     ^
factories.cpp:97:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   97 |             dp[j][i] = COMBINE(dp[j - 1][i], dp[j - 1][i + (1 << j - 1)]);
      |                                                                  ~~^~~
factories.cpp:24:50: note: in definition of macro 'COMBINE'
   24 | #define COMBINE(u, v) (h[(u)] <= h[(v)] ? (u) : (v))
      |                                                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 62040 KB Output is correct
2 Correct 281 ms 86100 KB Output is correct
3 Correct 354 ms 86356 KB Output is correct
4 Correct 346 ms 86132 KB Output is correct
5 Correct 420 ms 86388 KB Output is correct
6 Correct 186 ms 86100 KB Output is correct
7 Correct 347 ms 86268 KB Output is correct
8 Correct 349 ms 86124 KB Output is correct
9 Correct 410 ms 86524 KB Output is correct
10 Correct 171 ms 86372 KB Output is correct
11 Correct 335 ms 86140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 62044 KB Output is correct
2 Correct 1638 ms 170508 KB Output is correct
3 Correct 2226 ms 172748 KB Output is correct
4 Correct 501 ms 171488 KB Output is correct
5 Correct 3740 ms 197248 KB Output is correct
6 Correct 2476 ms 173588 KB Output is correct
7 Correct 780 ms 116820 KB Output is correct
8 Correct 272 ms 116684 KB Output is correct
9 Correct 993 ms 119932 KB Output is correct
10 Correct 966 ms 117364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 62040 KB Output is correct
2 Correct 281 ms 86100 KB Output is correct
3 Correct 354 ms 86356 KB Output is correct
4 Correct 346 ms 86132 KB Output is correct
5 Correct 420 ms 86388 KB Output is correct
6 Correct 186 ms 86100 KB Output is correct
7 Correct 347 ms 86268 KB Output is correct
8 Correct 349 ms 86124 KB Output is correct
9 Correct 410 ms 86524 KB Output is correct
10 Correct 171 ms 86372 KB Output is correct
11 Correct 335 ms 86140 KB Output is correct
12 Correct 9 ms 62044 KB Output is correct
13 Correct 1638 ms 170508 KB Output is correct
14 Correct 2226 ms 172748 KB Output is correct
15 Correct 501 ms 171488 KB Output is correct
16 Correct 3740 ms 197248 KB Output is correct
17 Correct 2476 ms 173588 KB Output is correct
18 Correct 780 ms 116820 KB Output is correct
19 Correct 272 ms 116684 KB Output is correct
20 Correct 993 ms 119932 KB Output is correct
21 Correct 966 ms 117364 KB Output is correct
22 Correct 2637 ms 175224 KB Output is correct
23 Correct 2642 ms 176576 KB Output is correct
24 Correct 3257 ms 177384 KB Output is correct
25 Correct 3367 ms 179824 KB Output is correct
26 Correct 3272 ms 177828 KB Output is correct
27 Correct 4525 ms 197392 KB Output is correct
28 Correct 727 ms 178096 KB Output is correct
29 Correct 3127 ms 177760 KB Output is correct
30 Correct 3215 ms 176976 KB Output is correct
31 Correct 3284 ms 177524 KB Output is correct
32 Correct 891 ms 120436 KB Output is correct
33 Correct 265 ms 116744 KB Output is correct
34 Correct 561 ms 115440 KB Output is correct
35 Correct 608 ms 115280 KB Output is correct
36 Correct 816 ms 115772 KB Output is correct
37 Correct 758 ms 115768 KB Output is correct