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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;

int main() {
  int N;
  while (cin >> N && N) {
    char s[100000];
    char t[100000];
    vector<P> edge[200];
    map<string, int> id;
    REP(i, N) {
      int n, v1, v2;
      scanf(" 1 %s = 10^%d %s ", s, &n, t);
      string u1(s);
      string u2(t);
      if (!id.count(u1)) {
        int tmp = id.size();
        id[u1] = tmp;
      }
      if (!id.count(u2)) {
        int tmp = id.size();
        id[u2] = tmp;
      }
      v1 = id[u1], v2 = id[u2];
      edge[v1].push_back(P(v2, n));
      edge[v2].push_back(P(v1, -n));
    }
    int M = id.size();
    vector<int> value(M);
    fill(value.begin(), value.end(), INF);
    bool ans = true;
    REP(i, M) if (value[i] == INF) {
      queue<P> que;
      que.push(P(i, 0));
      while (!que.empty()) {
        P p = que.front();
        que.pop();
        int idx = p.first, t = p.second;
        if (value[idx] != INF) {
          if (value[idx] != t)
            ans = false;
          continue;
        }
        value[idx] = t;
        REP(j, edge[idx].size()) {
          P e = edge[idx][j];
          que.push(P(e.first, t + e.second));
        }
      }
    }
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
  int N;
  while (cin >> N) {
    if (!N)
      break;
    vector<string> tani;
    vector<int> ind;
    bool flag = true;
    bool first = true;
    int tmp;
    string tmp2;
    int count = 0;
    int date[100];
    string date2[100][2];
    while (N--) {
      string tani1, tani2;
      int index;
      cin >> tmp;
      cin >> tani1;
      cin >> tmp2;
      cin >> tmp2;
      index = (tmp2[3] != '-') ? (tmp2[3] - '0') : ('0' - tmp2[4]);
      cin >> tani2;
      if (first) {
        tani.push_back(tani1);
        tani.push_back(tani2);
        ind.push_back(0);
        ind.push_back(index);
        first = false;
        continue;
      }
      vector<string>::iterator it1 = find(tani.begin(), tani.end(), tani1),
                               it2 = find(tani.begin(), tani.end(), tani2);
      bool f1 = (it1 != tani.end());
      bool f2 = (it2 != tani.end());
      int ind1 = ind[distance(tani.begin(), it1)];
      int ind2 = ind[distance(tani.begin(), it2)];
      if (f1) {
        if (f2) {
          if (ind2 - ind1 != index) {
            flag = false;
          }
        } else {
          ind.push_back(ind1 + index);
          tani.push_back(tani2);
        }
      } else {
        if (f2) {
          ind.push_back(ind2 - index);
          tani.push_back(tani1);
        } else {
          date[count] = index;
          date2[count][0] = tani1;
          date2[count][1] = tani2;
          count++;
        }
      }
    }
    bool flag5 = false;
    do {
      flag5 = false;
      REP(i, count) {
        vector<string>::iterator it1 = find(tani.begin(), tani.end(),
                                            date2[i][0]),
                                 it2 = find(tani.begin(), tani.end(),
                                            date2[i][1]);
        bool f1 = (it1 != tani.end());
        bool f2 = (it2 != tani.end());
        int ind1 = ind[distance(tani.begin(), it1)];
        int ind2 = ind[distance(tani.begin(), it2)];
        if (f1 && f2) {
          if (ind2 - ind1 != date[i])
            flag = false;
        } else if (f1 && !f2) {
          ind.push_back(ind1 + date[i]);
          tani.push_back(date2[i][1]);
        } else if (!f1 && f2) {
          ind.push_back(ind2 - date[i]);
          tani.push_back(date2[i][0]);
        } else {
          flag5 = true;
        }
      }
    } while (flag5);
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}