Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

#define all(x) x.begin(), x.end()

const ll INF = 1e9;

ll n, m;
vector<int> graph[1030000];
ll p[1030000];
set<pair<int, int>> q_min;
set<pair<int, int>> q_max;
ll min_value[1030000];
ll max_value[1030000];
ll c[1030000];
ll mod2[1030000];

void dfs(int v, int value) {
  mod2[v] = value;
  if (p[v] != -INF && p[v] % 2 != value) {
    cout << "No";
    exit(0);
  }
  if (c[v] == 1)
    return;
  c[v] = 1;
  for (int j = 0; j < graph[v].size(); j++) {
    int u = graph[v][j];
    dfs(u, 1 - value);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fill(min_value, min_value + 100001, -INF);
  fill(max_value, max_value + 100001, -INF);
  fill(p, p + 100001, -INF);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  cin >> m;
  int a0, b0;
  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a0 = a, b0 = b;
    p[a] = b;
    q_min.insert({b, a});
    q_max.insert({b, a});
    min_value[a] = b;
    max_value[a] = b;
  }
  // write odd-even check
  for (int i = m; i < n;) {
    auto it = q_min.end();
    if (q_min.end() == q_min.begin()) {
      cout << "No";
      return 0;
    }
    it--;
    int v = it->second;
    int w = it->first;
    for (int j = 0; j < graph[v].size(); j++) {
      int u = graph[v][j];
      if (min_value[u] == -INF) {
        min_value[u] = w - 1;
        q_min.insert({w - 1, u});
        i++;
      } else {
        if (w - 1 >= min_value[u]) {
          q_min.erase({min_value[u], u});
          min_value[u] = w - 1;
          q_min.insert({w - 1, u});
        }
      }
    }
    q_min.erase(it);
  }

  for (int i = m; i < n;) {
    auto it = q_max.begin();
    if (q_max.end() == q_max.begin()) {
      cout << "No";
      return 0;
    }
    int v = it->second;
    int w = it->first;
    for (int j = 0; j < graph[v].size(); j++) {
      int u = graph[v][j];
      if (max_value[u] == -INF) {
        max_value[u] = w + 1;
        q_max.insert({w + 1, u});
        i++;
      } else {
        if (w + 1 <= max_value[u]) {
          q_max.erase({max_value[u], u});
          max_value[u] = w + 1;
          q_max.insert({w + 1, u});
        }
      }
    }
    q_max.erase(it);
  }
  for (int i = 1; i <= n; i++) {
    ; // cout << min_value[i] << " " << max_value[i] << endl;
    if (max_value[i] < min_value[i]) {
      cout << "No";
      return 0;
    }
  }
  dfs(a0, b0 % 2);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < graph[i].size(); j++) {
      int h = graph[i][j];
      if (abs(min_value[i] - min_value[h]) != 1) {
        cout << 100 / 0;
      }
    }
  }
  cout << "Yes\n";
  for (int i = 0; i < n; i++) {
    cout << min_value[i + 1] << "\n";
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

#define all(x) x.begin(), x.end()

const ll INF = 1e9;

ll n, m;
vector<int> graph[1030000];
ll p[1030000];
set<pair<int, int>> q_min;
set<pair<int, int>> q_max;
ll min_value[1030000];
ll max_value[1030000];
ll c[1030000];
ll mod2[1030000];

void dfs(int v, int value) {
  mod2[v] = value;
  if (p[v] != -INF && p[v] % 2 != value) {
    cout << "No";
    exit(0);
  }
  if (c[v] == 1)
    return;
  c[v] = 1;
  for (int j = 0; j < graph[v].size(); j++) {
    int u = graph[v][j];
    dfs(u, 1 - value);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fill(min_value, min_value + 100001, -INF);
  fill(max_value, max_value + 100001, -INF);
  fill(p, p + 100001, -INF);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  cin >> m;
  int a0, b0;
  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a0 = a, b0 = b;
    p[a] = b;
    q_min.insert({b, a});
    q_max.insert({b, a});
    min_value[a] = b;
    max_value[a] = b;
  }
  // write odd-even check
  for (int i = m; i < n;) {
    auto it = q_min.end();
    if (q_min.end() == q_min.begin()) {
      cout << "No";
      return 0;
    }
    it--;
    int v = it->second;
    int w = it->first;
    for (int j = 0; j < graph[v].size(); j++) {
      int u = graph[v][j];
      if (min_value[u] == -INF) {
        min_value[u] = w - 1;
        q_min.insert({w - 1, u});
        i++;
      } else {
        if (w - 1 >= min_value[u]) {
          q_min.erase({min_value[u], u});
          min_value[u] = w - 1;
          q_min.insert({w - 1, u});
        }
      }
    }
    q_min.erase(it);
  }

  for (int i = m; i < n;) {
    auto it = q_max.begin();
    if (q_max.end() == q_max.begin()) {
      cout << "No";
      return 0;
    }
    int v = it->second;
    int w = it->first;
    for (int j = 0; j < graph[v].size(); j++) {
      int u = graph[v][j];
      if (max_value[u] == -INF) {
        max_value[u] = w + 1;
        q_max.insert({w + 1, u});
        i++;
      } else {
        if (w + 1 <= max_value[u]) {
          q_max.erase({max_value[u], u});
          max_value[u] = w + 1;
          q_max.insert({w + 1, u});
        }
      }
    }
    q_max.erase(it);
  }
  for (int i = 1; i <= n; i++) {
    ; // cout << min_value[i] << " " << max_value[i] << endl;
    if (max_value[i] < min_value[i]) {
      cout << "No";
      return 0;
    }
  }
  dfs(a0, b0 % 2);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < graph[i].size(); j++) {
      int h = graph[i][j];
      if (abs(min_value[i] - min_value[h]) != 1) {
        cout << "No";
        return 0;
      }
    }
  }
  cout << "Yes\n";
  for (int i = 0; i < n; i++) {
    cout << min_value[i + 1] << "\n";
  }
}