Language: C++

#include<iostream>
#include<algorithm>
#include<list>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  // ここでストリームに obj を書き込みます。
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}


void dfs(vector<vector<int>>& adj,vector<int>& ans,const vector<li>& a,
         int parent=-1,int v=0){
  //  return;
  if(df)print("begin",v);
  static vector<li> lis;
  //  static vector<vector<li>> len;
  
  auto itr=lower_bound(lis.begin(),lis.end(),a[v]);
  int i=itr-lis.begin();
  int flag;
  if(flag=(itr!=lis.end())){
    //    len[i].push_back(a[v]);
    flag=*itr;
    *itr=a[v];
  }else{
    //    len.push_back(vector<li> {a[v]});
    lis.push_back(a[v]);
  }
  ans[v]=lis.size();
  if(df)print(lis);
  if(df)print(ans);
  for(int x:adj[v]){
    if(x==parent) continue;
    dfs(adj,ans,a,v,x);
  }
  
  if(flag){
    //    len[i].pop_back();
    *itr=flag;
  } else {
    lis.pop_back();
    //    len.pop_back();
  }
  if(df)print("done",v);
}

int main(){
  int n; cin >>n;
  vector<li> a(n);
  rep(i,n) cin >> a[i];
  vector<vector<int>> adj(n);
  rep(i,n-1){
    int u,v; cin >>u >>v; u--; v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(df)rep(i,n)print(adj[i]);
  vector<int>  ans(n,-1);
  if(df)rep(i,n) print(ans[i]);
  dfs(adj,ans,a);
  
  if(df)print("done",ans);
  rep(i,n){
    print(ans[i]);
  }
  print("done");
  return 0;
  // print(ans);
}
#define df 0
#include<iostream>
#include<algorithm>
#include<list>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  // ここでストリームに obj を書き込みます。
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}


void dfs(vector<vector<int>>& adj,vector<int>& ans,const vector<li>& a,
         int parent=-1,int v=0){
  //  return;
  if(df)print("begin",v);
  static vector<li> lis;
  //  static vector<vector<li>> len;
  
  auto itr=lower_bound(lis.begin(),lis.end(),a[v]);
  int i=itr-lis.begin();
  int flag;
  if(flag=(itr!=lis.end())){
    //    len[i].push_back(a[v]);
    flag=*itr;
    *itr=a[v];
  }else{
    //    len.push_back(vector<li> {a[v]});
    lis.push_back(a[v]);
  }
  ans[v]=lis.size();
  if(df)print(lis);
  if(df)print(ans);
  for(int x:adj[v]){
    if(x==parent) continue;
    dfs(adj,ans,a,v,x);
  }
  
  if(flag){
    //    len[i].pop_back();
    *itr=flag;
  } else {
    lis.pop_back();
    //    len.pop_back();
  }
  if(df)print("done",v);
}

int main(){
  int n; cin >>n;
  vector<li> a(n);
  rep(i,n) cin >> a[i];
  vector<vector<int>> adj(n);
  rep(i,n-1){
    int u,v; cin >>u >>v; u--; v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(df)rep(i,n)print(adj[i]);
  vector<int>  ans(n,-1);
  if(df)rep(i,n) print(ans[i]);
  dfs(adj,ans,a);
  
  if(df)print("done",ans);
  rep(i,n){
    print(ans[i]);
  }
  if(df)print("done");
  return 0;
  // print(ans);
}


Compiler output:
a.cc:92:24: error: redefinition of 'template<class T> void print(const T&)'
   92 | template<class T> void print(const T& t){ cout << t << "\n"; }
      |                        ^~~~~
a.cc:9:24: note: 'template<class T> void print(const T&)' previously declared here
    9 | template<class T> void print(const T& t){ cout << t << "\n"; }
      |                        ^~~~~
a.cc:93:37: error: redefinition of 'template<class T, class ... Ts> void print(const T&, const Ts& ...)'
   93 | template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
      |                                     ^~~~~
a.cc:10:37: note: 'template<class T, class ... Ts> void print(const T&, const Ts& ...)' previously declared here
   10 | template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
      |                                     ^~~~~
a.cc:96:15: error: redefinition of 'template<template<class ...> class Ctn, class T> std::ostream& operator<<(std::ostream&, const Ctn<T>&)'
   96 | std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
      |               ^~~~~~~~
a.cc:13:15: note: 'template<template<class ...> class Ctn, class T> std::ostream& operator<<(std::ostream&, const Ctn<T>&)' previously declared here
   13 | std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
      |               ^~~~~~~~
a.cc:107:6: error: redefinition of 'void dfs(std::vector<std::vector<int> >&, std::vector<int>&, const std::vector<long long int>&, int, int)'
  107 | void dfs(vector<vector<int>>& adj,vector<int>& ans,const vector<li>& a,
      |      ^~~
a.cc:24:6: note: 'void dfs(std::vector<std::vector<int> >&, std::vector<int>&, const std::vector<long long int>&, int, int)' previously defined here
   24 | void dfs(vector<vector<int>>& adj,vector<int>& ans,const vector<li>& a,
      |      ^~~
a.cc:143:5: error: redefinition of 'int main()'
  143 | int main(){
      |     ^~~~
a.cc:60:5: note: 'int main()' previously defined here
   60 | int main(){
      |     ^~~~
a.cc: In instantiation of 'void print(const T&) [with T = std::vector<long long int>]':
a.cc:43:14:   required from here
   43 |   if(df)print(lis);
      |         ~~~~~^~~~~
a.cc:9:48: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'const std::vector<long long int>')
    9 | template<class T> void print(const T& t){ cout << t << "\n"; }
      |                                           ~~~~~^~~~
In file included from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/ostream:116:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(__ostream_type& (*)(__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  116 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |       ^~~~~~~~
/usr/include/c++/14/ostream:116:36: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)' {aka 'std::basic_ostream<char>& (*)(std::basic_ostream<char>&)'}
  116 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:125:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(__ios_type& (*)(__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>; __ios_type = std::basic_ios<char>]'
  125 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/usr/include/c++/14/ostream:125:32: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&)' {aka 'std::basic_ios<char>& (*)(std::basic_ios<char>&)'}
  125 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |                  ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:135:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  135 |       operator<<(ios_base& (*__pf) (ios_base&))
      |       ^~~~~~~~
/usr/include/c++/14/ostream:135:30: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'std::ios_base& (*)(std::ios_base&)'
  135 |       operator<<(ios_base& (*__pf) (ios_base&))
      |                  ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/ostream:174:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  174 |       operator<<(long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:174:23: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'long int'
  174 |       operator<<(long __n)
      |                  ~~~~~^~~
/usr/include/c++/14/ostream:178:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  178 |       operator<<(unsigned long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:178:32: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'long unsigned int'
  178 |       operator<<(unsigned long __n)
      |                  ~~~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:182:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  182 |       operator<<(bool __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:182:23: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'bool'
  182 |       operator<<(bool __n)
      |                  ~~~~~^~~
In file included from /usr/include/c++/14/ostream:1022:
/usr/include/c++/14/bits/ostream.tcc:96:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]'
   96 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/ostream.tcc:97:22: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'short int'
   97 |     operator<<(short __n)
      |                ~~~~~~^~~
/usr/include/c++/14/ostream:189:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  189 |       operator<<(unsigned short __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:189:33: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'short unsigned int'
  189 |       operator<<(unsigned short __n)
      |                  ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/ostream.tcc:110:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]'
  110 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/ostream.tcc:111:20: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'int'
  111 |     operator<<(int __n)
      |                ~~~~^~~
/usr/include/c++/14/ostream:200:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  200 |       operator<<(unsigned int __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:200:31: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'unsigned int'
  200 |       operator<<(unsigned int __n)
      |                  ~~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:211:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  211 |       operator<<(long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:211:28: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'long long int'
  211 |       operator<<(long long __n)
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/ostream:215:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  215 |       operator<<(unsigned long long __n)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:215:37: note:   no known conversion for argument 1 from 'const std::vector<long long int>' to 'long long unsigned int'
  215 |       operator<<(unsigned long long __n)
      |                  ~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/ostream:231:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]'
  231 |       operator<<(double __f)
      |       ^~~~~~~~
/usr/include/c++/14/ostream:231:25: note:   no known conversion for argument 1 from 'const std::vector<long