Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

static const int INF = 0x3f3f3f3f;

int main() {
        int n;
        scanf("%d", &n);
        vector<vector<int>> g(n);
        for (int i = 0; i < n - 1; i ++) {
                int a, b;
                scanf("%d%d", &a, &b);
                a --, b --;
                g[a].push_back(b);
                g[b].push_back(a);
        }
        int k;
        scanf("%d", &k);
        vector<int> val(n, INF);
        while (k --) {
                int v, p;
                scanf("%d%d", &v, &p);
                v --;
                val[v] = p;
        }
        vector<pair<int, int>> seg(n);
        function<bool (int, int)> dfs = [&](int u, int prev) {
                vector<pair<int, int>> segs;
                for (auto v : g[u]) if (v != prev) {
                        if (!dfs(v, u)) return false;
                        if (seg[v].first != INF) {
                                segs.push_back(make_pair(seg[v].first - 1, seg[v].second + 1));
                        }
                }
                if (segs.empty()) {
                        if (val[u] != INF) {
                                seg[u] = make_pair(val[u], val[u]);
                        } else {
                                seg[u] = make_pair(INF, INF);
                        }
                        return true;
                }
                int l = 0, r = INF;
                for (int i = 0; i < segs.size(); i ++) {
                        l = max(l, segs[i].first);
                        r = min(r, segs[i].second);
                }
                if (val[u] != INF) {
                        if (val[u] < l || r < val[u]) {
                                return false;
                        } else {
                                l = val[u], r = val[u];
                        }
                }
                if (l > r) {
                        return false;
                }
                seg[u] = make_pair(l, r);
                return true;
        };
        if (!dfs(0, -1)) {
                return !printf("No\n");
        } 
        printf("Yes\n");
        vector<int> ans(n, INF);
        function<void (int, int)> decide = [&](int u, int prev) {
                for (auto v : g[u]) if (v != prev) {
                        if (seg[v].first > INF / 2) {
                                ans[v] = ans[u] + 1;
                        } else {
                                ans[v] = seg[v].first;
                        }
                        decide(v, u);
                }
        };
        ans[0] = seg[0].first;
        decide(0, -1);
        for (int i = 0; i < n; i ++) {
                printf("%d\n", ans[i]);
        }
        return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:33:9: error: 'function' was not declared in this scope
   33 |         function<bool (int, int)> dfs = [&](int u, int prev) {
      |         ^~~~~~~~
a.cc:8:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    7 | #include <set>
  +++ |+#include <functional>
    8 | using namespace std;
a.cc:33:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   33 |         function<bool (int, int)> dfs = [&](int u, int prev) {
      |                                ^
a.cc:33:18: error: expected primary-expression before 'bool'
   33 |         function<bool (int, int)> dfs = [&](int u, int prev) {
      |                  ^~~~
a.cc:67:14: error: 'dfs' was not declared in this scope
   67 |         if (!dfs(0, -1)) {
      |              ^~~
a.cc:72:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   72 |         function<void (int, int)> decide = [&](int u, int prev) {
      |                                ^
a.cc:72:18: error: expected primary-expression before 'void'
   72 |         function<void (int, int)> decide = [&](int u, int prev) {
      |                  ^~~~
a.cc:83:9: error: 'decide' was not declared in this scope
   83 |         decide(0, -1);
      |         ^~~~~~