Problem p03995 (C++)

Buggy submission (status: Runtime Error):
/*+lmake
 * DEFINE += WAAUTOMATON
 */
#include <bits/stdc++.h>
#ifdef WAAUTOMATON
#define debug(args...)                                                         \
  {                                                                            \
    dbg, args;                                                                 \
    cerr << endl;                                                              \
  }
#define massert(x) assert(x)
#else
#define debug(args...) // Just strip off all debug tokens
#define massert(x)
#endif
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
struct debugger {
  template <typename T> debugger &operator,(const T &v) {
    std::cerr << v << " ";
    return *this;
  }
} dbg;

void iopen() {
  static bool isOpen = false;
  if (!isOpen) {
    isOpen = true;
#ifdef WAAUTOMATON
    freopen("in.txt", "r", stdin);
#endif
  }
}
template <size_t _I_Buffer_Size = 1 << 23, size_t _O_Buffer_Size = 1 << 23>
struct IO_Tp {
  char _I_Buffer[_I_Buffer_Size];
  char *_I_pos;
  const char *_I_end;

  char _O_Buffer[_O_Buffer_Size];
  char *_O_pos;
  const char *_O_end;

  IO_Tp()
      : _I_pos(_I_Buffer), _O_pos(_O_Buffer),
        _I_end(_I_Buffer + _I_Buffer_Size), _O_end(_O_Buffer + _O_Buffer_Size) {
  }

  ~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }

  inline char getchar() {
    char res = *_I_pos;
    nextchar();
    return res;
  }

  inline bool is_digit(const char ch) { return '0' <= ch && ch <= '9'; }

  inline void nextchar() {
    ++_I_pos;
    if (_I_pos == _I_end) {
      fread(_I_Buffer, 1, _I_Buffer_Size, stdin);
      _I_pos = _I_Buffer;
    }
  }

  template <typename Int> inline IO_Tp &operator>>(Int &res) {
    res = 0;
    int k = 1;
    while (!is_digit(*_I_pos)) {
      if (*_I_pos == '-')
        k = -1;
      nextchar();
    }
    do {
      (res *= 10) += (*_I_pos) - '0';
      nextchar();
    } while (is_digit(*_I_pos));
    res *= k;
    return *this;
  }

  inline IO_Tp &operator>>(char &res) {
    res = *_I_pos;
    nextchar();
    return *this;
  }

  inline void putchar(char x) {
    if (_O_pos == _O_end) {
      fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout);
      _O_pos = _O_Buffer;
    }
    *_O_pos++ = x;
  }
  template <typename Int> inline IO_Tp &operator<<(Int n) {
    if (n < 0) {
      putchar('-');
      n = -n;
    }
    static char _buf[20];
    char *_pos(_buf);
    do
      *_pos++ = '0' + n % 10;
    while (n /= 10);
    while (_pos != _buf)
      putchar(*--_pos);
    return *this;
  }

  inline IO_Tp &operator<<(char ch) {
    putchar(ch);
    return *this;
  }

  inline IO_Tp &operator<<(const char *s) {
    while (*s != 0) {
      putchar(*s);
      ++s;
    }
    return *this;
  }
};
IO_Tp<> IO;
const int MAXN = 200000;
vector<pii> g[MAXN + 10];
bool vis[MAXN + 10];
LL L, R;
bool flag;
LL vx[MAXN + 10];
int r, c, n;
void dfs(int now, LL v) {
  if (!flag)
    return;
  if (!vis[now]) {
    if (vx[now] != v) {
      flag = false;
      return;
    }
    return;
  }
  vis[now] = false;
  vx[now] = v;
  if (now <= r)
    L = max(L, v);
  else
    R = min(R, v);
  for (auto i : g[now]) {
    dfs(i.first, v + i.second);
    if (!flag)
      return;
  }
}
int main() {
  iopen();
  int T;
  IO >> T;
  while (T--) {
    IO >> r >> c >> n;
    for (int i = 1; i <= r + c; ++i) {
      g[i].clear();
    }
    for (int i = 1; i <= n; ++i) {
      int x, y, v;
      IO >> x >> y >> v;
      g[x].push_back(pii(y + r, v));
      g[y + r].push_back(pii(x, -v));
    }
    memset(vis, true, sizeof(vis));
    memset(vx, 0, sizeof(vx));
    flag = true;
    for (int i = 1; i <= r + c; ++i) {
      if (vis[i]) {
        L = LLONG_MIN;
        R = LLONG_MAX;
        dfs(i, 0);
        if (L > R) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      IO << "Yes\n";
    } else {
      IO << "No\n";
    }
  }
  return 0;
}


Fixed submission:
/*+lmake
 * DEFINE += WAAUTOMATON
 */
#include <bits/stdc++.h>
#ifdef WAAUTOMATON
#define debug(args...)                                                         \
  {                                                                            \
    dbg, args;                                                                 \
    cerr << endl;                                                              \
  }
#define massert(x) assert(x)
#else
#define debug(args...) // Just strip off all debug tokens
#define massert(x)
#endif
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
struct debugger {
  template <typename T> debugger &operator,(const T &v) {
    std::cerr << v << " ";
    return *this;
  }
} dbg;

void iopen() {
  static bool isOpen = false;
  if (!isOpen) {
    isOpen = true;
#ifdef WAAUTOMATON
    freopen("in.txt", "r", stdin);
#endif
  }
}
template <size_t _I_Buffer_Size = 1 << 23, size_t _O_Buffer_Size = 1 << 23>
struct IO_Tp {
  char _I_Buffer[_I_Buffer_Size];
  char *_I_pos;
  const char *_I_end;

  char _O_Buffer[_O_Buffer_Size];
  char *_O_pos;
  const char *_O_end;

  IO_Tp()
      : _I_pos(_I_Buffer), _O_pos(_O_Buffer),
        _I_end(_I_Buffer + _I_Buffer_Size), _O_end(_O_Buffer + _O_Buffer_Size) {
  }

  ~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }

  inline char getchar() {
    char res = *_I_pos;
    nextchar();
    return res;
  }

  inline bool is_digit(const char ch) { return '0' <= ch && ch <= '9'; }

  inline void nextchar() {
    ++_I_pos;
    if (_I_pos == _I_end) {
      fread(_I_Buffer, 1, _I_Buffer_Size, stdin);
      _I_pos = _I_Buffer;
    }
  }

  template <typename Int> inline IO_Tp &operator>>(Int &res) {
    res = 0;
    int k = 1;
    while (!is_digit(*_I_pos)) {
      if (*_I_pos == '-')
        k = -1;
      nextchar();
    }
    do {
      (res *= 10) += (*_I_pos) - '0';
      nextchar();
    } while (is_digit(*_I_pos));
    res *= k;
    return *this;
  }

  inline IO_Tp &operator>>(char &res) {
    res = *_I_pos;
    nextchar();
    return *this;
  }

  inline void putchar(char x) {
    if (_O_pos == _O_end) {
      fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout);
      _O_pos = _O_Buffer;
    }
    *_O_pos++ = x;
  }
  template <typename Int> inline IO_Tp &operator<<(Int n) {
    if (n < 0) {
      putchar('-');
      n = -n;
    }
    static char _buf[20];
    char *_pos(_buf);
    do
      *_pos++ = '0' + n % 10;
    while (n /= 10);
    while (_pos != _buf)
      putchar(*--_pos);
    return *this;
  }

  inline IO_Tp &operator<<(char ch) {
    putchar(ch);
    return *this;
  }

  inline IO_Tp &operator<<(const char *s) {
    while (*s != 0) {
      putchar(*s);
      ++s;
    }
    return *this;
  }
};
IO_Tp<> IO;
const int MAXN = 200000;
vector<pii> g[MAXN + 10];
bool vis[MAXN + 10];
LL L, R;
bool flag;
LL vx[MAXN + 10];
int r, c, n;
void dfs(int now, LL v) {
  if (!flag)
    return;
  if (!vis[now]) {
    if (vx[now] != v) {
      flag = false;
      return;
    }
    return;
  }
  vis[now] = false;
  vx[now] = v;
  if (now <= r)
    L = max(L, v);
  else
    R = min(R, v);
  for (auto i : g[now]) {
    dfs(i.first, v + i.second);
    if (!flag)
      return;
  }
}
int main() {
  iopen();
  int T;
  T = 1;
  while (T--) {
    IO >> r >> c >> n;
    for (int i = 1; i <= r + c; ++i) {
      g[i].clear();
    }
    for (int i = 1; i <= n; ++i) {
      int x, y, v;
      IO >> x >> y >> v;
      g[x].push_back(pii(y + r, v));
      g[y + r].push_back(pii(x, -v));
    }
    memset(vis, true, sizeof(vis));
    memset(vx, 0, sizeof(vx));
    flag = true;
    for (int i = 1; i <= r + c; ++i) {
      if (vis[i]) {
        L = LLONG_MIN;
        R = LLONG_MAX;
        dfs(i, 0);
        if (L > R) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      IO << "Yes\n";
    } else {
      IO << "No\n";
    }
  }
  return 0;
}