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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

class Data {
public:
  string lhs;
  string rhs;
  int power;
  Data(string _l, string _r, int _p) {
    lhs = _l;
    rhs = _r;
    power = _p;
  }
};

map<string, int> gUnit;
int gCount = 0;

int dp[101][101];

int unit2num(string str) {
  if (gUnit.find(str) == gUnit.end()) {
    gUnit[str] = gCount++;
  }

  return gUnit[str];
}

int main() {
  int n;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    gCount = 0;
    gUnit.clear();
    memset(dp, 0x3f, sizeof(dp));

    vector<Data> relations;
    for (int i = 0; i < n; i++) {
      int num;
      string lhs;
      string eq;
      string rhs;
      string tmp;

      cin >> num >> lhs >> eq >> tmp >> rhs;
      int base, pow;
      sscanf(tmp.c_str(), "%d^%d", &base, &pow);
      relations.push_back(Data(lhs, rhs, pow));
    }

    for (int i = 0; i < relations.size(); i++) {
      dp[unit2num(relations[i].lhs)][unit2num(relations[i].rhs)] =
          -relations[i].power;
      dp[unit2num(relations[i].rhs)][unit2num(relations[i].lhs)] =
          relations[i].power;
    }

    for (int k = 0; k < gCount; k++) {
      for (int i = 0; i < gCount; i++) {
        for (int j = 0; j < gCount; j++) {
          dp[i][j] = min(dp[i][k] + dp[k][j], dp[i][j]);
        }
      }
    }

    bool isok = true;
    for (int i = 0; i < gCount; i++) {
      if (dp[i][i] != 0)
        isok = false;
    }
    //
    //
    //		map<string,set<int> > vals;
    //
    //		bool isok = true;
    //		vals[relations[0].rhs].insert(0);
    //
    //		for(int i=0;i<n;i++){
    //			for(int j=0;j<n;j++){
    //				for(set<int>::iterator it = vals[relations[j].rhs].begin();it !=
    //vals[relations[j].rhs].end();it++){
    //					//1 kilobyte = 10^3 byte
    //					vals[relations[j].lhs].insert(relations[j].power +
    //*it); 					if(vals[relations[j].lhs].size() > 1){ 						isok = false; 						goto found;
    //					}
    //				}
    //				//sort(vals[relations[j].lhs].begin(),vals[relations[j].lhs].end());
    //				//vector<int>::iterator it =
    //unique(vals[relations[j].lhs].begin(),vals[relations[j].lhs].end());
    //				//vals[relations[j].lhs].resize( it -
    //vals[relations[j].lhs].begin() );
    //
    //				//if(vals[relations[j].lhs].size() > 1) isok =
    //false;
    //			}
    //
    //
    //			for(int j=0;j<n;j++){
    //				for(set<int>::iterator it = vals[relations[j].lhs].begin();it !=
    //vals[relations[j].lhs].end();it++){
    //					//1 kilobyte = 10^3 byte
    //					vals[relations[j].rhs].insert(-relations[j].power +
    //*it); 					if(vals[relations[j].rhs].size() > 1){ 						isok = false; 						goto found;
    //					}
    //				}
    //				//sort(vals[relations[j].rhs].begin(),vals[relations[j].rhs].end());
    //				//vector<int>::iterator it =
    //unique(vals[relations[j].rhs].begin(),vals[relations[j].rhs].end());
    //				//vals[relations[j].rhs].resize( it -
    //vals[relations[j].rhs].begin() );
    //				//if(vals[relations[j].rhs].size() > 1) isok =
    //false;
    //			}
    //		}
    //
    //
    //		for(int j=0;j<n;j++){
    //			if(vals.find(relations[j].lhs) == vals.end()
    //				|| vals.find(relations[j].rhs) == vals.end()){
    //					isok = false;
    //			}
    //		}
    //
    // found:;

    printf("%s\\n", isok ? "Yes" : "No");
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

class Data {
public:
  string lhs;
  string rhs;
  int power;
  Data(string _l, string _r, int _p) {
    lhs = _l;
    rhs = _r;
    power = _p;
  }
};

int main() {
  int n;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    vector<Data> relations;
    for (int i = 0; i < n; i++) {
      int num;
      string lhs;
      string eq;
      string rhs;
      string tmp;

      cin >> num >> lhs >> eq >> tmp >> rhs;
      int base, pow;
      sscanf(tmp.c_str(), "%d^%d", &base, &pow);
      relations.push_back(Data(lhs, rhs, pow));
    }

    map<string, set<int>> vals;

    bool isok = true;
    vals[relations[0].rhs].insert(0);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (set<int>::iterator it = vals[relations[j].rhs].begin();
             it != vals[relations[j].rhs].end(); it++) {
          // 1 kilobyte = 10^3 byte
          vals[relations[j].lhs].insert(relations[j].power + *it);
        }
        // sort(vals[relations[j].lhs].begin(),vals[relations[j].lhs].end());
        // vector<int>::iterator it =
        // unique(vals[relations[j].lhs].begin(),vals[relations[j].lhs].end());
        // vals[relations[j].lhs].resize( it - vals[relations[j].lhs].begin() );

        // if(vals[relations[j].lhs].size() > 1) isok = false;
      }

      for (int j = 0; j < n; j++) {
        for (set<int>::iterator it = vals[relations[j].lhs].begin();
             it != vals[relations[j].lhs].end(); it++) {
          // 1 kilobyte = 10^3 byte
          vals[relations[j].rhs].insert(-relations[j].power + *it);
        }
        // sort(vals[relations[j].rhs].begin(),vals[relations[j].rhs].end());
        // vector<int>::iterator it =
        // unique(vals[relations[j].rhs].begin(),vals[relations[j].rhs].end());
        // vals[relations[j].rhs].resize( it - vals[relations[j].rhs].begin() );
        // if(vals[relations[j].rhs].size() > 1) isok = false;
      }
    }

    for (map<string, set<int>>::iterator it = vals.begin(); it != vals.end();
         it++) {
      if (it->second.size() > 1)
        isok = false;
    }
    printf("%s\\n", isok ? "Yes" : "No");
  }
}