답안 #1107009

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1107009 2024-10-31T11:25:20 Z _callmelucian Cat Exercise (JOI23_ho_t4) C++14
100 / 100
163 ms 45644 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 2e5 + 5;
int up[mn][19], depth[mn], p[mn];
vector<int> adj[mn];
bool added[mn];

void dfs (int u, int p, int d) {
    up[u][0] = p, depth[u] = d;
    for (int i = 1; i < 19; i++)
        up[u][i] = up[up[u][i - 1]][i - 1];
    for (int v : adj[u])
        if (v != p) dfs(v, u, d + 1);
}

int goUp (int a, int k) {
    for (int i = 0; i < 19; i++)
        if (k & (1 << i)) a = up[a][i];
    return a;
}

int lca (int a, int b) {
    if (depth[a] < depth[b]) swap(a, b);
    a = goUp(a, depth[a] - depth[b]);
    if (a == b) return a;
    for (int i = 18; i >= 0; i--)
        if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
    return up[a][0];
}

int distance (int a, int b) {
    return depth[a] + depth[b] - 2 * depth[lca(a, b)];
}

struct DSU {
    vector<ll> lab, weight, hi;
    DSU (int sz) : lab(sz + 1, -1), weight(sz + 1), hi(sz + 1) {
        for (int i = 1; i <= sz; i++) hi[i] = i;
    }

    int get (int u) {
        if (lab[u] < 0) return u;
        return lab[u] = get(lab[u]);
    }

    void unite (int a, int b) {
        a = get(a), b = get(b);
        if (a == b) return;
        if (-lab[a] < -lab[b]) swap(a, b), weight[a] += distance(hi[a], hi[b]);
        else weight[b] += distance(hi[a], hi[b]);
        lab[a] += lab[b], lab[b] = a;
        weight[a] = max(weight[a], weight[b]);
        hi[a] = max(hi[a], hi[b]);
    }

    ll getWeight (int u) { return weight[get(u)]; }
};

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> p[i];
    for (int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        adj[p[a]].push_back(p[b]);
        adj[p[b]].push_back(p[a]);
        //cout << p[a] << " " << p[b] << "\n";
    }
    dfs(1, 1, 0);

    DSU dsu(n);
    for (int i = 1; i <= n; i++) {
        for (int j : adj[i])
            if (j < i) dsu.unite(i, j);
    }
    cout << dsu.getWeight(1);

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8600 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8600 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8684 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 2 ms 8528 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8600 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8684 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 2 ms 8528 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 4 ms 9040 KB Output is correct
19 Correct 4 ms 9040 KB Output is correct
20 Correct 6 ms 9040 KB Output is correct
21 Correct 4 ms 9040 KB Output is correct
22 Correct 4 ms 9040 KB Output is correct
23 Correct 4 ms 9208 KB Output is correct
24 Correct 3 ms 9036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8600 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8684 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 2 ms 8528 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 4 ms 9040 KB Output is correct
19 Correct 4 ms 9040 KB Output is correct
20 Correct 6 ms 9040 KB Output is correct
21 Correct 4 ms 9040 KB Output is correct
22 Correct 4 ms 9040 KB Output is correct
23 Correct 4 ms 9208 KB Output is correct
24 Correct 3 ms 9036 KB Output is correct
25 Correct 2 ms 8700 KB Output is correct
26 Correct 3 ms 9040 KB Output is correct
27 Correct 3 ms 9040 KB Output is correct
28 Correct 3 ms 9040 KB Output is correct
29 Correct 3 ms 9040 KB Output is correct
30 Correct 4 ms 8784 KB Output is correct
31 Correct 3 ms 8784 KB Output is correct
32 Correct 3 ms 8784 KB Output is correct
33 Correct 3 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8600 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8684 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 2 ms 8528 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 4 ms 9040 KB Output is correct
19 Correct 4 ms 9040 KB Output is correct
20 Correct 6 ms 9040 KB Output is correct
21 Correct 4 ms 9040 KB Output is correct
22 Correct 4 ms 9040 KB Output is correct
23 Correct 4 ms 9208 KB Output is correct
24 Correct 3 ms 9036 KB Output is correct
25 Correct 82 ms 41032 KB Output is correct
26 Correct 80 ms 45640 KB Output is correct
27 Correct 86 ms 45644 KB Output is correct
28 Correct 113 ms 44984 KB Output is correct
29 Correct 110 ms 40904 KB Output is correct
30 Correct 114 ms 44104 KB Output is correct
31 Correct 114 ms 43080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8528 KB Output is correct
3 Correct 91 ms 36168 KB Output is correct
4 Correct 88 ms 36164 KB Output is correct
5 Correct 101 ms 36176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8600 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8684 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 2 ms 8528 KB Output is correct
15 Correct 2 ms 8784 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 4 ms 9040 KB Output is correct
19 Correct 4 ms 9040 KB Output is correct
20 Correct 6 ms 9040 KB Output is correct
21 Correct 4 ms 9040 KB Output is correct
22 Correct 4 ms 9040 KB Output is correct
23 Correct 4 ms 9208 KB Output is correct
24 Correct 3 ms 9036 KB Output is correct
25 Correct 2 ms 8700 KB Output is correct
26 Correct 3 ms 9040 KB Output is correct
27 Correct 3 ms 9040 KB Output is correct
28 Correct 3 ms 9040 KB Output is correct
29 Correct 3 ms 9040 KB Output is correct
30 Correct 4 ms 8784 KB Output is correct
31 Correct 3 ms 8784 KB Output is correct
32 Correct 3 ms 8784 KB Output is correct
33 Correct 3 ms 8784 KB Output is correct
34 Correct 82 ms 41032 KB Output is correct
35 Correct 80 ms 45640 KB Output is correct
36 Correct 86 ms 45644 KB Output is correct
37 Correct 113 ms 44984 KB Output is correct
38 Correct 110 ms 40904 KB Output is correct
39 Correct 114 ms 44104 KB Output is correct
40 Correct 114 ms 43080 KB Output is correct
41 Correct 2 ms 8528 KB Output is correct
42 Correct 2 ms 8528 KB Output is correct
43 Correct 91 ms 36168 KB Output is correct
44 Correct 88 ms 36164 KB Output is correct
45 Correct 101 ms 36176 KB Output is correct
46 Correct 121 ms 42704 KB Output is correct
47 Correct 108 ms 42756 KB Output is correct
48 Correct 119 ms 42708 KB Output is correct
49 Correct 98 ms 42756 KB Output is correct
50 Correct 163 ms 36424 KB Output is correct
51 Correct 117 ms 36424 KB Output is correct
52 Correct 126 ms 36368 KB Output is correct
53 Correct 100 ms 36424 KB Output is correct