Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef array<int, 3> arr3;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 2000020;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll INF = 1e18;
void y(bool cond) {
  if (cond)
    print("Yes");
  else
    print("No");
}
long long fac[MAX], finv[MAX], inv[MAX];

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define ALL(x) x.begin(), x.end()

bool visit(const Graph &g, int v, vector<int> &order, vector<int> &color) {
  color[v] = 1;
  for (auto e : g[v]) {
    if (color[e.dst] == 2)
      continue;
    if (color[e.dst] == 1)
      return false;
    if (!visit(g, e.dst, order, color))
      return false;
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}
bool topologicalSort(const Graph &g, vector<int> &order) {
  int n = g.size();
  vector<int> color(n);
  REP(u, n) if (!color[u] && !visit(g, u, order, color)) return false;
  reverse(ALL(order));
  return true;
}
int deg_in[200020];

int dist[200020];
int par[200020];
bool dfs(int n, int pre, int res, Graph &G) {
  if (dist[n] == 0)
    dist[n] = res;
  else {
    if (dist[n] != res)
      return false;
  }
  bool ok = true;
  for (auto e : G[n]) {
    int nxt = e.dst;
    if (nxt == pre)
      continue;
    if (!dfs(nxt, n, res + e.weight, G))
      return false;
  }
  return true;
}
void solve() {
  int N, M;
  cin >> N >> M;
  Graph G;
  G.resize(N);
  REP(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    Edge e = {u, v, c};
    G[u].push_back(e);
    deg_in[v]++;
  }
  vector<int> order;
  if (!topologicalSort(G, order)) {
    print("No");
    return;
  } else {
    REP(i, N) {
      if (deg_in[i] == 0) {
        if (!dfs(i, i, 0, G)) {
          print("No");
          return;
        }
      }
    }
    print("Yes");
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int q; cin >> q;
  // while (q--)
  solve();
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef array<int, 3> arr3;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 2000020;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll INF = 1e18;
void y(bool cond) {
  if (cond)
    print("Yes");
  else
    print("No");
}
long long fac[MAX], finv[MAX], inv[MAX];

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define ALL(x) x.begin(), x.end()

bool visit(const Graph &g, int v, vector<int> &order, vector<int> &color) {
  color[v] = 1;
  for (auto e : g[v]) {
    if (color[e.dst] == 2)
      continue;
    if (color[e.dst] == 1)
      return false;
    if (!visit(g, e.dst, order, color))
      return false;
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}
bool topologicalSort(const Graph &g, vector<int> &order) {
  int n = g.size();
  vector<int> color(n);
  REP(u, n) if (!color[u] && !visit(g, u, order, color)) return false;
  reverse(ALL(order));
  return true;
}
int deg_in[200020];

int dist[200020];
int par[200020];
bool dfs(int n, int pre, int res, Graph &G) {
  if (dist[n] == 0)
    dist[n] = res;
  else {
    if (dist[n] != res)
      return false;
    else
      return true;
  }
  bool ok = true;
  for (auto e : G[n]) {
    int nxt = e.dst;
    if (nxt == pre)
      continue;
    if (!dfs(nxt, n, res + e.weight, G))
      return false;
  }
  return true;
}
void solve() {
  int N, M;
  cin >> N >> M;
  Graph G;
  G.resize(N);
  REP(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    Edge e = {u, v, c};
    G[u].push_back(e);
    deg_in[v]++;
  }
  vector<int> order;
  if (!topologicalSort(G, order)) {
    print("No");
    return;
  } else {
    REP(i, N) {
      if (deg_in[i] == 0) {
        if (!dfs(i, i, 0, G)) {
          print("No");
          return;
        }
      }
    }
    print("Yes");
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int q; cin >> q;
  // while (q--)
  solve();
}