Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// x^y≡x^(y%(p-1))mod p
// but there is a condition that x needs to be coprime to p
/// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
/// is(65-90) and 32 for space
typedef long long int ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fo(i, a, b) for (ll i = a; i <= b; i += 1)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
#define vll vector<ll>
#define tests()                                                                \
  int test_cases;                                                              \
  cin >> test_cases;                                                           \
  while (test_cases--)
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define MAXN 100010
#define MOD 1000000007
#define mod 998244353
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define sp " "
#define MS(x, y) fill_n(*x, sizeof x / sizeof **x, y);
#define mem(x, y) memset(x, y, sizeof(x));
#define INF 1000000000000000000
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result
  x = x % p;
  if (y == 0)
    return 1;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;      // y = y/2
    x = (x * x) % p; // Change x to x^2
  }
  return res;
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll modinv(ll a, ll p) { return power(a, p - 2, p); }
vll adj[2 * MAXN];
ll LIS[MAXN];
vll a;
vll dp;
void dfs(ll s, ll p) {
  auto it = lower_bound(all(dp), a[s]) - dp.begin();
  ll sz = dp.size();
  ll store;
  if (it != sz) {
    store = dp[it];
    dp[it] = a[s];
  } else {
    dp.pb(a[s]);
  }
  LIS[s] = dp.size();
  for (auto v : adj[s]) {
    if (v == p) {
      continue;
    }
    dfs(v, s);
  }

  if (it != sz) {
    dp[it] = store;
  } else {
    dp.resize(sz);
  }
  return;
}
int solve() {
  ll n;
  cin >> n;
  a.resize(n + 1);
  f(i, 0, n) { cin >> a[i + 1]; }
  f(i, 0, n - 1) {
    ll a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, 1);
  fo(i, 1, n) { cout << LIS[i] << endl; }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  // tests(){solve();}
  solve();
  return 0;
} /*#ifndef ONLINE_JUDGE
     #endif*/

// make_pair make_tuple  tuple<int,int,int>

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// x^y≡x^(y%(p-1))mod p
// but there is a condition that x needs to be coprime to p
/// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
/// is(65-90) and 32 for space
typedef long long int ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fo(i, a, b) for (ll i = a; i <= b; i += 1)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
#define vll vector<ll>
#define tests()                                                                \
  int test_cases;                                                              \
  cin >> test_cases;                                                           \
  while (test_cases--)
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define MAXN 100010
#define MOD 1000000007
#define mod 998244353
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define sp " "
#define MS(x, y) fill_n(*x, sizeof x / sizeof **x, y);
#define mem(x, y) memset(x, y, sizeof(x));
#define INF 1000000000000000000
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result
  x = x % p;
  if (y == 0)
    return 1;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;      // y = y/2
    x = (x * x) % p; // Change x to x^2
  }
  return res;
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll modinv(ll a, ll p) { return power(a, p - 2, p); }
vll adj[2 * MAXN];
ll LIS[2 * MAXN];
vll a;
vll dp;
void dfs(ll s, ll p) {
  auto it = lower_bound(all(dp), a[s]) - dp.begin();
  ll sz = dp.size();
  ll store;
  if (it != sz) {
    store = dp[it];
    dp[it] = a[s];
  } else {
    dp.pb(a[s]);
  }
  LIS[s] = dp.size();
  for (auto v : adj[s]) {
    if (v == p) {
      continue;
    }
    dfs(v, s);
  }

  if (it != sz) {
    dp[it] = store;
  } else {
    dp.resize(sz);
  }
  return;
}
int solve() {
  ll n;
  cin >> n;
  a.resize(n + 1);
  f(i, 0, n) { cin >> a[i + 1]; }
  f(i, 0, n - 1) {
    ll a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, 1);
  fo(i, 1, n) { cout << LIS[i] << endl; }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  // tests(){solve();}
  solve();
  return 0;
} /*#ifndef ONLINE_JUDGE
     #endif*/

// make_pair make_tuple  tuple<int,int,int>