답안 #1014840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014840 2024-07-05T15:38:34 Z underwaterkillerwhale Cat Exercise (JOI23_ho_t4) C++17
100 / 100
183 ms 49272 KB
//#include "pour.h"
#include <bits/stdc++.h>
#define se              second
#define fs              first
#define mp              make_pair
#define pb              push_back
#define ll              long long
#define ii              pair<ll,ll>
#define ld              long double
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(), v.end()
#define bit(msk, i)     ((msk >> i) & 1)
#define iter(id, v)     for(auto id : v)
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r)
{
    return uniform_int_distribution<ll> (l, r)(rd);
}

const int N  = 2e5 + 7;
const int Mod = 111539786;
const int szBL = 916;
const ll INF = 1e18;
const int BASE = 137;

int n, m;
vector<int> ke[N];
int a[N];
ll dp[N];
int high[N], par[N][25];

void dfs (int u, int p) {
    high[u] = high[p] + 1;
    par[u][0] = p;
    rep (i, 1, 20) par[u][i] = par[par[u][i - 1]][i - 1];
    iter (&v, ke[u]) if (v != p) {
        dfs (v, u);
    }
}

int Lca (int u, int v) {
    if (high[v] < high[u]) swap (u, v);
    int delta = high[v] - high[u];
    rep (i, 0, 20) if (bit(delta, i)) {
        v = par[v][i];
    }
    if (v == u) return v;
    reb (i, 20, 0) {
        if (par[u][i] != par[v][i]) {
            u = par[u][i];
            v = par[v][i];
        }
    }
    return par[v][0];
}

int Dist (int u, int v) {
    return high[u] + high[v] - 2 * high[Lca(u, v)];
}


struct Disjoin_set {
    int lab[N], sz[N], mx[N];

    void init (int n) {
        rep (i, 1, n) lab[i] = i, sz[i] = 1, mx[i] = i;
    }
    int Find (int u) {
        return u == lab[u] ? u : lab[u] = Find(lab[u]);
    }
    void Join (int u, int v) {
        u = Find(u);
        v = Find(v);
        if (u == v) return;
        if (sz[u] < sz[v]) swap (u, v);
        sz[u] += sz[v];
        lab[v] = u;
        mx[u] = max(mx[u], mx[v]);
    }
}DSU;

void solution () {
    cin >> n;
    rep (i, 1, n) {
        cin >> a[i];
    }
    rep (i, 1, n - 1) {
        int u, v;
        cin >> u >> v;
        ke[a[u]].push_back(a[v]);
        ke[a[v]].push_back(a[u]);
    }
    dfs (1, 0);
    DSU.init (n);
    dp[1] = 0;
    rep (u, 1, n) {
        iter (&v, ke[u]) {
            if (u > v) {
                int mx = DSU.mx[DSU.Find(v)];
                dp[u] = max(dp[u], dp[mx] + Dist(u, mx));
                DSU.Join(u, v);
            }
        }
//        cout << dp[u] <<"\n";
    }
    cout << dp[n] <<"\n";
}

#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//    file ("c");
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
3 2
0 1 1
0 0 1
0 0 0
3 3
1 2
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5136 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5136 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 5 ms 5976 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 5 ms 5976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5136 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 5 ms 5976 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 5 ms 5976 KB Output is correct
25 Correct 2 ms 4956 KB Output is correct
26 Correct 4 ms 5980 KB Output is correct
27 Correct 4 ms 5980 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 5 ms 5980 KB Output is correct
30 Correct 4 ms 5980 KB Output is correct
31 Correct 6 ms 5980 KB Output is correct
32 Correct 7 ms 5980 KB Output is correct
33 Correct 6 ms 5872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5136 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 5 ms 5976 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 5 ms 5976 KB Output is correct
25 Correct 85 ms 41056 KB Output is correct
26 Correct 96 ms 49232 KB Output is correct
27 Correct 87 ms 49272 KB Output is correct
28 Correct 159 ms 48976 KB Output is correct
29 Correct 141 ms 44884 KB Output is correct
30 Correct 158 ms 47956 KB Output is correct
31 Correct 160 ms 46932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 144 ms 36240 KB Output is correct
4 Correct 152 ms 36388 KB Output is correct
5 Correct 124 ms 36420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5136 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 5 ms 5976 KB Output is correct
23 Correct 5 ms 5980 KB Output is correct
24 Correct 5 ms 5976 KB Output is correct
25 Correct 2 ms 4956 KB Output is correct
26 Correct 4 ms 5980 KB Output is correct
27 Correct 4 ms 5980 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 5 ms 5980 KB Output is correct
30 Correct 4 ms 5980 KB Output is correct
31 Correct 6 ms 5980 KB Output is correct
32 Correct 7 ms 5980 KB Output is correct
33 Correct 6 ms 5872 KB Output is correct
34 Correct 85 ms 41056 KB Output is correct
35 Correct 96 ms 49232 KB Output is correct
36 Correct 87 ms 49272 KB Output is correct
37 Correct 159 ms 48976 KB Output is correct
38 Correct 141 ms 44884 KB Output is correct
39 Correct 158 ms 47956 KB Output is correct
40 Correct 160 ms 46932 KB Output is correct
41 Correct 2 ms 4956 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 144 ms 36240 KB Output is correct
44 Correct 152 ms 36388 KB Output is correct
45 Correct 124 ms 36420 KB Output is correct
46 Correct 117 ms 46672 KB Output is correct
47 Correct 109 ms 46672 KB Output is correct
48 Correct 111 ms 46676 KB Output is correct
49 Correct 117 ms 46672 KB Output is correct
50 Correct 159 ms 40164 KB Output is correct
51 Correct 144 ms 40276 KB Output is correct
52 Correct 143 ms 40280 KB Output is correct
53 Correct 183 ms 40160 KB Output is correct