alexjercan/bugnet huggingface dataset:
Problem p01322 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, d[100];
  string s[100];
  while (cin >> n >> m && n) {
    int a = 0;
    for (int i = 0; i < n; i++)
      cin >> s[i] >> d[i];
    for (int i = 0; i < m; i++) {
      string t;
      cin >> t;
      for (int k = 0; k < n; k++) {
        bool f = 1;
        for (int j = 0; j < t.size(); j++)
          if (s[k][j] != '*' && s[k][j] != t[j])
            f = 0;
        if (f)
          a += d[k];
      }
    }
    cout << a << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, m, d[100];
  string s[100];
  while (cin >> n >> m && n) {
    int a = 0;
    for (int i = 0; i < n; i++)
      cin >> s[i] >> d[i];
    for (int i = 0; i < n; i++) {
      string t;
      cin >> t;
      for (int k = 0; k < m; k++) {
        bool f = 1;
        for (int j = 0; j < t.size(); j++)
          if (s[k][j] != '*' && s[k][j] != t[j])
            f = 0;
        if (f)
          a += d[k];
      }
    }
    cout << a << endl;
  }
  return 0;
}
Problem p01322 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

struct R {
  string y;
  int C;
};

int main() {

  int N, M;

  while (1) {

    struct R n[101];
    cin >> N;
    if (N == 0)
      break;
    cin >> M;
    for (int i = 0; i < N; i++) {
      cin >> n[i].y;
      cin >> n[i].C;
    }
    string str;
    int s = 0;
    for (int iii = 0; iii < M; iii++) {
      cin >> str;
      for (int i = 0; i < N; i++) {
        bool f = true;
        for (int j = 0; j < 8; j++)
          if ((n[i].y[j] != str[j]) && n[i].y[j] != '*') {
            f = false;
          } // cout<<n[i].y[j]<<"#"<<str[j]<<endl;}
        if (f == true)
          s += n[i].C;
      }
    }

    cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

struct R {
  string y;
  int C;
};

int main() {

  int N, M;

  while (1) {

    struct R n[101];
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
      cin >> n[i].y;
      cin >> n[i].C;
    }
    string str;
    int s = 0;
    for (int iii = 0; iii < M; iii++) {
      cin >> str;
      for (int i = 0; i < N; i++) {
        bool f = true;
        for (int j = 0; j < 8; j++)
          if ((n[i].y[j] != str[j]) && n[i].y[j] != '*') {
            f = false;
          } // cout<<n[i].y[j]<<"#"<<str[j]<<endl;}
        if (f == true)
          s += n[i].C;
      }
    }

    cout << s << endl;
  }
  return 0;
}
Problem p01322 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
bool sc(string num, string in) {
  bool han = true;
  rep(i, 8) {
    if (num[i] == '*')
      continue;
    if (num[i] != in[i])
      han = false;
  }
  return han;
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<string> num(n);
    vector<int> money(n);
    rep(i, n) { cin >> num[i] >> money[i]; }
    int sum = 0;
    rep(i, m) {
      string in;
      cin >> in;
      rep(j, n) if (sc(num[j], in)) sum += money[j];
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
bool sc(string num, string in) {
  bool han = true;
  rep(i, 8) {
    if (num[i] == '*')
      continue;
    if (num[i] != in[i])
      han = false;
  }
  return han;
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<string> num(n);
    vector<int> money(m);
    rep(i, n) { cin >> num[i] >> money[i]; }
    int sum = 0;
    rep(i, m) {
      string in;
      cin >> in;
      rep(j, n) if (sc(num[j], in)) sum += money[j];
    }
    cout << sum << endl;
  }
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

struct NODE {
  string to;
  int cost;
  NODE(string to, int cost) : to(to), cost(cost) {}
};

int main() {
  int n;
  while (cin >> n && n) {
    map<string, vector<NODE>> g;

    for (int i = 0; i < n; i++) {
      string a, b, c, d, e;
      cin >> a >> b >> c >> d >> e;
      g[b].push_back(NODE(e, atoi(d.substr(d.find("^") + 1).c_str())));
      g[e].push_back(NODE(b, -atoi(d.substr(d.find("^") + 1).c_str())));
    }
    int ok = 1;
    for (map<string, vector<NODE>>::iterator it = g.begin(); it != g.end();
         ++it) {
      map<string, int> done;
      queue<NODE> Q;
      Q.push(NODE(it->first, 0));
      while (Q.size()) {
        NODE q = Q.front();
        Q.pop();
        if (done.count(q.to)) {
          if (done[q.to] != q.cost)
            ok = 0;
          continue;
        }
        done[q.to] = q.cost;
        for (int i = 0; i < g[q.to].size(); i++)
          Q.push(NODE(g[q.to][i].to, q.cost + g[q.to][i].cost));
      }
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<string, int> done;
map<string, vector<pair<string, int>>> graph;
bool flag;

void dfs(string p, int cost) {
  if (done.find(p) != done.end()) {
    if (cost != done[p])
      flag = false;
    return;
  } else {
    done[p] = cost;
  }
  vector<pair<string, int>> r = graph[p];
  for (int i = 0; i < r.size(); i++) {
    dfs(r[i].first, cost + r[i].second);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    graph.clear();
    vector<string> vertex;
    for (int i = 0; i < n; i++) {
      string a, b, c, d;
      cin >> d >> a >> d >> c >> b;
      c = c.substr(c.find("^") + 1);
      int cost = atoi(c.c_str());
      graph[b].push_back(make_pair(a, cost));
      // graph[a].push_back(make_pair(b,-cost));
      vertex.push_back(a);
      vertex.push_back(b);
    }
    sort(vertex.begin(), vertex.end());
    vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
    bool ans = true;
    for (int i = 0; i < n; i++) {
      done.clear();
      flag = true;
      dfs(vertex[i], 0);
      ans &= flag;
    }
    cout << (ans ? "Yes" : "No") << endl;
  }
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  while (true) {
    int n, m = 0;
    int a, b, e;
    string s1, s2;
    char c, d;
    int i, j, k;
    cin >> n;
    if (!n)
      break;
    int graph[200][200];
    for (i = 0; i < 200; i++)
      for (j = 0; j < 200; j++)
        graph[i][j] = 10000000;
    vector<string> names(200);
    for (i = 0; i < n; i++) {
      cin >> a >> s1 >> c >> b >> d >> e >> s2;
      j = 0, k = 0;
      for (; j < m; j++)
        if (names[j] == s1)
          break;
      if (j == m) {
        names[j] = s1;
        m++;
      }
      for (; k < m; k++)
        if (names[k] == s2)
          break;
      if (k == m) {
        names[k] = s2;
        m++;
      }
      graph[k][j] = e;
      graph[j][k] = -e;
    }
    for (k = 0; k < m; k++) {
      for (i = 0; i < m; i++) {
        for (j = 0; j < m; j++) {
          if (graph[i][k] + graph[k][j] < graph[i][j])
            graph[i][j] = graph[i][k] + graph[k][j];
        }
      }
    }
    bool bb = true;
    for (i = 0; i < m; i++)
      if (graph[i][i] < 0)
        bb = false;
    if (bb)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define mp make_pair
using namespace std;
int main() {
  int n, m = 0;
  int a, b, e;
  string s1, s2;
  char c, d;
  while (true) {
    cin >> n;
    if (!n)
      break;
    int graph[200][200];
    for (int i = 0; i < 200; i++)
      for (int j = 0; j < 200; j++)
        graph[i][j] = 100000;
    vector<string> names(200);
    for (int i = 0; i < n; i++) {
      cin >> a >> s1 >> c >> b >> d >> e >> s2;
      int j = 0, k = 0;
      for (; j < m; j++)
        if (names[j] == s1)
          break;
      names[j] = s1;
      if (j == m)
        m++;
      for (; k < m; k++)
        if (names[k] == s2)
          break;
      names[k] = s2;
      if (k == m)
        m++;
      graph[k][j] = e;
      graph[j][k] = -e;
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          if (graph[i][k] + graph[k][j] < graph[i][j])
            graph[i][j] = graph[i][k] + graph[k][j];
        }
      }
    }
    bool bb = true;
    for (int i = 0; i < m; i++)
      if (graph[i][i] < 0)
        bb = false;
    if (bb)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
map<string, vector<pair<string, int>>> b;
map<string, int> c;
int fn(string s, int p) {
  int i;
  if (c.count(s)) {
    if (c[s] != p)
      return 1;
    else
      return 0;
  } else {
    c[s] = p;
    for (i = 0; i < (int)b[s].size(); i++) {
      if (fn(b[s][i].first, p + b[s][i].second))
        return 1;
    }
    return 0;
  }
}
int main() {
  int i;
  int n;
  while (cin >> n, n) {
    set<string> a;
    b.clear();
    for (i = 0; i < n; i++) {
      string s, t;
      char c;
      int p;
      cin >> s >> s >> c >> c >> c >> c >> p >> t;
      a.insert(s);
      a.insert(t);
      b[s].push_back(make_pair(t, -p));
      b[t].push_back(make_pair(s, p));
    }
    set<string>::iterator it;
    for (it = a.begin(); it != a.end(); it++) {
      c.clear();
      if (fn(*it, 0))
        break;
    }
    if (it != a.end())
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
map<string, vector<pair<string, int>>> b;
map<string, int> c;
int fn(string s, int p) {
  int i;
  if (c.count(s)) {
    if (c[s] != p)
      return 1;
    else
      return 0;
  } else {
    c[s] = p;
    for (i = 0; i < (int)b[s].size(); i++) {
      if (fn(b[s][i].first, p + b[s][i].second))
        return 1;
    }
    c.erase(s);
    return 0;
  }
}
int main() {
  int i;
  int n;
  while (cin >> n, n) {
    set<string> a;
    b.clear();
    for (i = 0; i < n; i++) {
      string s, t;
      char c;
      int p;
      cin >> s >> s >> c >> c >> c >> c >> p >> t;
      a.insert(s);
      a.insert(t);
      b[s].push_back(make_pair(t, -p));
      b[t].push_back(make_pair(s, p));
    }
    set<string>::iterator it;
    for (it = a.begin(); it != a.end(); it++) {
      c.clear();
      if (fn(*it, 0))
        break;
    }
    if (it != a.end())
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <map>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair

int n, vis[256], rk[256];
map<string, int> dict;
vector<pair<int, int>> g[256];

int of(const string &s) {
  if (dict.count(s) == 0)
    dict[s] = n++;
  return dict[s];
}

bool visit(int at, int r) {
  if (vis[at])
    return rk[at] == r;
  vis[at] = 1;
  rk[at] = r;
  rep(i, g[at].size()) {
    if (!visit(g[at][i].first, r + g[at][i].second))
      return false;
  }
  return true;
}

int main() {
  for (;;) {
    int m;
    scanf("%d", &m);
    if (m == 0)
      return 0;
    n = 0;
    dict.clear();
    rep(i, 256) g[i].clear();
    rep(_, m) {
      static char a[32], b[32];
      int x;
      scanf(" 1 %s = 10^%d %s", a, &x, b);
      const int u = of(a);
      const int v = of(b);
      g[u].push_back(mp(v, -x));
      g[v].push_back(mp(u, x));
    }
    rep(i, n) vis[i] = 0;
    bool ans = true;
    rep(i, n) if (!vis[i]) {
      if (!visit(i, 0))
        ans = false;
    }
    puts(ans ? "Yes" : "No");
  }
}

Error: 0

Fixed submission:

#include <map>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair

int n, vis[128], rk[128];
map<string, int> dict;
vector<pair<int, int>> g[128];

int of(const string &s) {
  if (dict.count(s) == 0)
    dict[s] = n++;
  return dict[s];
}

bool visit(int at, int r) {
  if (vis[at])
    return rk[at] == r;
  vis[at] = 1;
  rk[at] = r;
  rep(i, g[at].size()) {
    if (!visit(g[at][i].first, r + g[at][i].second))
      return false;
  }
  return true;
}

int main() {
  for (;;) {
    int m;
    scanf("%d", &m);
    if (m == 0)
      return 0;
    n = 0;
    dict.clear();
    rep(i, 128) g[i].clear();
    rep(_, m) {
      static char a[32], b[32];
      int x;
      scanf(" 1 %s = 10^%d %s", a, &x, b);
      const int u = of(a);
      const int v = of(b);
      g[u].push_back(mp(v, -x));
      g[v].push_back(mp(u, x));
    }
    rep(i, n) vis[i] = 0;
    bool ans = true;
    rep(i, n) if (!vis[i]) {
      if (!visit(i, 0))
        ans = false;
    }
    puts(ans ? "Yes" : "No");
  }
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

bool visited[200];
int c[200];

bool dfs(vector<vector<pair<int, int>>> &edge, int now, int cost) {
  if (visited[now])
    return cost == c[now];
  c[now] = cost;
  visited[now] = true;
  bool res = true;
  rep(i, edge[now].size()) {
    int next = edge[now][i].first;
    int ncost = edge[now][i].second;
    res &= dfs(edge, next, cost + ncost);
  }
  return res;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  while (1) {
    cin >> n;
    map<string, int> v;
    vector<vector<pair<int, int>>> edge(2 * n);
    if (n == 0)
      break;
    int cnt = 0;
    rep(i, n) {
      int idummy;
      char cdummy;
      string s[2], sdummy;
      int cost;
      cin >> idummy >> s[0] >> sdummy >> idummy >> cdummy >> cost >> s[1];
      rep(j, 2) {
        if (v.find(s[j]) == v.end()) {
          v[s[j]] = cnt;
          cnt++;
        }
      }
      edge[v[s[0]]].push_back(make_pair(v[s[1]], cost));
      edge[v[s[1]]].push_back(make_pair(v[s[0]], -cost));
    }
    bool ok = true;
    rep(i, cnt) {
      rep(j, cnt) { visited[j] = false; }
      ok &= dfs(edge, i, 0);
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

bool visited[100];
int c[100];

bool dfs(vector<vector<pair<int, int>>> &edge, int now, int cost) {
  if (visited[now])
    return cost == c[now];
  c[now] = cost;
  visited[now] = true;
  bool res = true;
  rep(i, edge[now].size()) {
    int next = edge[now][i].first;
    int ncost = edge[now][i].second;
    res &= dfs(edge, next, cost + ncost);
  }
  return res;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  while (1) {
    cin >> n;
    map<string, int> v;
    vector<vector<pair<int, int>>> edge(n);
    if (n == 0)
      break;
    int cnt = 0;
    rep(i, n) {
      int idummy;
      char cdummy;
      string s[2], sdummy;
      int cost;
      cin >> idummy >> s[0] >> sdummy >> idummy >> cdummy >> cost >> s[1];
      rep(j, 2) {
        if (v.find(s[j]) == v.end()) {
          v[s[j]] = cnt;
          cnt++;
        }
      }
      edge[v[s[0]]].push_back(make_pair(v[s[1]], cost));
      edge[v[s[1]]].push_back(make_pair(v[s[0]], -cost));
    }
    bool ok = true;
    rep(i, n) {
      rep(j, 100) { visited[j] = false; }
      ok &= dfs(edge, i, 0);
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

using ll = long long;
constexpr ll MAX = 514;
constexpr ll UNVISITED = (1 << 25);

struct Edge {
  ll to, cost;
  Edge() {}
  Edge(ll to, ll cost) : to{to}, cost{cost} {}
};

vector<Edge> G[MAX];
ll dist[MAX];

bool dfs(ll v, ll p, ll d) {
  if (dist[v] != UNVISITED) {
    return (dist[v] != d);
  }

  dist[v] = d;
  for (const auto &g : G[v]) {
    if (g.to == p)
      continue;
    if (dfs(g.to, v, d + g.cost)) {
      return true;
    }
  }
  return false;
}

bool run() {
  ll N;
  cin >> N;
  if (N == 0) {
    return false;
  }

  for (ll i = 0; i < MAX; i++) {
    G[i].clear();
  }

  ll n = 0;
  map<string, ll> s2i;

  string t, u, v;
  for (ll i = 0; i < N; i++) {
    cin >> u >> u;

    if (s2i.count(u) == 0) {
      s2i[u] = n++;
    }

    cin >> t >> t >> v;
    t = t.substr(3);

    if (s2i.count(v) == 0) {
      s2i[v] = n++;
    }

    ll num = stoi(t);
    G[s2i[v]].emplace_back(s2i[u], -num);
    G[s2i[u]].emplace_back(s2i[v], num);
  }

  bool ok = 1;
  for (ll i = 0; i < n; i++) {
    fill(dist, dist + MAX, UNVISITED);
    if (dfs(i, -1, 0)) {
      ok = 0;
      break;
    }
  }
  cout << (ok ? "Yes" : "No") << endl;
  return true;
}

int main() {
  while (run())
    ;
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

constexpr int MAX = 100;

struct Edge {
  int to, cost;
  Edge() {}
  Edge(int to, int cost) : to{to}, cost{cost} {}
};

vector<Edge> G[MAX];
int dist[MAX];

bool dfs(int v, int p, int d) {
  if (dist[v] != -1) {
    return (dist[v] != d);
  }
  dist[v] = d;

  for (auto &g : G[v]) {
    if (g.to == p)
      continue;
    if (dfs(g.to, v, d + g.cost)) {
      return true;
    }
  }
  return false;
}

bool run() {
  int N;
  cin >> N;
  if (N == 0) {
    return false;
  }

  for (int i = 0; i < N; i++) {
    G[i].clear();
  }

  map<string, int> s2i;

  string t, u, v;
  for (int i = 0; i < N; i++) {
    cin >> u >> u;

    if (s2i.count(u) == 0) {
      s2i[u] = s2i.size();
    }

    cin >> t >> t >> v;
    t = t.substr(3);

    if (s2i.count(v) == 0) {
      s2i[v] = s2i.size();
    }

    int num = stoi(t);
    G[s2i[v]].emplace_back(s2i[u], -num);
    G[s2i[u]].emplace_back(s2i[v], num);
  }
  bool ok = 1;
  for (int i = 0; i < (int)s2i.size(); i++) {
    memset(dist, -1, sizeof(dist));
    if (dfs(i, -1, 0)) {
      ok = 0;
      break;
    }
  }
  cout << (ok ? "Yes" : "No") << endl;
  return true;
}

int main() {
  while (run())
    ;
  return 0;
}
Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX_M = 201;
const int INF = 1 << 24;
map<string, int> id;
int G[MAX_M][MAX_M];
int M;

int getId(string s) {
  if (id.find(s) == id.end()) {
    id[s] = M++;
  }
  return id[s];
}

bool solve() {
  for (int k = 0; k < M; ++k) {
    for (int s = 0; s < M; ++s) {
      for (int d = 0; d < M; ++d) {
        if (G[s][k] == INF || G[k][d] == INF)
          continue;
        if (G[s][d] == INF) {
          G[s][d] = G[s][k] + G[k][d];
        } else if (G[s][d] != G[s][k] + G[k][d]) {
          return false;
        }
      }
    }
  }
  return true;
}

int main() {
  int N;
  while (cin >> N && N) {
    M = 0;
    id.clear();
    fill(G[0], G[MAX_M], INF);
    for (int i = 0; i < N; ++i) {
      string in[5];
      for (int j = 0; j < 5; ++j)
        cin >> in[j];
      int a = getId(in[1]);
      int b = getId(in[4]);
      int cost = atoi((in[3].substr(3)).c_str());
      G[a][b] = cost;
      G[b][a] = -cost;
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX_M = 101;
const int INF = 1 << 24;
map<string, int> id;
int G[MAX_M][MAX_M];
int M;

int getId(string s) {
  if (id.find(s) == id.end()) {
    id[s] = M++;
  }
  return id[s];
}

bool solve() {
  for (int k = 0; k < M; ++k) {
    for (int s = 0; s < M; ++s) {
      for (int d = 0; d < M; ++d) {
        if (G[s][k] == INF || G[k][d] == INF)
          continue;
        if (G[s][d] == INF) {
          G[s][d] = G[s][k] + G[k][d];
        } else if (G[s][d] != G[s][k] + G[k][d]) {
          return false;
        }
      }
    }
  }
  return true;
}

int main() {
  int N;
  while (cin >> N && N) {
    M = 0;
    id.clear();
    fill(G[0], G[MAX_M], INF);
    for (int i = 0; i < N; ++i) {
      string in[5];
      for (int j = 0; j < 5; ++j)
        cin >> in[j];
      int a = getId(in[1]);
      int b = getId(in[4]);
      int cost = atoi((in[3].substr(3)).c_str());
      G[a][b] = cost;
      G[b][a] = -cost;
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}