답안 #731125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
731125 2023-04-27T03:56:56 Z stevancv Cat Exercise (JOI23_ho_t4) C++14
100 / 100
379 ms 58076 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9 + 2;
int p[N], inv[N];
int par[N][18], in[N], out[N], dep[N], tsz;
vector<int> g[N];
vector<array<int, 2>> ng[N];
void Dfs(int s, int e) {
    in[s] = ++tsz;
    par[s][0] = e;
    for (int i = 1; i < 18; i++) par[s][i] = par[par[s][i - 1]][i - 1];
    for (int u : g[s]) {
        if (u == e) continue;
        dep[u] = dep[s] + 1;
        Dfs(u, s);
    }
    out[s] = tsz;
}
bool Ancestor(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
    if (Ancestor(u, v)) return u;
    if (Ancestor(v, u)) return v;
    for (int i = 17; i >= 0; i--) {
        if (par[u][i] && !Ancestor(par[u][i], v)) u = par[u][i];
    }
    return par[u][0];
}
struct Dsu {
    int link[N], mx[N];
    void Init(int n) {
        for (int i = 1; i <= n; i++) {
            link[i] = i;
            mx[i] = p[i];
        }
    }
    int Find(int x) {
        if (x == link[x]) return x;
        return link[x] = Find(link[x]);
    }
    void Unite(int x, int y) {
        x = Find(x); y = Find(y);
        if (mx[x] < mx[y]) swap(x, y);
        link[y] = x;
    }
}dsu;
ll Dfs1(int s, int e) {
    ll ret = 0;
    for (auto u : ng[s]) {
        if (u[0] == e) continue;
        smax(ret, Dfs1(u[0], s) + u[1]);
    }
    return ret;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        inv[p[i]] = i;
    }
    vector<array<int, 3>> e(n - 1);
    for (int i = 0; i < n - 1; i++) {
        cin >> e[i][1] >> e[i][2];
        e[i][0] = max(p[e[i][1]], p[e[i][2]]);
        g[e[i][1]].push_back(e[i][2]);
        g[e[i][2]].push_back(e[i][1]);
    }
    Dfs(1, 0);
    sort(e.begin(), e.end());
    dsu.Init(n);
    for (auto u : e) {
        int x = dsu.Find(u[1]);
        int y = dsu.Find(u[2]);
        x = inv[dsu.mx[x]];
        y = inv[dsu.mx[y]];
        dsu.Unite(x, y);
        int d = dep[x] + dep[y] - 2 * dep[Lca(x, y)];
        ng[x].push_back({y, d});
        ng[y].push_back({x, d});
    }
    cout << Dfs1(inv[n], 0) << en;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9652 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9652 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9652 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 8 ms 10836 KB Output is correct
19 Correct 8 ms 10940 KB Output is correct
20 Correct 9 ms 10920 KB Output is correct
21 Correct 9 ms 10968 KB Output is correct
22 Correct 9 ms 10904 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 10 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9652 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 8 ms 10836 KB Output is correct
19 Correct 8 ms 10940 KB Output is correct
20 Correct 9 ms 10920 KB Output is correct
21 Correct 9 ms 10968 KB Output is correct
22 Correct 9 ms 10904 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 10 ms 10964 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 9 ms 10836 KB Output is correct
27 Correct 9 ms 10776 KB Output is correct
28 Correct 9 ms 10708 KB Output is correct
29 Correct 9 ms 10836 KB Output is correct
30 Correct 10 ms 10580 KB Output is correct
31 Correct 10 ms 10668 KB Output is correct
32 Correct 8 ms 10580 KB Output is correct
33 Correct 9 ms 10580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9652 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 8 ms 10836 KB Output is correct
19 Correct 8 ms 10940 KB Output is correct
20 Correct 9 ms 10920 KB Output is correct
21 Correct 9 ms 10968 KB Output is correct
22 Correct 9 ms 10904 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 10 ms 10964 KB Output is correct
25 Correct 159 ms 56632 KB Output is correct
26 Correct 154 ms 57100 KB Output is correct
27 Correct 145 ms 57144 KB Output is correct
28 Correct 189 ms 57884 KB Output is correct
29 Correct 184 ms 57840 KB Output is correct
30 Correct 215 ms 58076 KB Output is correct
31 Correct 203 ms 57920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 245 ms 45688 KB Output is correct
4 Correct 219 ms 45684 KB Output is correct
5 Correct 238 ms 45720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9652 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 8 ms 10836 KB Output is correct
19 Correct 8 ms 10940 KB Output is correct
20 Correct 9 ms 10920 KB Output is correct
21 Correct 9 ms 10968 KB Output is correct
22 Correct 9 ms 10904 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 10 ms 10964 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 9 ms 10836 KB Output is correct
27 Correct 9 ms 10776 KB Output is correct
28 Correct 9 ms 10708 KB Output is correct
29 Correct 9 ms 10836 KB Output is correct
30 Correct 10 ms 10580 KB Output is correct
31 Correct 10 ms 10668 KB Output is correct
32 Correct 8 ms 10580 KB Output is correct
33 Correct 9 ms 10580 KB Output is correct
34 Correct 159 ms 56632 KB Output is correct
35 Correct 154 ms 57100 KB Output is correct
36 Correct 145 ms 57144 KB Output is correct
37 Correct 189 ms 57884 KB Output is correct
38 Correct 184 ms 57840 KB Output is correct
39 Correct 215 ms 58076 KB Output is correct
40 Correct 203 ms 57920 KB Output is correct
41 Correct 6 ms 9684 KB Output is correct
42 Correct 5 ms 9728 KB Output is correct
43 Correct 245 ms 45688 KB Output is correct
44 Correct 219 ms 45684 KB Output is correct
45 Correct 238 ms 45720 KB Output is correct
46 Correct 339 ms 51480 KB Output is correct
47 Correct 323 ms 51060 KB Output is correct
48 Correct 296 ms 51992 KB Output is correct
49 Correct 340 ms 51148 KB Output is correct
50 Correct 379 ms 45320 KB Output is correct
51 Correct 346 ms 45324 KB Output is correct
52 Correct 362 ms 45216 KB Output is correct
53 Correct 331 ms 45360 KB Output is correct