Language: C++

#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
const ll MOD=1e9+7;

int lb[112345];
int rb[112345];

const int MAX_N=(1<<18);
const ll INF=(1ll<<60);
int nn=MAX_N;

struct Seg{
 int mn,ind;
  Seg(int d=1000000,int i=100000){
    mn=d;
    ind=i;
  }
  static Seg e;
};
vector<Seg> dat(2*MAX_N);
Seg Seg::e=Seg();

Seg operator+(Seg l,Seg r){
  if(l.mn>r.mn)
    swap(l,r);
  return l;
}
//ostream& operator<<(ostream &os, const Seg &t) { return os<<"["<<t.mn<<")";}
template<class T=Seg> void upd(int k,T a,vector<T> &dat=dat){
  k+=dat.size()/2-1;
  dat[k]=a;
  while(k>0){
    k=(k-1)/2;
    dat[k]=dat[k*2+1]+dat[k*2+2];
  }
}

//(l,r,0,0,nn)
template<class T=Seg> T query(int a,int b,const vector<T> &dat=dat,int k=0,int l=0,int r=-1){
  if(r<0) r=dat.size()/2;
  if(r<=a || b<=l)return T::e;
  if(a<=l && r<=b) return dat[k];
  T lv=query(a,b,dat,k*2+1,l,(l+r)/2) ,rv= query(a,b,dat,k*2+2,(l+r)/2,r);
  return lv+rv;
}

pii dfs(vv<int> &g,vector<int> &usd,int v,int p,int l,int r){
  MX(lb[v],l);
  MN(rb[v],r);
  for(int w:g[v]) if(w!=p){
      dfs(g,usd,w,v,lb[v]-1,rb[v]+1);
      MX(lb[v],lb[w]-1);
      MN(rb[v],rb[w]+1);
    }
  //cout<<v<<pii(lb[v],rb[v])<<endl;
  return pii(lb[v],rb[v]);
}

int main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);
  int n;
  cin>>n;
  fill(lb,lb+n,-MOD);
  fill(rb,rb+n,MOD);
  vv<int> g(n);
  rep(i,n-1){
    int x,y;
    cin>>x>>y; --x; --y;
    g[x].pb(y);
    g[y].pb(x);
  }
  vector<int> vs(n);
  vector<int> nums(n);
  int t,poyo;
  cin>>t;
  rep(i,t){
    int x,y;
    cin>>x>>y; --x;
    poyo=x;
    vs[x]=1;
    nums[x]=y;
    MX(lb[x],nums[x]);
    MN(rb[x],nums[x]);
  }
  rep(hoge,2) dfs(g,vs,poyo,-1,nums[poyo],nums[poyo]);
  rep(i,n) dat[nn-1+i]=Seg(rb[i]-lb[i],i);
  if(query(0,n).mn<0){
    cout<<"No"<<endl;
    return 0;
  }
  rrep(i,nn-1) dat[i]=dat[2*i+1]+dat[2*i+2];
  vector<int> f(n);
  vector<int> re(n);
  rep(hoge,n){
    auto q=query(0,n);
    if(q.mn<0){
      cout<<"No"<<endl;
      return 0;
    }
    int i=q.ind;
    upd(i,Seg());
    f[i]=1;
    re[i]=lb[i];
    for(int v:g[i]) if(!f[v]){
	MX(lb[v],re[i]-1);
	MN(rb[v],re[i]+1);
	upd(v,Seg(rb[v]-lb[v],v));
      }
  }
  int f=1;
  rep(i,n) for(int v:g[i]) if(abs(re[v]-re[i])>1) f=0;
  if(f){
    cout<<"Yes"<<endl;
    for(int x:re) cout<<x<<endl;
  }else{
    cout<<"No"<<endl;
  }
  //cout<<re;
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:155:7: error: conflicting declaration 'int f'
  155 |   int f=1;
      |       ^
a.cc:137:15: note: previous declaration as 'std::vector<int, std::allocator<int> > f'
  137 |   vector<int> f(n);
      |               ^
a.cc:156:53: error: no match for 'operator=' (operand types are 'std::vector<int, std::allocator<int> >' and 'int')
  156 |   rep(i,n) for(int v:g[i]) if(abs(re[v]-re[i])>1) f=0;
      |                                                     ^
In file included from /usr/include/c++/14/vector:72,
                 from a.cc:2:
/usr/include/c++/14/bits/vector.tcc:210:5: note: candidate: 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]'
  210 |     vector<_Tp, _Alloc>::
      |     ^~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:211:42: note:   no known conversion for argument 1 from 'int' to 'const std::vector<int, std::allocator<int> >&'
  211 |     operator=(const vector<_Tp, _Alloc>& __x)
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/c++/14/vector:66:
/usr/include/c++/14/bits/stl_vector.h:766:7: note: candidate: 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = int; _Alloc = std::allocator<int>]'
  766 |       operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:766:26: note:   no known conversion for argument 1 from 'int' to 'std::vector<int, std::allocator<int> >&&'
  766 |       operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
      |                 ~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:788:7: note: candidate: 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = int; _Alloc = std::allocator<int>]'
  788 |       operator=(initializer_list<value_type> __l)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:788:46: note:   no known conversion for argument 1 from 'int' to 'std::initializer_list<int>'
  788 |       operator=(initializer_list<value_type> __l)
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
a.cc:157:6: error: could not convert 'f' from 'std::vector<int, std::allocator<int> >' to 'bool'
  157 |   if(f){
      |      ^
      |      |
      |      std::vector<int, std::allocator<int> >