Submission #851168

# Submission time Handle Problem Language Result Execution time Memory
851168 2023-09-18T17:38:21 Z Charizard2021 Cat Exercise (JOI23_ho_t4) C++17
41 / 100
2000 ms 70660 KB
#include <bits/stdc++.h>
using namespace std;
const long long N = 200001;
vector<long long> adj[N];
vector<long long> depth(N);
long long parents[N][18];
vector<long long> parent(N);
vector<long long> maxVal(N);
vector<long long> h(N);
vector<long long> sz(N);
void dfs(long long u, long long p){
    for(long long v : adj[u]){
        if(v != p){
            parents[v][0] = u;
            depth[v] = depth[u] + 1;
            dfs(v, u);
        }
    }
}
long long distance(long long a, long long b){
    long long dist = depth[a] + depth[b];
    if(depth[a] < depth[b]){
        swap(a, b);
    }
    long long diff = depth[a] - depth[b];
    long long val = 0;
    while(diff > 0){
        if(diff & 1){
            a = parents[a][val];
        }
        diff /= 2;
        val++;
    }
    if(a == b){
        return dist - 2 * depth[a];
    }
    for(long long i = 17; i >= 0; i++){
        if(parents[a][i] != parents[b][i]){
            a = parents[a][i];
            b = parents[b][i];
        }
    }
    return dist - 2 * depth[a] + 2;
}
long long getRoot(long long u){
    if(parent[u] == u){
        return u;
    }
    return parent[u] = getRoot(parent[u]);
}
void unite(long long a, long long b){
    long long u = getRoot(a);
    long long v = getRoot(b);
    if(u == v){
        return;
    }
    if(sz[u] == sz[v]){
        sz[u]++;
    }
    if(sz[u] > sz[v]){
        parent[v] = u;
        if(h[maxVal[v]] > h[maxVal[u]]){
            maxVal[u] = maxVal[v];
        }
    }
    else{
        parent[u] = v;
        if(h[maxVal[u]] > h[maxVal[v]]){
            maxVal[v] = maxVal[u];
        }
    }
}
int main(){
    long long n;
    cin >> n;
    for(long long i = 1; i <= n; i++){
        cin >> h[i];
    }
    for(long long i = 0; i < n - 1; i++){
        long long a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    parents[1][0] = 1;
    dfs(1, 0);
    for(long long j = 1; j <= 17; j++){
        for(long long i = 1; i <= n; i++){
            parents[i][j] = parents[parents[i][j - 1]][j - 1];
        }
    }
    for(long long i = 1; i <= n; i++){
        parent[i] = i;
        maxVal[i] = i;
    }
    vector<pair<long long, long long> > adj2[1 + n];
    vector<pair<long long, long long> > positions(1 + n);
    for(long long i = 1; i <= n; i++){
        positions[i] = make_pair(h[i], i);
    }
    sort(positions.begin(), positions.end());
    for(long long i = 1; i <= n; i++){
        long long u = positions[i].second;
        for(long long v : adj[u]){
            if(h[v] <= h[u]){
                long long weight = maxVal[getRoot(v)];
                adj2[u].push_back(make_pair(weight, distance(u, weight)));
                unite(u, v);
            }
        }
    }
    vector<long long> dist(1 + n);
    vector<bool> visited(1 + n, false);
    queue<long long> q;
    q.push(positions[n].second);
    while(!q.empty()){
        long long u = q.front();
        q.pop();
        visited[u] = true;
        for(pair<long long, long long> x : adj2[u]){
            long long v = x.first;
            if(!visited[v]){
                visited[v] = true;
                dist[v] = dist[u] + x.second;
                q.push(v);
            }
        }
    }
    long long ans = 0;
    for(long long i = 1; i <= n; i++){
        ans = max(ans, dist[i]);
    }
    cout << ans << "\n";
}

Compilation message

Main.cpp: In function 'long long int distance(long long int, long long int)':
Main.cpp:37:5: warning: iteration 9223372036854775790 invokes undefined behavior [-Waggressive-loop-optimizations]
   37 |     for(long long i = 17; i >= 0; i++){
      |     ^~~
Main.cpp:37:29: note: within this loop
   37 |     for(long long i = 17; i >= 0; i++){
      |                           ~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 5 ms 14168 KB Output is correct
4 Correct 5 ms 14168 KB Output is correct
5 Correct 5 ms 14168 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 5 ms 14172 KB Output is correct
8 Correct 5 ms 14172 KB Output is correct
9 Correct 5 ms 14168 KB Output is correct
10 Correct 5 ms 14168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 5 ms 14168 KB Output is correct
4 Correct 5 ms 14168 KB Output is correct
5 Correct 5 ms 14168 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 5 ms 14172 KB Output is correct
8 Correct 5 ms 14172 KB Output is correct
9 Correct 5 ms 14168 KB Output is correct
10 Correct 5 ms 14168 KB Output is correct
11 Correct 5 ms 14168 KB Output is correct
12 Correct 5 ms 14172 KB Output is correct
13 Correct 5 ms 14168 KB Output is correct
14 Correct 5 ms 14172 KB Output is correct
15 Correct 5 ms 14168 KB Output is correct
16 Correct 5 ms 14168 KB Output is correct
17 Correct 5 ms 14168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 5 ms 14168 KB Output is correct
4 Correct 5 ms 14168 KB Output is correct
5 Correct 5 ms 14168 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 5 ms 14172 KB Output is correct
8 Correct 5 ms 14172 KB Output is correct
9 Correct 5 ms 14168 KB Output is correct
10 Correct 5 ms 14168 KB Output is correct
11 Correct 5 ms 14168 KB Output is correct
12 Correct 5 ms 14172 KB Output is correct
13 Correct 5 ms 14168 KB Output is correct
14 Correct 5 ms 14172 KB Output is correct
15 Correct 5 ms 14168 KB Output is correct
16 Correct 5 ms 14168 KB Output is correct
17 Correct 5 ms 14168 KB Output is correct
18 Correct 9 ms 17240 KB Output is correct
19 Correct 9 ms 17240 KB Output is correct
20 Correct 9 ms 17240 KB Output is correct
21 Correct 9 ms 17244 KB Output is correct
22 Correct 9 ms 17240 KB Output is correct
23 Correct 9 ms 17244 KB Output is correct
24 Correct 11 ms 17240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 5 ms 14168 KB Output is correct
4 Correct 5 ms 14168 KB Output is correct
5 Correct 5 ms 14168 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 5 ms 14172 KB Output is correct
8 Correct 5 ms 14172 KB Output is correct
9 Correct 5 ms 14168 KB Output is correct
10 Correct 5 ms 14168 KB Output is correct
11 Correct 5 ms 14168 KB Output is correct
12 Correct 5 ms 14172 KB Output is correct
13 Correct 5 ms 14168 KB Output is correct
14 Correct 5 ms 14172 KB Output is correct
15 Correct 5 ms 14168 KB Output is correct
16 Correct 5 ms 14168 KB Output is correct
17 Correct 5 ms 14168 KB Output is correct
18 Correct 9 ms 17240 KB Output is correct
19 Correct 9 ms 17240 KB Output is correct
20 Correct 9 ms 17240 KB Output is correct
21 Correct 9 ms 17244 KB Output is correct
22 Correct 9 ms 17240 KB Output is correct
23 Correct 9 ms 17244 KB Output is correct
24 Correct 11 ms 17240 KB Output is correct
25 Execution timed out 2039 ms 14168 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 5 ms 14168 KB Output is correct
4 Correct 5 ms 14168 KB Output is correct
5 Correct 5 ms 14168 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 5 ms 14172 KB Output is correct
8 Correct 5 ms 14172 KB Output is correct
9 Correct 5 ms 14168 KB Output is correct
10 Correct 5 ms 14168 KB Output is correct
11 Correct 5 ms 14168 KB Output is correct
12 Correct 5 ms 14172 KB Output is correct
13 Correct 5 ms 14168 KB Output is correct
14 Correct 5 ms 14172 KB Output is correct
15 Correct 5 ms 14168 KB Output is correct
16 Correct 5 ms 14168 KB Output is correct
17 Correct 5 ms 14168 KB Output is correct
18 Correct 9 ms 17240 KB Output is correct
19 Correct 9 ms 17240 KB Output is correct
20 Correct 9 ms 17240 KB Output is correct
21 Correct 9 ms 17244 KB Output is correct
22 Correct 9 ms 17240 KB Output is correct
23 Correct 9 ms 17244 KB Output is correct
24 Correct 11 ms 17240 KB Output is correct
25 Correct 222 ms 70660 KB Output is correct
26 Correct 217 ms 70596 KB Output is correct
27 Correct 208 ms 70480 KB Output is correct
28 Correct 275 ms 69856 KB Output is correct
29 Correct 266 ms 69968 KB Output is correct
30 Correct 274 ms 70228 KB Output is correct
31 Correct 269 ms 69960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2047 ms 14168 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 5 ms 14168 KB Output is correct
4 Correct 5 ms 14168 KB Output is correct
5 Correct 5 ms 14168 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 5 ms 14172 KB Output is correct
8 Correct 5 ms 14172 KB Output is correct
9 Correct 5 ms 14168 KB Output is correct
10 Correct 5 ms 14168 KB Output is correct
11 Correct 5 ms 14168 KB Output is correct
12 Correct 5 ms 14172 KB Output is correct
13 Correct 5 ms 14168 KB Output is correct
14 Correct 5 ms 14172 KB Output is correct
15 Correct 5 ms 14168 KB Output is correct
16 Correct 5 ms 14168 KB Output is correct
17 Correct 5 ms 14168 KB Output is correct
18 Correct 9 ms 17240 KB Output is correct
19 Correct 9 ms 17240 KB Output is correct
20 Correct 9 ms 17240 KB Output is correct
21 Correct 9 ms 17244 KB Output is correct
22 Correct 9 ms 17240 KB Output is correct
23 Correct 9 ms 17244 KB Output is correct
24 Correct 11 ms 17240 KB Output is correct
25 Execution timed out 2039 ms 14168 KB Time limit exceeded
26 Halted 0 ms 0 KB -