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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const int INF = 200 * 100 + 100;

struct Dict {
  map<string, int> dict;
  int id;

  Dict() : id(0) {}

  int get(const string &str) {
    if (dict.count(str))
      return dict[str];
    return dict[str] = id++;
  }

  int size() const { return id; }
};

bool dfs(int pos, int val, const vector<vector<pair<int, int>>> &graph,
         vector<int> &dist) {
  if (dist[pos] != INF)
    return dist[pos] == val;
  dist[pos] = val;
  const vector<pair<int, int>> &edges = graph[pos];
  for (vector<pair<int, int>>::const_iterator it = edges.begin();
       it != edges.end(); ++it) {
    int nc = val + it->second;
    if (!dfs(it->first, nc, graph, dist))
      return false;
  }
  return true;
}

vector<vector<pair<int, int>>> v(200);
vector<int> dist(200);
vector<unsigned char> visited(200);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  while (true) {
    int N;
    cin >> N;
    if (!N)
      break;
    cin.ignore();
    for (int i = 0; i < 2 * N; ++i) {
      v[i].clear();
    }
    Dict dict;
    for (int i = 0; i < N; ++i) {
      string line;
      getline(cin, line);
      for (string::iterator it = line.begin(); it != line.end(); ++it) {
        if (*it == '=' || *it == '^')
          *it = ' ';
      }
      istringstream is(line);
      string unit_left, unit_right;
      int ord;
      int tmp;
      is >> tmp >> unit_left >> tmp >> ord >> unit_right;
      // sscanf(line.c_str(), "%*d %s = %*d^%d %s ", unit_left, &ord,
      // unit_right);
      const int id_left = dict.get(string(unit_left));
      const int id_right = dict.get(string(unit_right));
      v[id_left].push_back(make_pair(id_right, ord));
      v[id_right].push_back(make_pair(id_left, -ord));
    }

    const int M = dict.size();
    bool ok = true;
    fill(dist.begin(), dist.end(), INF);
    for (int i = 0; i < M; ++i) {
      if (dist[i] == INF) {
        if (!dfs(i, 0, v, dist)) {
          ok = false;
          break;
        }
      }
    }

    if (!ok) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const int INF = 100 * 100 + 100;

struct Dict {
  map<string, int> dict;
  int id;

  Dict() : id(0) {}

  int get(const string &str) {
    if (dict.count(str))
      return dict[str];
    return dict[str] = id++;
  }
};

bool relax(const vector<vector<pair<int, int>>> &graph, vector<int> &dist,
           int N) {
  bool updated = false;
  for (int i = 0; i < N; ++i) {
    const vector<pair<int, int>> &edges = graph[i];
    for (vector<pair<int, int>>::const_iterator it = edges.begin();
         it != edges.end(); ++it) {
      const int nc = dist[i] + it->second;
      if (dist[it->first] > nc) {
        dist[it->first] = nc;
        updated = true;
      }
    }
  }
  return updated;
}

vector<vector<pair<int, int>>> v(100);
vector<int> dist(100);
vector<unsigned char> visited(100);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  while (true) {
    int N;
    cin >> N;
    if (!N)
      break;
    cin.ignore();
    for (int i = 0; i < N; ++i) {
      v[i].clear();
      dist[i] = INF;
    }
    Dict dict;
    for (int i = 0; i < N; ++i) {
      string line;
      getline(cin, line);
      for (string::iterator it = line.begin(); it != line.end(); ++it) {
        if (*it == '=' || *it == '^')
          *it = ' ';
      }
      istringstream is(line);
      string unit_left, unit_right;
      int ord;
      int tmp;
      is >> tmp >> unit_left >> tmp >> ord >> unit_right;
      // sscanf(line.c_str(), "%*d %s = %*d^%d %s ", unit_left, &ord,
      // unit_right);
      const int id_left = dict.get(string(unit_left));
      const int id_right = dict.get(string(unit_right));
      v[id_left].push_back(make_pair(id_right, ord));
      v[id_right].push_back(make_pair(id_left, -ord));
    }

    // テ」ツ?敕」ツつ古」ツ?榲」ツつ古」ツ?ョテゥツ?」テァツオツ静ヲツ按静・ツ按?」ツ?ォテ」ツ?、テ」ツ?催ッツシツ?テ」ツδ偲」ツδシテ」ツδ嘉」ツつ津・ツ債佚、ツスツ催・ツ?ε」ツ?ィテ」ツ?凖」ツつ凝ッツシツ?
    fill(visited.begin(), visited.end(), 0);
    queue<int> q;
    for (int i = 0; i < N; ++i) {
      if (!visited[i]) {
        dist[i] = 0;
        visited[i] = 1;
        q.push(i);
        while (!q.empty()) {
          int cur = q.front();
          q.pop();
          const vector<pair<int, int>> &edges = v[cur];
          for (vector<pair<int, int>>::const_iterator it = edges.begin();
               it != edges.end(); ++it) {
            if (!visited[it->first]) {
              visited[it->first] = 1;
              q.push(it->first);
            }
          }
        }
      }
    }

    // Bellman-ford
    for (int phase = 0; phase < N; ++phase) {
      if (!relax(v, dist, N))
        break;
    }
    if (relax(v, dist, N)) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}