Submission #741580

# Submission time Handle Problem Language Result Execution time Memory
741580 2023-05-14T11:30:52 Z maomao90 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
447 ms 48900 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
const int MAXL = 20;

int n;
int a[MAXN], inv[MAXN];
vi adj[MAXN];
ll dp[MAXN];

int p[MAXL][MAXN], lvl[MAXN];
void dfs(int u) {
    REP (k, 1, MAXL) {
        if (p[k - 1][u] == -1) {
            p[k][u] = -1;
        } else {
            p[k][u] = p[k - 1][p[k - 1][u]];
        }
    }
    for (int v : adj[u]) {
        if (v == p[0][u]) {
            continue;
        }
        lvl[v] = lvl[u] + 1;
        p[0][v] = u;
        dfs(v);
    }
}
int getd(int a, int b) {
    if (lvl[a] < lvl[b]) {
        swap(a, b);
    }
    int res = 0;
    RREP (k, MAXL - 1, 0) {
        if (p[k][a] != -1 && lvl[p[k][a]] >= lvl[b]) {
            a = p[k][a];
            res += 1 << k;
        }
    }
    if (a == b) {
        return res;
    }
    RREP (k, MAXL - 1, 0) {
        if (p[k][a] != p[k][b]) {
            res += 1 << k + 1;
            a = p[k][a];
            b = p[k][b];
        }
    }
    return res + 2;
}

int up[MAXN], rnk[MAXN], mx[MAXN];
void init() {
    REP (i, 1, n + 1) {
        up[i] = i;
        rnk[i] = 1;
        mx[i] = a[i];
    }
}
int findp(int i) {
    if (up[i] == i) return i;
    return up[i] = findp(up[i]);
}
void join(int a, int b) {
    int pa = findp(a), pb = findp(b);
    if (pa == pb) {
        return;
    }
    if (rnk[pa] < rnk[pb]) {
        swap(pa, pb);
    }
    if (rnk[pa] == rnk[pb]) {
        rnk[pa]++;
    }
    up[pb] = pa;
    mxto(mx[pa], mx[pb]);
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n;
    REP (i, 1, n + 1) {
        cin >> a[i];
        inv[a[i]] = i;
    }
    REP (i, 1, n) {
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    p[0][1] = -1;
    dfs(1);
    init();
    REP (i, 1, n + 1) {
        int u = inv[i];
        cerr << u << '\n';
        for (int v : adj[u]) {
            if (a[v] > i) {
                continue;
            }
            int j = mx[findp(v)];
            int nv = inv[j];
            cerr << ' ' << j << ' ' << nv << ' ' << getd(u, nv) << '\n';
            mxto(dp[i], dp[j] + getd(u, nv));
            join(u, v);
        }
    }
    cout << dp[n] << '\n';
    return 0;
}

Compilation message

Main.cpp: In function 'int getd(int, int)':
Main.cpp:78:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   78 |             res += 1 << k + 1;
      |                         ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5160 KB Output is correct
5 Correct 4 ms 5160 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5160 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5160 KB Output is correct
5 Correct 4 ms 5160 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5160 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5160 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5164 KB Output is correct
17 Correct 4 ms 5168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5160 KB Output is correct
5 Correct 4 ms 5160 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5160 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5160 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5164 KB Output is correct
17 Correct 4 ms 5168 KB Output is correct
18 Correct 5 ms 6192 KB Output is correct
19 Correct 8 ms 6124 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 7 ms 6156 KB Output is correct
22 Correct 6 ms 6228 KB Output is correct
23 Correct 6 ms 6192 KB Output is correct
24 Correct 6 ms 6196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5160 KB Output is correct
5 Correct 4 ms 5160 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5160 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5160 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5164 KB Output is correct
17 Correct 4 ms 5168 KB Output is correct
18 Correct 5 ms 6192 KB Output is correct
19 Correct 8 ms 6124 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 7 ms 6156 KB Output is correct
22 Correct 6 ms 6228 KB Output is correct
23 Correct 6 ms 6192 KB Output is correct
24 Correct 6 ms 6196 KB Output is correct
25 Correct 3 ms 5160 KB Output is correct
26 Correct 6 ms 6100 KB Output is correct
27 Correct 8 ms 6100 KB Output is correct
28 Correct 6 ms 6068 KB Output is correct
29 Correct 7 ms 6064 KB Output is correct
30 Correct 7 ms 5844 KB Output is correct
31 Correct 6 ms 5936 KB Output is correct
32 Correct 7 ms 5932 KB Output is correct
33 Correct 7 ms 5844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5160 KB Output is correct
5 Correct 4 ms 5160 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5160 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5160 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5164 KB Output is correct
17 Correct 4 ms 5168 KB Output is correct
18 Correct 5 ms 6192 KB Output is correct
19 Correct 8 ms 6124 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 7 ms 6156 KB Output is correct
22 Correct 6 ms 6228 KB Output is correct
23 Correct 6 ms 6192 KB Output is correct
24 Correct 6 ms 6196 KB Output is correct
25 Correct 122 ms 48900 KB Output is correct
26 Correct 139 ms 47164 KB Output is correct
27 Correct 136 ms 47780 KB Output is correct
28 Correct 226 ms 45976 KB Output is correct
29 Correct 227 ms 45900 KB Output is correct
30 Correct 245 ms 47040 KB Output is correct
31 Correct 241 ms 46208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 5 ms 5076 KB Output is correct
3 Correct 261 ms 35796 KB Output is correct
4 Correct 236 ms 33376 KB Output is correct
5 Correct 264 ms 34056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5160 KB Output is correct
5 Correct 4 ms 5160 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5160 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5160 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5164 KB Output is correct
17 Correct 4 ms 5168 KB Output is correct
18 Correct 5 ms 6192 KB Output is correct
19 Correct 8 ms 6124 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 7 ms 6156 KB Output is correct
22 Correct 6 ms 6228 KB Output is correct
23 Correct 6 ms 6192 KB Output is correct
24 Correct 6 ms 6196 KB Output is correct
25 Correct 3 ms 5160 KB Output is correct
26 Correct 6 ms 6100 KB Output is correct
27 Correct 8 ms 6100 KB Output is correct
28 Correct 6 ms 6068 KB Output is correct
29 Correct 7 ms 6064 KB Output is correct
30 Correct 7 ms 5844 KB Output is correct
31 Correct 6 ms 5936 KB Output is correct
32 Correct 7 ms 5932 KB Output is correct
33 Correct 7 ms 5844 KB Output is correct
34 Correct 122 ms 48900 KB Output is correct
35 Correct 139 ms 47164 KB Output is correct
36 Correct 136 ms 47780 KB Output is correct
37 Correct 226 ms 45976 KB Output is correct
38 Correct 227 ms 45900 KB Output is correct
39 Correct 245 ms 47040 KB Output is correct
40 Correct 241 ms 46208 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 5 ms 5076 KB Output is correct
43 Correct 261 ms 35796 KB Output is correct
44 Correct 236 ms 33376 KB Output is correct
45 Correct 264 ms 34056 KB Output is correct
46 Correct 447 ms 40648 KB Output is correct
47 Correct 415 ms 39144 KB Output is correct
48 Correct 441 ms 41364 KB Output is correct
49 Correct 437 ms 39740 KB Output is correct
50 Correct 415 ms 34112 KB Output is correct
51 Correct 416 ms 33416 KB Output is correct
52 Correct 442 ms 33204 KB Output is correct
53 Correct 381 ms 33272 KB Output is correct