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

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

struct coef {
  int r, c, s, i;
  coef(int R = 0, int C = 0, int S = 0, int I = 0) : r(R), c(C), s(S), i(I) {}
};

void countState(coef &d, string s) {
  for (auto c : s) {
    switch (c) {
    case '(':
      d.r++;
      break;
    case '{':
      d.c++;
      break;
    case '[':
      d.s++;
      break;
    case ')':
      d.r--;
      break;
    case '}':
      d.c--;
      break;
    case ']':
      d.s--;
      break;
    }
  }
}

int main(void) {
  int p, q;
  while (cin >> p >> q, p) {
    vector<coef> data(p + 1);
    for (int i = 1; i <= p; i++) {
      string s;
      cin >> s;
      data[i] = data[i - 1];
      countState(data[i], s);
      for (auto c : s) {
        if (c == '.') {
          data[i - 1].i++;
        } else
          break;
      }
    }

    vector<coef> ansset;
    for (int r = 1; r <= 20; r++) {
      for (int c = 1; c <= 20; c++) {
        for (int s = 1; s <= 20; s++) {
          bool ok = true;
          for (int i = 1; i <= p; i++) {
            ok &= (r * data[i].r + c * data[i].c + s * data[i].s == data[i].i);
          }
          if (ok) {
            ansset.emplace_back(r, c, s);
          }
        }
      }
    }

    coef state;
    for (int i = 0; i < q; i++) {
      string s;
      cin >> s;
      state.i = 0;
      set<int> ans;
      for (auto a : ansset) {
        ans.insert(a.r * state.r + a.c * state.c + a.s * state.s);
        if (ans.size() > 1) {
          break;
        }
      }
      if (ans.size() == 1) {
        cout << *ans.begin();
      } else {
        cout << -1;
      }
      if (i + 1 == q) {
        cout << endl;
      } else {
        cout << " ";
      }
      countState(state, s);
    }
  }
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define MOD ((ll)1e9 + 7)
void get() {}
template <class H, class... T> void get(H &h, T &...t) {
  cin >> h;
  get(t...);
}
template <class T> void geti(T &a) {
  for (auto &_ : a)
    cin >> _;
}
template <class T> void getii(T &a) {
  for (auto &_ : a)
    geti(_);
}
void put(string d) {}
template <class H, class... T> void put(string d, H &h, T &...t) {
  cout << h;
  if (sizeof...(t))
    cout << d;
  put(d, t...);
}
template <class T> void puti(T &a, string d = " ") {
  bool f = 1;
  for (auto &_ : a)
    cout << (exchange(f, 0) ? "" : d) << _;
  cout << endl;
}
template <class T> void putii(T &a, string d = " ") {
  for (auto &_ : a)
    puti(_, d);
}
#define dcl(...)                                                               \\
  ll __VA_ARGS__;                                                              \\
  get(__VA_ARGS__)
#define dclt(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  get(__VA_ARGS__)
template <class F, class S> void add(map<F, S> &mp, F f, S s) {
  if (mp.find(f) == mp.end())
    mp[f] = F();
  mp[f] += s;
}

int main() {
  while (1) {
    dcl(p, q);
    if (p == 0)
      break;
    vector<vector<pii>> edge(p + 1);
    vector<ll> val(p + 1, LLONG_MAX);
    rep(query, q) {
      char d;
      cin >> d;
      if (d == '!') {
        ll a, b, c;
        cin >> a >> b >> c;
        edge[a].eb(b, c);
        edge[b].eb(a, -c);
      } else {
        ll a, b;
        cin >> a >> b;
        if (val[a] != LLONG_MAX && val[b] != LLONG_MAX) {
          cout << val[b] - val[a] << endl;
        } else {
          bool sw = val[b] != LLONG_MAX;
          if (sw) {
            swap(a, b);
          }
          bool michi = false;
          auto cp = val;
          if (val[a] == LLONG_MAX) {
            michi = true;
            val = vector<ll>(p + 1, LLONG_MAX);
            val[a] = 0;
          }
          queue<int> que;
          que.emplace(a);

          vector<bool> search(p + 1, false);
          while (!que.empty()) {
            int index = que.front();
            que.pop();
            for (auto l : edge[index]) {
              if (search[l.fs] == false) {
                if (michi && cp[index] != LLONG_MAX) {
                  ll diff = cp[index] - val[index];
                  for (int i = 0; i <= p; i++) {
                    if (val[i] != LLONG_MAX) {
                      cp[i] = val[i] + diff;
                    }
                  }
                  val = cp;
                  if (val[b] != LLONG_MAX) {
                    goto fin;
                  }
                  fill(all(search), false);
                  queue<int> tmp;
                  que = tmp;
                  que.push(b);
                  val = vector<ll>(p + 1, LLONG_MAX);
                  val[b] = 0;
                  break;
                } else {
                  val[l.fs] = val[index] + l.sc;
                  que.emplace(l.fs);
                  search[l.fs] = true;
                  if (l.fs == b) {
                    goto fin;
                  }
                }
              }
            }
          }
        fin:
          if (val[b] == LLONG_MAX) {
            cout << "UNKNOWN" << endl;
          } else {
            if (sw)
              swap(a, b);
            cout << val[b] - val[a] << endl;
          }
        }
      }
    }
  }
  return 0;
}