Submission #737315

# Submission time Handle Problem Language Result Execution time Memory
737315 2023-05-07T04:32:41 Z nguyentunglam Cat Exercise (JOI23_ho_t4) C++17
54 / 100
236 ms 42800 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int par[20][N], h[N], p[N], pos[N], lab[N], f[N];
vector<int> adj[N];

void dfs(int u) {
    for(int j = 1; j <= 18; j++) par[j][u] = par[j - 1][par[j - 1][u]];
    for(int &v : adj[u]) if (v != par[0][u]) {
        par[0][v] = u; h[v] = h[u] + 1;
        dfs(v);
    }
}

int lca(int u, int v) {
    if (h[u] < h[v]) swap(u, v);
    int delta = h[u] - h[v];
    for(int j = 18; j >= 0; j--) if (delta >> j & 1) u = par[j][u];
    if (u == v) return u;
    for(int j = 18; j >= 0; j--) if (par[j][u] != par[j][v]) {
        u = par[j][u];
        v = par[j][v];
    }
    return par[0][u];
}

int dist(int u, int v) {
    return h[u] + h[v] - 2 * h[lca(u, v)];
}

int root(int v) {
    return lab[v] == v ? v : lab[v] = root(lab[v]);
}

int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i;
    for(int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(1);
    for(int i = 1; i <= n; i++) lab[i] = i;
    for(int i = 1; i <= n; i++) {
        int u = pos[i];
        for(int &v : adj[u]) if (p[v] < i) {
            int ver = root(v);
            f[u] = max(f[u], f[ver] + dist(u, ver));
        }
        for(int &v : adj[u]) if (p[v] < i) {
            int ver = root(v);
            lab[ver] = u;
        }
    }
    cout << f[pos[n]];
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:43:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:44:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:47:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5152 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5152 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5156 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5152 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5156 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 6 ms 6060 KB Output is correct
19 Correct 7 ms 6060 KB Output is correct
20 Correct 7 ms 6100 KB Output is correct
21 Correct 8 ms 6160 KB Output is correct
22 Correct 7 ms 6100 KB Output is correct
23 Correct 8 ms 6100 KB Output is correct
24 Correct 7 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5152 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5156 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 6 ms 6060 KB Output is correct
19 Correct 7 ms 6060 KB Output is correct
20 Correct 7 ms 6100 KB Output is correct
21 Correct 8 ms 6160 KB Output is correct
22 Correct 7 ms 6100 KB Output is correct
23 Correct 8 ms 6100 KB Output is correct
24 Correct 7 ms 6100 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 8 ms 5972 KB Output is correct
27 Correct 8 ms 5972 KB Output is correct
28 Correct 7 ms 5940 KB Output is correct
29 Correct 8 ms 5972 KB Output is correct
30 Correct 7 ms 5716 KB Output is correct
31 Correct 8 ms 5808 KB Output is correct
32 Correct 7 ms 5728 KB Output is correct
33 Correct 7 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5152 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5156 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 6 ms 6060 KB Output is correct
19 Correct 7 ms 6060 KB Output is correct
20 Correct 7 ms 6100 KB Output is correct
21 Correct 8 ms 6160 KB Output is correct
22 Correct 7 ms 6100 KB Output is correct
23 Correct 8 ms 6100 KB Output is correct
24 Correct 7 ms 6100 KB Output is correct
25 Incorrect 124 ms 42800 KB Output isn't correct
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5048 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 204 ms 32452 KB Output is correct
4 Correct 236 ms 30308 KB Output is correct
5 Correct 198 ms 30912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5152 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5156 KB Output is correct
12 Correct 4 ms 5156 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 6 ms 6060 KB Output is correct
19 Correct 7 ms 6060 KB Output is correct
20 Correct 7 ms 6100 KB Output is correct
21 Correct 8 ms 6160 KB Output is correct
22 Correct 7 ms 6100 KB Output is correct
23 Correct 8 ms 6100 KB Output is correct
24 Correct 7 ms 6100 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 8 ms 5972 KB Output is correct
27 Correct 8 ms 5972 KB Output is correct
28 Correct 7 ms 5940 KB Output is correct
29 Correct 8 ms 5972 KB Output is correct
30 Correct 7 ms 5716 KB Output is correct
31 Correct 8 ms 5808 KB Output is correct
32 Correct 7 ms 5728 KB Output is correct
33 Correct 7 ms 5716 KB Output is correct
34 Incorrect 124 ms 42800 KB Output isn't correct
35 Halted 0 ms 0 KB -