Submission #729486

# Submission time Handle Problem Language Result Execution time Memory
729486 2023-04-24T08:07:57 Z MohammadAghil Cat Exercise (JOI23_ho_t4) C++17
100 / 100
384 ms 40044 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)size(x)
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pp;

void dbg(){
     cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
     cerr << h << ", ";
     dbg(t...);
}

void IOS(){
     cin.tie(0) -> sync_with_stdio(0);
     #ifndef ONLINE_JUDGE
          // freopen("inp.txt", "r", stdin);
          // freopen("out.txt", "w", stdout);
          #define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__)
     #else
          #define er(...) 0
     #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll mod = 1e9 + 7, maxn = 2e5 + 5, maxk = 31, lg = 19, inf = ll(1e18) + 5;

int par[maxn][lg], h[maxn];
vector<int> adj[maxn];

void dfs(int r, int p){
     par[r][0] = p;
     rep(j,1,lg){
          par[r][j] = par[par[r][j-1]][j-1];
     }
     for(int c: adj[r]) if(c - p){
          h[c] = h[r] + 1;
          dfs(c, r);
     }
}

int lca(int u, int v){
     if(h[u] < h[v]) swap(u, v);
     per(i,lg-1,0) if(h[par[u][i]] >= h[v]) u = par[u][i];
     if(u == v) return u;
     per(i,lg-1,0) if(par[u][i] - par[v][i]) u = par[u][i], v = par[v][i];
     return par[u][0];
}

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

int pr[maxn];
int get(int x){
     return pr[x] == -1? x: pr[x] = get(pr[x]);
}

ll dp[maxn];

int main(){ IOS();
     int n; cin >> n;
     vector<int> h(n);
     rep(i,0,n){
          cin >> h[i];
          pr[i] = -1;
     }
     rep(i,1,n){
          int u, v; cin >> u >> v; u--, v--;
          adj[u].pb(v), adj[v].pb(u);
     }
     dfs(0, 0);
     vector<int> srt(n); iota(all(srt), 0);
     sort(all(srt), [&](int u, int v){
          return h[u] < h[v];
     });
     for(int r: srt){
          for(int c: adj[r]) if(h[c] < h[r]){ c = get(c);
               dp[r] = max(dp[r], dp[c] + dist(c, r));
               pr[c] = r;
          }
     }
     cout << dp[srt.back()] << '\n';
     return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4964 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4964 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5064 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4964 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5064 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 6 ms 5844 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 6 ms 5844 KB Output is correct
21 Correct 7 ms 5844 KB Output is correct
22 Correct 6 ms 5860 KB Output is correct
23 Correct 7 ms 5900 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4964 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5064 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 6 ms 5844 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 6 ms 5844 KB Output is correct
21 Correct 7 ms 5844 KB Output is correct
22 Correct 6 ms 5860 KB Output is correct
23 Correct 7 ms 5900 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 7 ms 5780 KB Output is correct
27 Correct 7 ms 5736 KB Output is correct
28 Correct 7 ms 5716 KB Output is correct
29 Correct 7 ms 5716 KB Output is correct
30 Correct 6 ms 5588 KB Output is correct
31 Correct 6 ms 5588 KB Output is correct
32 Correct 6 ms 5588 KB Output is correct
33 Correct 6 ms 5588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4964 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5064 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 6 ms 5844 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 6 ms 5844 KB Output is correct
21 Correct 7 ms 5844 KB Output is correct
22 Correct 6 ms 5860 KB Output is correct
23 Correct 7 ms 5900 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
25 Correct 153 ms 38040 KB Output is correct
26 Correct 145 ms 38676 KB Output is correct
27 Correct 162 ms 40044 KB Output is correct
28 Correct 202 ms 37812 KB Output is correct
29 Correct 221 ms 38144 KB Output is correct
30 Correct 225 ms 39980 KB Output is correct
31 Correct 227 ms 37812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 220 ms 32772 KB Output is correct
4 Correct 216 ms 31268 KB Output is correct
5 Correct 236 ms 33644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4964 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5064 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 6 ms 5844 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 6 ms 5844 KB Output is correct
21 Correct 7 ms 5844 KB Output is correct
22 Correct 6 ms 5860 KB Output is correct
23 Correct 7 ms 5900 KB Output is correct
24 Correct 6 ms 5844 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 7 ms 5780 KB Output is correct
27 Correct 7 ms 5736 KB Output is correct
28 Correct 7 ms 5716 KB Output is correct
29 Correct 7 ms 5716 KB Output is correct
30 Correct 6 ms 5588 KB Output is correct
31 Correct 6 ms 5588 KB Output is correct
32 Correct 6 ms 5588 KB Output is correct
33 Correct 6 ms 5588 KB Output is correct
34 Correct 153 ms 38040 KB Output is correct
35 Correct 145 ms 38676 KB Output is correct
36 Correct 162 ms 40044 KB Output is correct
37 Correct 202 ms 37812 KB Output is correct
38 Correct 221 ms 38144 KB Output is correct
39 Correct 225 ms 39980 KB Output is correct
40 Correct 227 ms 37812 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 220 ms 32772 KB Output is correct
44 Correct 216 ms 31268 KB Output is correct
45 Correct 236 ms 33644 KB Output is correct
46 Correct 375 ms 35216 KB Output is correct
47 Correct 354 ms 34132 KB Output is correct
48 Correct 312 ms 35668 KB Output is correct
49 Correct 384 ms 35584 KB Output is correct
50 Correct 347 ms 33264 KB Output is correct
51 Correct 327 ms 32184 KB Output is correct
52 Correct 371 ms 31240 KB Output is correct
53 Correct 317 ms 31088 KB Output is correct