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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n, m;
  cin >> n >> m;

  vector<string> s(n);
  for (Int i = 1; i < n; i++)
    cin >> s[i];

  using P = pair<Int, Int>;
  vector<vector<Int>> G(n);
  for (Int i = 1; i < n; i++) {
    Int u, v;
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  vector<Int> c(n, 0);
  {
    Int x = 0b011;
    Int y = 0b101;
    vector<Int> w({0, x & y, (x | y) ^ y, x, (x | y) ^ x, y, x ^ y, x | y});
    int cnt = 0;
    function<void(Int, Int)> dfs = [&](Int v, Int p) {
      cnt++;
      for (Int u : G[v]) {
        if (u == p)
          continue;
        if (s[u] == "T=T&X")
          c[u] = c[v] & x;
        if (s[u] == "T=T&Y")
          c[u] = c[v] & y;
        if (s[u] == "T=T|X")
          c[u] = c[v] | x;
        if (s[u] == "T=T|Y")
          c[u] = c[v] | y;
        if (s[u] == "T=T^X")
          c[u] = c[v] ^ x;
        if (s[u] == "T=T^Y")
          c[u] = c[v] ^ y;
        dfs(u, v);
      }
    };
    dfs(0, -1);
    assert(cnt == n);
  }

  map<vector<Int>, Int> ans;

  for (Int i = 0; i < m; i++) {
    Int x, y;
    cin >> x >> y;
    vector<Int> w({0, x & y, (x | y) ^ y, x, (x | y) ^ x, y, x ^ y, x | y});
    function<Int(Int, Int, Int)> dfs = [&](Int v, Int p, Int d) {
      Int res = -1;
      for (Int u : G[v]) {
        if (u == p)
          continue;
        {
          if (s[u] == "T=T&X")
            assert(w[c[u]] == (w[c[v]] & x));
          if (s[u] == "T=T&Y")
            assert(w[c[u]] == (w[c[v]] & y));
          if (s[u] == "T=T|X")
            assert(w[c[u]] == (w[c[v]] | x));
          if (s[u] == "T=T|Y")
            assert(w[c[u]] == (w[c[v]] | y));
          if (s[u] == "T=T^X")
            assert(w[c[u]] == (w[c[v]] ^ x));
          if (s[u] == "T=T^Y")
            assert(w[c[u]] == (w[c[v]] ^ y));
        }
        Int e = dfs(u, v, !d);
        if (res < 0)
          res = e;
        if (!d && (w[e] > w[res] || (w[e] == w[res] && e > res)))
          res = e;
        if (d && (w[e] < w[res] || (w[e] == w[res] && e < res)))
          res = e;
      }
      if (res < 0)
        res = c[v];
      return res;
    };

    vector<P> v;
    for (Int i = 0; i < 8; i++)
      v.emplace_back(w[i], i);
    sort(v.begin(), v.end());
    vector<Int> u(8);
    for (Int i = 0; i < 8; i++)
      u[i] = v[i].second;
    // for(int a:u) cout<<a<<" ";cout<<endl;
    if (!ans.count(u))
      ans[u] = dfs(0, -1, 0);
    cout << w[ans[u]] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n, m;
  cin >> n >> m;

  vector<string> s(n);
  for (Int i = 1; i < n; i++)
    cin >> s[i];

  using P = pair<Int, Int>;
  vector<vector<Int>> G(n);
  for (Int i = 1; i < n; i++) {
    Int u, v;
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  map<vector<Int>, Int> ans;

  for (Int i = 0; i < m; i++) {
    Int x, y;
    cin >> x >> y;
    vector<Int> w({0, x & y, (x | y) ^ y, x, (x | y) ^ x, y, x ^ y, x | y});
    vector<Int> r(1 << 16, -1);
    for (Int i = 0; i < 8; i++)
      r[w[i]] = i;
    vector<Int> c(n, 0);
    function<Int(Int, Int, Int)> dfs = [&](Int v, Int p, Int d) {
      set<Int> si;
      for (Int u : G[v]) {
        if (u == p)
          continue;
        if (s[u] == "T=T&X")
          c[u] = c[v] & x;
        if (s[u] == "T=T&Y")
          c[u] = c[v] & y;
        if (s[u] == "T=T|X")
          c[u] = c[v] | x;
        if (s[u] == "T=T|Y")
          c[u] = c[v] | y;
        if (s[u] == "T=T^X")
          c[u] = c[v] ^ x;
        if (s[u] == "T=T^Y")
          c[u] = c[v] ^ y;
        si.emplace(dfs(u, v, !d));
      }
      if (si.empty())
        return r[c[v]];
      Int res = *si.begin();
      for (Int e : si) {
        if (!d && w[e] > w[res])
          res = e;
        if (d && w[e] < w[res])
          res = e;
      }
      return res;
    };

    vector<P> v;
    for (Int i = 0; i < 8; i++)
      v.emplace_back(w[i], i);
    sort(v.begin(), v.end());
    vector<Int> u(8);
    for (Int i = 0; i < 8; i++)
      u[i] = v[i].second;

    if (!ans.count(u))
      ans[u] = dfs(0, -1, 0);
    cout << w[ans[u]] << endl;
  }
  assert(ans.size() < 100);
  return 0;
}