Problem p02373 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

int n;
vector<int> vs[100005];

int ans[100005];

set<int> s[100005];

void dfs() {
  vector<int> stc;
  stc.push_back(0);
  {
    for (int i = 0; i != stc.size(); i++) {
      int no = stc[i];
      rep(j, vs[no].size()) { stc.push_back(vs[no][j]); }
    }
  }

  irep(k, stc.size()) {
    int no = stc[k];

    rep(i, vs[no].size()) {
      int to = vs[no][i];
      if (s[to].size() > s[no].size())
        swap(s[no], s[to]);

      for (set<int>::iterator ite = s[to].begin(); ite != s[to].end(); ite++) {
        int p = *ite;
        if (s[no].count(p) >= 1) {
          s[no].erase(p);
          ans[p] = no;
        } else {
          s[no].insert(p);
        }
      }
    }
  }
}

int main(void) {
  scanf("%d", &n);
  rep(i, n) {
    int k;
    scanf("%d", &k);
    rep(j, k) {
      int a;
      scanf("%d", &a);
      vs[i].push_back(a);
    }
  }

  int q;
  scanf("%d", &q);
  rep(i, q) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a == b)
      ans[i] = a;
    else {
      s[a].insert(i);
      s[b].insert(i);
    }
  }

  dfs();

  rep(i, q) { printf("%d\n", ans[i]); }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

int n;
vector<int> vs[100005];

int ans[100005];

set<int> s[100005];

void dfs() {
  vector<int> stc;
  stc.push_back(0);
  {
    for (int i = 0; i != stc.size(); i++) {
      int no = stc[i];
      rep(j, vs[no].size()) { stc.push_back(vs[no][j]); }
    }
  }

  irep(k, stc.size()) {
    int no = stc[k];

    rep(i, vs[no].size()) {
      int to = vs[no][i];
      if (s[to].size() > s[no].size())
        swap(s[no], s[to]);

      for (set<int>::iterator ite = s[to].begin(); ite != s[to].end(); ite++) {
        int p = *ite;
        if (s[no].count(p) >= 1) {
          s[no].erase(p);
          ans[p] = no;
        } else {
          s[no].insert(p);
        }
      }
      s[to].clear();
    }
  }
}

int main(void) {
  scanf("%d", &n);
  rep(i, n) {
    int k;
    scanf("%d", &k);
    rep(j, k) {
      int a;
      scanf("%d", &a);
      vs[i].push_back(a);
    }
  }

  int q;
  scanf("%d", &q);
  rep(i, q) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a == b)
      ans[i] = a;
    else {
      s[a].insert(i);
      s[b].insert(i);
    }
  }

  dfs();

  rep(i, q) { printf("%d\n", ans[i]); }

  return 0;
}