Problem p01816 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s[100001];
int qq[100001];
vector<int> a[100001], check;
vector<pair<int, int>> dig;
#define mk make_pair
map<vector<int>, int> visited;
int n, q, x, y;
void digda(int val, int dep) {
  if (!dep)
    qq[dep] = 0;
  else if (s[dep] == "T=T|X")
    qq[dep] = val | x;
  else if (s[dep] == "T=T|Y")
    qq[dep] = val | y;
  else if (s[dep] == "T=T^X")
    qq[dep] = val ^ x;
  else if (s[dep] == "T=T^Y")
    qq[dep] = val ^ y;
  else if (s[dep] == "T=T&X")
    qq[dep] = val & x;
  else if (s[dep] == "T=T&Y")
    qq[dep] = val & y;
  r(i, a[dep].size()) digda(qq[dep], a[dep][i]);
}
void digging() {
  dig.push_back(mk(0, 1));
  dig.push_back(mk(x, 2));
  dig.push_back(mk(y, 3));
  dig.push_back(mk(x ^ y, 4));
  dig.push_back(mk(x & y, 5));
  dig.push_back(mk(x | y, 6));
  dig.push_back(mk(x ^ (x & y), 7));
  dig.push_back(mk(y ^ (x & y), 8));
}
int diggame(int pl, int turn) {
  if (a[pl].size() == 0)
    return qq[pl];
  int res;
  if (turn % 2) {
    res = 1e9;
    r(i, a[pl].size()) res = min(res, diggame(a[pl][i], turn + 1));
  } else {
    res = 0;
    r(i, a[pl].size()) res = max(res, diggame(a[pl][i], turn + 1));
  }
  return res;
}
int main() {
  cin >> n >> q;
  r(i, n - 1) cin >> s[i + 1];
  r(i, n - 1) {
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
  }
  while (q--) {
    scanf("%d%d", &x, &y);
    dig.clear();
    check.clear();
    memset(qq, 0, sizeof(qq));
    digging();
    sort(dig.begin(), dig.end());
    r(i, 8) check.push_back(dig[i].second);
    if (visited[check]) {
      int di = visited[check];
      if (di == 1)
        cout << 0 << endl;
      else if (di == 2)
        cout << x << endl;
      else if (di == 3)
        cout << y << endl;
      else if (di == 4)
        cout << (x ^ y) << endl;
      else if (di == 5)
        cout << (x & y) << endl;
      else if (di == 6)
        cout << (x | y) << endl;
      else if (di == 7)
        cout << (x ^ (x & y)) << endl;
      else if (di == 8)
        cout << (y ^ (x & y)) << endl;
    } else {
      digda(0, 0);
      int p = diggame(0, 0);
      cout << p << endl;
      r(i, 8) if (dig[i].first == p) visited[check] = dig[i].second;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s[100001];
int qq[100001];
vector<int> a[100001], check;
vector<pair<int, int>> dig;
#define mk make_pair
map<vector<int>, int> visited;
int n, q, x, y;
void digda(int val, int dep) {
  if (!dep)
    qq[dep] = 0;
  else if (s[dep] == "T=T|X")
    qq[dep] = val | x;
  else if (s[dep] == "T=T|Y")
    qq[dep] = val | y;
  else if (s[dep] == "T=T^X")
    qq[dep] = val ^ x;
  else if (s[dep] == "T=T^Y")
    qq[dep] = val ^ y;
  else if (s[dep] == "T=T&X")
    qq[dep] = val & x;
  else if (s[dep] == "T=T&Y")
    qq[dep] = val & y;
  r(i, a[dep].size()) digda(qq[dep], a[dep][i]);
}
void digging() {
  dig.push_back(mk(0, 1));
  dig.push_back(mk(x, 2));
  dig.push_back(mk(y, 3));
  dig.push_back(mk(x ^ y, 4));
  dig.push_back(mk(x & y, 5));
  dig.push_back(mk(x & y, 6));
  dig.push_back(mk(x ^ (x & y), 7));
  dig.push_back(mk(y ^ (x & y), 8));
}
int diggame(int pl, int turn) {
  if (a[pl].size() == 0)
    return qq[pl];
  int res;
  if (turn % 2) {
    res = 1e9;
    r(i, a[pl].size()) res = min(res, diggame(a[pl][i], turn + 1));
  } else {
    res = 0;
    r(i, a[pl].size()) res = max(res, diggame(a[pl][i], turn + 1));
  }
  return res;
}
int main() {
  cin >> n >> q;
  r(i, n - 1) cin >> s[i + 1];
  r(i, n - 1) {
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
  }
  while (q--) {
    scanf("%d%d", &x, &y);
    dig.clear();
    check.clear();
    memset(qq, 0, sizeof(qq));
    digging();
    sort(dig.begin(), dig.end());
    r(i, 8) check.push_back(dig[i].second);
    if (visited[check]) {

    } else {
      digda(0, 0);
      int p = diggame(0, 0);
      cout << p << endl;
    }
  }
}