Problem p02698 (C++) — buggy submission, verdict: Runtime Error

//  Copyright © 2019 Diego Garcia Rodriguez del Campo. All rights reserved.
#include <bits/stdc++.h>
#define MAX 200005
#define optimiza_io                                                            \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
using namespace std;
typedef long long i64;

int N, a[MAX], vl[MAX];
int head[MAX], prv[MAX];
int ans[MAX];
vector<int> ady[MAX];

void dfs(int x, int p, int lst) {

  if (vl[x] < vl[head[1]]) {
    ans[x] = lst;
    int tmp = head[1];
    head[1] = x;
    for (auto v : ady[x])
      if (v != p)
        dfs(v, x, lst);
    head[1] = tmp;
    return;
  } else if (vl[x] > vl[head[lst]]) {
    head[++lst] = x;
    ans[x] = lst;
    for (auto v : ady[x])
      if (v != p)
        dfs(v, x, lst);
    return;
  } else {
    int l = 0;
    int r = lst;
    while (l < r) {
      int m = (l + r + 1) / 2;
      if (vl[head[m]] < vl[x])
        l = m;
      else
        r = m - 1;
    }
    int pp = l + 1;
    int tmp = head[pp];
    head[pp] = x;
    ans[x] = lst;
    for (auto v : ady[x])
      if (v != p)
        dfs(v, x, lst);
    head[pp] = tmp;
    return;
  }
}
/*
int lis()
{
    int lst = 1;
    head[1] = 1;
    for( int i = 2; i <= N; i ++ ) {
        if( a[i] < a[head[1]] )
            head[1] = i;
        else if( a[i] > a[head[lst]] ) {
            head[++lst] = i;
        }
        else  {
            int l = 0;
            int r = lst;
            while( l < r ) {
                int m = ( l + r + 1 ) / 2;
                if( a[head[m]] < a[i] )
                    l = m;
                else
                    r = m - 1;
            }
            int p = l + 1;
            head[p] = i;
        }
    }
    return lst;
}

  */

int main() {
  // optimiza_io
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> vl[i];

  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    ady[u].push_back(v);
    ady[v].push_back(u);
  }

  ans[1] = 1;
  head[1] = 1;

  for (auto v : ady[1])
    dfs(v, 1, 1);

  for (int i = 1; i <= N; i++)
    cout << ans[i] << "\\n";

  return 0;
}

Error: WA

Fixed submission:

//  Copyright © 2019 Diego Garcia Rodriguez del Campo. All rights reserved.
#include <bits/stdc++.h>
#define MAX 200005
#define optimiza_io                                                            \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
using namespace std;
typedef long long i64;

int N, a[MAX], vl[MAX];
int head[MAX], prv[MAX];
int ans[MAX];
vector<int> ady[MAX];

void dfs(int x, int p, int lst) {
  if (x == 1)
    return;
  if (vl[x] < vl[head[1]]) {
    ans[x] = lst;
    int tmp = head[1];
    head[1] = x;
    for (auto v : ady[x])
      if (v != p)
        dfs(v, x, lst);
    head[1] = tmp;
    return;
  } else if (vl[x] > vl[head[lst]]) {
    head[++lst] = x;
    ans[x] = lst;
    for (auto v : ady[x])
      if (v != p)
        dfs(v, x, lst);
    return;
  } else {
    int l = 0;
    int r = lst;
    while (l < r) {
      int m = (l + r + 1) / 2;
      if (vl[head[m]] < vl[x])
        l = m;
      else
        r = m - 1;
    }
    int p = l + 1;
    int tmp = head[p];
    head[p] = x;
    ans[x] = lst;
    for (auto v : ady[x])
      if (v != p)
        dfs(v, x, lst);
    head[p] = tmp;
    return;
  }
}
/*
int lis()
{
    int lst = 1;
    head[1] = 1;
    for( int i = 2; i <= N; i ++ ) {
        if( a[i] < a[head[1]] )
            head[1] = i;
        else if( a[i] > a[head[lst]] ) {
            head[++lst] = i;
        }
        else  {
            int l = 0;
            int r = lst;
            while( l < r ) {
                int m = ( l + r + 1 ) / 2;
                if( a[head[m]] < a[i] )
                    l = m;
                else
                    r = m - 1;
            }
            int p = l + 1;
            head[p] = i;
        }
    }
    return lst;
}

  */

int main() {
  // optimiza_io
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> vl[i];

  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    ady[u].push_back(v);
    ady[v].push_back(u);
  }

  head[1] = 1;

  for (auto v : ady[1])
    dfs(v, 1, 1);

  for (int i = 1; i <= N; i++)
    cout << ans[i] << "\\n";

  return 0;
}