Problem p02373 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;

vector<int> chi[100000];
int fir[100000], dep = 0;
vector<pair<int, int>> et; // Euler Tour

int min(int a, int b) { return a < b ? a : b; }

void dfs(int a) {
  int i;
  if (fir[a] == -1)
    fir[a] = et.size();
  et.push_back(make_pair(a, dep));
  for (i = 0; i < chi[a].size(); ++i) {
    dep++;
    dfs(chi[a][i]);
    dep--;
    et.push_back(make_pair(a, dep));
  }
}

int main(void) {
  int i, j, k, n, c, now, q, u, v;
  scanf("%d", &n);
  for (i = 0; i < n; ++i)
    fir[i] = -1;
  for (i = 0; i < n; ++i) {
    scanf("%d", &k);
    for (j = 0; j < k; ++j) {
      scanf("%d", &c);
      chi[i].push_back(c);
    }
  }
  dfs(0);
  int sm = 1;
  while (sm < et.size())
    sm *= 2;
  pair<int, int> seg[sm * 2];
  int las[sm * 2];
  for (i = sm; i < sm * 2; ++i) {
    if (i < sm + et.size())
      seg[i].first = et[i - sm].second, seg[i].second = et[i - sm].first;
    else
      seg[i] = make_pair(1000000, 1000000);
    if (i % 2) {
      now = i / 2;
      while (now)
        seg[now] = min(seg[now * 2], seg[now * 2 + 1]), now /= 2;
    }
  }
  now = 1;
  for (i = 1; i < sm * 2; ++i) {
    if (now * 2 == i)
      now *= 2;
    las[i] = (sm / now) * (i - now + 1) - 1;
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    int t;
    scanf("%d%d", &u, &v);
    if (fir[u] > fir[v])
      t = u, u = v, v = t;
    now = sm + fir[u];
    pair<int, int> min = make_pair(1000000, 1000000);
    while (1) {
      while (!(now % 2))
        now /= 2;
      while (las[now] > fir[v])
        now *= 2;
      if (min > seg[now])
        min = seg[now];
      if (las[now] == fir[v])
        break;
      now++;
    }
    printf("%d\\n", min.second);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;

vector<int> chi[100000];
int fir[100000], dep = 0;
vector<pair<int, int>> et; // Euler Tour

int min(int a, int b) { return a < b ? a : b; }

void dfs(int a) {
  int i;
  if (fir[a] == -1)
    fir[a] = et.size();
  et.push_back(make_pair(a, dep));
  for (i = 0; i < chi[a].size(); ++i) {
    dep++;
    dfs(chi[a][i]);
    dep--;
    et.push_back(make_pair(a, dep));
  }
}

int main(void) {
  int i, j, k, n, c, now, q, u, v;
  scanf("%d", &n);
  for (i = 0; i < n; ++i)
    fir[i] = -1;
  for (i = 0; i < n; ++i) {
    scanf("%d", &k);
    for (j = 0; j < k; ++j) {
      scanf("%d", &c);
      chi[i].push_back(c);
    }
  }
  dfs(0);
  /*for(i = 0; i < et.size(); ++i) printf("%d %d\\n", et[i].first, et[i].second);
  return 0;*/
  int sm = 1;
  while (sm < et.size())
    sm *= 2;
  pair<int, int> seg[sm * 2];
  int las[sm * 2];
  for (i = sm; i < sm * 2; ++i) {
    if (i < sm + et.size())
      seg[i].first = et[i - sm].second, seg[i].second = et[i - sm].first;
    else
      seg[i] = make_pair(1000000, 1000000);
    if (i % 2) {
      now = i / 2;
      while (now)
        seg[now] = min(seg[now * 2], seg[now * 2 + 1]), now /= 2;
    }
  }
  now = 1;
  for (i = 1; i < sm * 2; ++i) {
    if (now * 2 == i)
      now *= 2;
    las[i] = (sm / now) * (i - now + 1) - 1;
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d%d", &u, &v);
    now = sm + fir[u];
    pair<int, int> min = make_pair(1000000, 1000000);
    while (1) {
      while (!(now % 2))
        now /= 2;
      while (las[now] > fir[v])
        now *= 2;
      if (min > seg[now])
        min = seg[now];
      if (las[now] == fir[v])
        break;
      now++;
    }
    printf("%d\\n", min.second);
  }
  return 0;
}