Language: C++

#include<bits/stdc++.h>
#define INF (1e9)
#define N 100005
using namespace std;
typedef pair<int,int> P;
int n,a,b,k,v,p,ans[N];
vector<int> e[N];
P minmax[N];
bool flag=true;

P rmerge(P A,P B){
  return P(max(A.first,B.first),min(A.second,B.second));
}

P dfs(int x,int px){
  P res=minmax[x];
  for(int i=0;i<(int)e[x].size();i++)
    if(e[x][i]!=px)res=rmerge(res,dfs(e[x][i],x));
  return minmax[x]=res;
}

void dfs2(int x,int px,int v){
  if(!flag)return;
  ans[x]=v;
  for(int i=0;i<(int)e[x].size();i++){
    int nv,nx=e[x][i];
    int minv=minmax[nx].first;
    int maxv=minmax[nx].second;
    if(nx!=px){
      if(minv<=ans[x]+1&&ans[x]+1<=maxv)dfs2(e[x][i],x,ans[x]+1);
      else if(minv<=ans[x]-1&&ans[x]-1<=maxv)dfs2(e[x][i],x,ans[x]-1);
      else{
	flag=false;
	break;
      }
    }
  }
}

int main(){
  cin>>n;
  for(int i=0;i<n-1;i++){
    cin>>a>>b; a--; b--;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  for(int i=0;i<n;i++)
    ans[i]=-INF;
  cin>>k;
  for(int i=0;i<k;i++){
    cin>>v>>p;
    ans[v-1]=p;
  }
  for(int i=0;i<n;i++){
    minmax[i].first=-INF;
    minmax[i].second=INF;
    if(ans[i]!=-INF){
      minmax[i].first=ans[i]-1;
      minmax[i].second=ans[i]+1;
    }
  }
  dfs(0,-1);
  int f=0;
  for(int i=0;i<n;i++){
    if(ans[i]!=-INF){
      dfs2(i,-1,ans[i]);
      f=1;
      break;
    }
  }
  if(!f)dfs2(0,-1,0);
  if(flag){
    cout<<"Yes"<<endl;
    for(int i=0;i<n;i++)cout<<ans[i]<<endl;
  }else cout<<"No"<<endl;
  return 0;
}


Compiler output:
a.cc: In function 'P dfs(int, int)':
a.cc:16:9: error: reference to 'minmax' is ambiguous
   16 |   P res=minmax[x];
      |         ^~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc:19:10: error: reference to 'minmax' is ambiguous
   19 |   return minmax[x]=res;
      |          ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc: In function 'void dfs2(int, int, int)':
a.cc:27:14: error: reference to 'minmax' is ambiguous
   27 |     int minv=minmax[nx].first;
      |              ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc:28:14: error: reference to 'minmax' is ambiguous
   28 |     int maxv=minmax[nx].second;
      |              ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc: In function 'int main()':
a.cc:55:5: error: reference to 'minmax' is ambiguous
   55 |     minmax[i].first=-INF;
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc:56:5: error: reference to 'minmax' is ambiguous
   56 |     minmax[i].second=INF;
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc:58:7: error: reference to 'minmax' is ambiguous
   58 |       minmax[i].first=ans[i]-1;
      |       ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~
a.cc:59:7: error: reference to 'minmax' is ambiguous
   59 |       minmax[i].second=ans[i]+1;
      |       ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3447:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>, _Compare)'
 3447 |     minmax(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3435:5: note:                 'template<class _Tp> constexpr std::pair<_FIter, _FIter> std::minmax(initializer_list<_Tp>)'
 3435 |     minmax(initializer_list<_Tp> __l)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3309:5: note:                 'template<class _Tp, class _Compare> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&, _Compare)'
 3309 |     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h:3288:5: note:                 'template<class _Tp> constexpr std::pair<const _Tp&, const _Tp&> std::minmax(const _Tp&, const _Tp&)'
 3288 |     minmax(const _Tp& __a, const _Tp& __b)
      |     ^~~~~~
a.cc:8:3: note:                 'P minmax [100005]'
    8 | P minmax[N];
      |   ^~~~~~