답안 #963524

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963524 2024-04-15T09:15:28 Z vladburac Cat Exercise (JOI23_ho_t4) C++17
100 / 100
313 ms 63828 KB
// credit: valeriu
#include <bits/stdc++.h>
using namespace std;
#define pii pair<long long, long long>
using ll = long long;
const int NMAX = 5e5;
const int VALMAX = 1e6;
const int LOGMAX = 18;
const int INF = 1e9;
const int MOD = 998244353;
mt19937 rnd( chrono::steady_clock::now().time_since_epoch().count() );

/*
#ifndef HOME
  ifstream fin( "regate.in" );
  ofstream fout( "regate.out" );
  #define cin fin
  #define cout fout
#endif // HOME
*/

vector <int> edges[NMAX+1];
pii h[NMAX+1];
int parent[NMAX+1];
int heavy[NMAX+1];

int find( int node ) {
  if( parent[node] == node )
    return node;
  return parent[node] = find( parent[node] );
}
void unite( int a, int b ) {
  a = find( a );
  b = find( b );
  if( a != b ) {
    parent[b] = a;
    heavy[a] = h[heavy[a]] > h[heavy[b]] ? heavy[a] : heavy[b];
  }
}

int anc[NMAX+1][LOGMAX+1];
int dep[NMAX+1];
void dfs( int node, int par = 0 ) {
  int i;
  anc[node][0] = par;
  dep[node] = dep[par] + 1;
  for( i = 1; i < LOGMAX; i++ ) {
    if( anc[node][i-1] != 0 )
      anc[node][i] = anc[anc[node][i-1]][i-1];
  }
  for( auto vec: edges[node] ) {
    if( vec != par )
      dfs( vec, node );
  }
}
int dist( int a, int b ) {
  int ca = a, cb = b, i;
  if( dep[a] < dep[b] )
    swap( a, b );
  int d = dep[a] - dep[b];
  for( i = 0; i < LOGMAX; i++ ) {
    if( d & ( 1 << i ) )
      a = anc[a][i];
  }
  for( i = LOGMAX - 1; i >= 0; i-- ) {
    if( anc[a][i] != anc[b][i] )
      a = anc[a][i], b = anc[b][i];
  }
  if( a != b )
    a = anc[a][0], b = anc[b][0];
  return dep[ca] - dep[a] + dep[cb] - dep[a];
}

ll dp[NMAX+1], ord[NMAX+1];
void solve() {
  int n, i, a, b, nxt;
  cin >> n;
  for( i = 1; i <= n; i++ ) {
    cin >> h[i].first, h[i].second = i;
    ord[i] = i;
  }
  for( i = 0; i < n - 1; i++ ) {
    cin >> a >> b;
    edges[a].push_back( b );
    edges[b].push_back( a );
  }
  sort( ord + 1, ord + n + 1, []( int a, int b ) {
      return h[a] < h[b];
  } );
  for( i = 1; i <= n; i++ )
    parent[i] = i, heavy[i] = i;
  dfs( 1 );
  //cout << dist( 3, 5 ) << "\n";
  for( i = 1; i <= n; i++ ) {
    for( auto vec: edges[ord[i]] ) {
      if( h[vec] < h[ord[i]] ) {
        nxt = heavy[find(vec)];
        dp[ord[i]] = max( dp[ord[i]], dp[nxt] + dist( nxt, ord[i] ) );
      }
    }
    for( auto vec: edges[ord[i]] ) {
      if( h[vec] < h[ord[i]] )
        unite( vec, ord[i] );
    }
  }
  cout << dp[ord[n]];
}
int main() {
  ios_base::sync_with_stdio( false );
  cin.tie( NULL );
  cout.tie( NULL );
  int t = 1;
  //cin >> t;
  while( t-- ) {
    solve();
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21336 KB Output is correct
2 Correct 5 ms 21456 KB Output is correct
3 Correct 4 ms 21336 KB Output is correct
4 Correct 4 ms 21340 KB Output is correct
5 Correct 4 ms 21336 KB Output is correct
6 Correct 5 ms 21340 KB Output is correct
7 Correct 5 ms 21340 KB Output is correct
8 Correct 5 ms 21336 KB Output is correct
9 Correct 4 ms 21340 KB Output is correct
10 Correct 4 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21336 KB Output is correct
2 Correct 5 ms 21456 KB Output is correct
3 Correct 4 ms 21336 KB Output is correct
4 Correct 4 ms 21340 KB Output is correct
5 Correct 4 ms 21336 KB Output is correct
6 Correct 5 ms 21340 KB Output is correct
7 Correct 5 ms 21340 KB Output is correct
8 Correct 5 ms 21336 KB Output is correct
9 Correct 4 ms 21340 KB Output is correct
10 Correct 4 ms 21340 KB Output is correct
11 Correct 4 ms 21340 KB Output is correct
12 Correct 4 ms 21512 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 4 ms 21340 KB Output is correct
15 Correct 4 ms 21340 KB Output is correct
16 Correct 5 ms 21532 KB Output is correct
17 Correct 5 ms 21324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21336 KB Output is correct
2 Correct 5 ms 21456 KB Output is correct
3 Correct 4 ms 21336 KB Output is correct
4 Correct 4 ms 21340 KB Output is correct
5 Correct 4 ms 21336 KB Output is correct
6 Correct 5 ms 21340 KB Output is correct
7 Correct 5 ms 21340 KB Output is correct
8 Correct 5 ms 21336 KB Output is correct
9 Correct 4 ms 21340 KB Output is correct
10 Correct 4 ms 21340 KB Output is correct
11 Correct 4 ms 21340 KB Output is correct
12 Correct 4 ms 21512 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 4 ms 21340 KB Output is correct
15 Correct 4 ms 21340 KB Output is correct
16 Correct 5 ms 21532 KB Output is correct
17 Correct 5 ms 21324 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 7 ms 22008 KB Output is correct
20 Correct 7 ms 21856 KB Output is correct
21 Correct 6 ms 21852 KB Output is correct
22 Correct 7 ms 21852 KB Output is correct
23 Correct 7 ms 21852 KB Output is correct
24 Correct 7 ms 21964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21336 KB Output is correct
2 Correct 5 ms 21456 KB Output is correct
3 Correct 4 ms 21336 KB Output is correct
4 Correct 4 ms 21340 KB Output is correct
5 Correct 4 ms 21336 KB Output is correct
6 Correct 5 ms 21340 KB Output is correct
7 Correct 5 ms 21340 KB Output is correct
8 Correct 5 ms 21336 KB Output is correct
9 Correct 4 ms 21340 KB Output is correct
10 Correct 4 ms 21340 KB Output is correct
11 Correct 4 ms 21340 KB Output is correct
12 Correct 4 ms 21512 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 4 ms 21340 KB Output is correct
15 Correct 4 ms 21340 KB Output is correct
16 Correct 5 ms 21532 KB Output is correct
17 Correct 5 ms 21324 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 7 ms 22008 KB Output is correct
20 Correct 7 ms 21856 KB Output is correct
21 Correct 6 ms 21852 KB Output is correct
22 Correct 7 ms 21852 KB Output is correct
23 Correct 7 ms 21852 KB Output is correct
24 Correct 7 ms 21964 KB Output is correct
25 Correct 4 ms 21340 KB Output is correct
26 Correct 7 ms 21852 KB Output is correct
27 Correct 7 ms 21852 KB Output is correct
28 Correct 7 ms 21852 KB Output is correct
29 Correct 8 ms 21852 KB Output is correct
30 Correct 7 ms 21844 KB Output is correct
31 Correct 7 ms 21596 KB Output is correct
32 Correct 7 ms 21596 KB Output is correct
33 Correct 7 ms 21596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21336 KB Output is correct
2 Correct 5 ms 21456 KB Output is correct
3 Correct 4 ms 21336 KB Output is correct
4 Correct 4 ms 21340 KB Output is correct
5 Correct 4 ms 21336 KB Output is correct
6 Correct 5 ms 21340 KB Output is correct
7 Correct 5 ms 21340 KB Output is correct
8 Correct 5 ms 21336 KB Output is correct
9 Correct 4 ms 21340 KB Output is correct
10 Correct 4 ms 21340 KB Output is correct
11 Correct 4 ms 21340 KB Output is correct
12 Correct 4 ms 21512 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 4 ms 21340 KB Output is correct
15 Correct 4 ms 21340 KB Output is correct
16 Correct 5 ms 21532 KB Output is correct
17 Correct 5 ms 21324 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 7 ms 22008 KB Output is correct
20 Correct 7 ms 21856 KB Output is correct
21 Correct 6 ms 21852 KB Output is correct
22 Correct 7 ms 21852 KB Output is correct
23 Correct 7 ms 21852 KB Output is correct
24 Correct 7 ms 21964 KB Output is correct
25 Correct 131 ms 61892 KB Output is correct
26 Correct 126 ms 63580 KB Output is correct
27 Correct 114 ms 63568 KB Output is correct
28 Correct 168 ms 63568 KB Output is correct
29 Correct 199 ms 63732 KB Output is correct
30 Correct 178 ms 63828 KB Output is correct
31 Correct 176 ms 63732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21336 KB Output is correct
2 Correct 4 ms 21336 KB Output is correct
3 Correct 193 ms 52612 KB Output is correct
4 Correct 181 ms 52560 KB Output is correct
5 Correct 187 ms 52556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21336 KB Output is correct
2 Correct 5 ms 21456 KB Output is correct
3 Correct 4 ms 21336 KB Output is correct
4 Correct 4 ms 21340 KB Output is correct
5 Correct 4 ms 21336 KB Output is correct
6 Correct 5 ms 21340 KB Output is correct
7 Correct 5 ms 21340 KB Output is correct
8 Correct 5 ms 21336 KB Output is correct
9 Correct 4 ms 21340 KB Output is correct
10 Correct 4 ms 21340 KB Output is correct
11 Correct 4 ms 21340 KB Output is correct
12 Correct 4 ms 21512 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 4 ms 21340 KB Output is correct
15 Correct 4 ms 21340 KB Output is correct
16 Correct 5 ms 21532 KB Output is correct
17 Correct 5 ms 21324 KB Output is correct
18 Correct 8 ms 21852 KB Output is correct
19 Correct 7 ms 22008 KB Output is correct
20 Correct 7 ms 21856 KB Output is correct
21 Correct 6 ms 21852 KB Output is correct
22 Correct 7 ms 21852 KB Output is correct
23 Correct 7 ms 21852 KB Output is correct
24 Correct 7 ms 21964 KB Output is correct
25 Correct 4 ms 21340 KB Output is correct
26 Correct 7 ms 21852 KB Output is correct
27 Correct 7 ms 21852 KB Output is correct
28 Correct 7 ms 21852 KB Output is correct
29 Correct 8 ms 21852 KB Output is correct
30 Correct 7 ms 21844 KB Output is correct
31 Correct 7 ms 21596 KB Output is correct
32 Correct 7 ms 21596 KB Output is correct
33 Correct 7 ms 21596 KB Output is correct
34 Correct 131 ms 61892 KB Output is correct
35 Correct 126 ms 63580 KB Output is correct
36 Correct 114 ms 63568 KB Output is correct
37 Correct 168 ms 63568 KB Output is correct
38 Correct 199 ms 63732 KB Output is correct
39 Correct 178 ms 63828 KB Output is correct
40 Correct 176 ms 63732 KB Output is correct
41 Correct 5 ms 21336 KB Output is correct
42 Correct 4 ms 21336 KB Output is correct
43 Correct 193 ms 52612 KB Output is correct
44 Correct 181 ms 52560 KB Output is correct
45 Correct 187 ms 52556 KB Output is correct
46 Correct 278 ms 59532 KB Output is correct
47 Correct 313 ms 58696 KB Output is correct
48 Correct 237 ms 59940 KB Output is correct
49 Correct 248 ms 58704 KB Output is correct
50 Correct 244 ms 54356 KB Output is correct
51 Correct 268 ms 54420 KB Output is correct
52 Correct 265 ms 54364 KB Output is correct
53 Correct 267 ms 54408 KB Output is correct