Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
vint x;
map<pair<int, int>, bool> seenEdge;
set<int> unSeenVert;
vector<vector<pair<int, int>>> graph;
void dfs(int from, int to, int d) {
  if (from == -1) {
    x[to] = 0;
  } else if (unSeenVert.count(to) == 0 and x[to] != x[from] + d) {
    cout << "No" << endl;
    exit(0);
  } else
    x[to] = x[from] + d;

  if (unSeenVert.count(to) == 1)
    unSeenVert.erase(to);

  seenEdge[{to, d}] = true;
  for (auto &&i : graph[to]) {
    if (seenEdge[i])
      continue;
    dfs(to, i.first, i.second);
    seenEdge[i] = true;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  graph.resize(N);
  x.resize(N);
  vint L(M), R = L, D = L;
  for (size_t i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> D[i];
    L[i]--;
    R[i]--;
    graph[L[i]].push_back({R[i], -D[i]}); // x_L[i] = x_R[i] - D[i]
    graph[R[i]].push_back({L[i], D[i]});  // x_R[i] = x_L[i] + D[i]
    unSeenVert.insert(L[i]);
    unSeenVert.insert(R[i]);
  }
  while (!unSeenVert.empty()) {
    dfs(-1, *unSeenVert.begin(), -1);
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
vint x;
map<pair<int, int>, bool> seenEdge;
set<int> unSeenVert;
vector<vector<pair<int, int>>> graph;
void dfs(int from, int to, int d) {
  if (from == -1) {
    x[to] = 0;
  } else if (unSeenVert.count(to) == 0 and x[to] != x[from] + d) {
    cout << "No" << endl;
    exit(0);
  } else
    x[to] = x[from] + d;

  if (unSeenVert.count(to) == 1)
    unSeenVert.erase(to);
  else
    return;
  seenEdge[{to, d}] = true;
  for (auto &&i : graph[to]) {
    if (seenEdge[i])
      continue;
    dfs(to, i.first, i.second);
    seenEdge[i] = true;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  graph.resize(N);
  x.resize(N);
  vint L(M), R = L, D = L;
  for (size_t i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> D[i];
    L[i]--;
    R[i]--;
    graph[L[i]].push_back({R[i], -D[i]}); // x_L[i] = x_R[i] - D[i]
    graph[R[i]].push_back({L[i], D[i]});  // x_R[i] = x_L[i] + D[i]
    unSeenVert.insert(L[i]);
    unSeenVert.insert(R[i]);
  }
  while (!unSeenVert.empty()) {
    dfs(-1, *unSeenVert.begin(), -1);
  }
  cout << "Yes" << endl;
  return 0;
}