Problem p03949 (C++)

Buggy submission (status: Time Limit Exceeded):
/*  -*- coding: utf-8 -*-
 *
 * c.cc: E: 木と整数 / Integers on a Tree
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;

typedef long long ll;

const int INF = 1 << 30;
const ll LINF = 1LL << 60;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int, int> pii;

struct Node {
  int v, minv, maxv, p;
  Node() : v(INF), minv(-INF), maxv(INF), p(-1) {}
  Node(int _v) : v(_v), minv(_v), maxv(_v), p(_v & 1) {}
  void print() {
    printf("Node(v=%d, minv=%d, maxv=%d, p=%d)\n", v, minv, maxv, p);
  }
};

/* global variables */

vi nbrs[MAX_N];
int vs[MAX_N], ps[MAX_N];
Node nds[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    nds[i] = Node();

  for (int i = 0; i < n - 1; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--, bi--;
    nbrs[ai].push_back(bi);
    nbrs[bi].push_back(ai);
  }

  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> vs[i] >> ps[i];
    vs[i]--;
    nds[vs[i]] = Node(ps[i]);
  }

  for (int i = 0; i < k; i++) {
    int st = vs[i];
    queue<pii> q;
    q.push(pii(st, -1));

    while (!q.empty()) {
      pii u = q.front();
      q.pop();
      int &ui = u.first, &uprt = u.second;
      Node &ndu = nds[ui];
      int vminv = ndu.minv - 1, vmaxv = ndu.maxv + 1, vp = ndu.p ^ 1;
      vi &nbru = nbrs[ui];
      for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
        if (*vit == uprt)
          continue;

        int &vi = *vit;
        Node &ndv = nds[vi];
        if (ndv.p >= 0 &&
            (ndv.p != vp || ndv.minv > vmaxv || ndv.maxv < vminv)) {
          puts("No");
          return 0;
        }

        ndv.minv = max(ndv.minv, vminv);
        ndv.maxv = min(ndv.maxv, vmaxv);
        ndv.p = vp;

        if (ndv.v >= INF)
          q.push(pii(vi, ui));
      }
    }
  }

  puts("Yes");

  // for (int i = 0; i < n; i++) printf("%d: ", i), nds[i].print();
  for (int i = 0; i < n; i++)
    printf("%d\n", nds[i].minv);

  return 0;
}


Fixed submission:
/*  -*- coding: utf-8 -*-
 *
 * c.cc: E: 木と整数 / Integers on a Tree
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;

typedef long long ll;

const int INF = 1 << 30;
const ll LINF = 1LL << 60;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int, int> pii;

struct Node {
  int v, minv, maxv, p;
  Node() : v(INF), minv(-INF), maxv(INF), p(-1) {}
  Node(int _v) : v(_v), minv(_v), maxv(_v), p(_v & 1) {}
  void print() {
    printf("Node(v=%d, minv=%d, maxv=%d, p=%d)\n", v, minv, maxv, p);
  }
};

/* global variables */

vi nbrs[MAX_N];
int vs[MAX_N], ps[MAX_N];
Node nds[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    nds[i] = Node();

  for (int i = 0; i < n - 1; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--, bi--;
    nbrs[ai].push_back(bi);
    nbrs[bi].push_back(ai);
  }

  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> vs[i] >> ps[i];
    vs[i]--;
    nds[vs[i]] = Node(ps[i]);
  }

  for (int i = 0; i < k; i++) {
    int st = vs[i];
    queue<pii> q;
    q.push(pii(st, -1));

    while (!q.empty()) {
      pii u = q.front();
      q.pop();
      int &ui = u.first, &uprt = u.second;
      Node &ndu = nds[ui];
      int vminv = ndu.minv - 1, vmaxv = ndu.maxv + 1, vp = ndu.p ^ 1;
      vi &nbru = nbrs[ui];
      for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
        if (*vit == uprt)
          continue;

        int &vi = *vit;
        Node &ndv = nds[vi];
        if (ndv.p >= 0 &&
            (ndv.p != vp || ndv.minv > vmaxv || ndv.maxv < vminv)) {
          puts("No");
          return 0;
        }

        if (ndv.minv < vminv || ndv.maxv > vmaxv) {
          ndv.minv = max(ndv.minv, vminv);
          ndv.maxv = min(ndv.maxv, vmaxv);
          ndv.p = vp;
          if (ndv.v >= INF)
            q.push(pii(vi, ui));
        }
      }
    }
  }

  puts("Yes");

  // for (int i = 0; i < n; i++) printf("%d: ", i), nds[i].print();
  for (int i = 0; i < n; i++)
    printf("%d\n", nds[i].minv);

  return 0;
}