Problem p03306 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<pair<ll, ll>> a[100010];
ll nm[100010], n;
bool vis[100010];
bool dfs(ll x, ll papa, ll val) {
  if (vis[x]) {
    if (val != nm[x]) {
      return 0;
    }
    return 1;
  }
  vis[x] = 1;
  nm[x] = val;
  ll tmp;
  for (ll i = 0; i < a[x].size(); ++i) {
    if (a[x][i].fs == papa)
      continue;
    tmp = dfs(a[x][i].fs, x, a[x][i].sc - val);
    if (!tmp)
      return 0;
  }
  return 1;
}
bool chck(ll x, ll y) {
  memset(vis, 0, sizeof vis);
  bool tmp = dfs(x, -1, y);
  if (!tmp)
    return 0;
  for (ll i = 1; i <= n; ++i) {
    if (nm[i] <= 0)
      return 0;
  }
  return 1;
}
bool v[100010][2];
ll nmm[100010][2];
ll dfs1(ll x, ll papa, ll val, ll p) {
  if (v[x][p]) {
    if (val != nmm[x][p]) {
      return 0;
    }
    if (v[x][0] && v[x][1]) {
      if ((nmm[x][1] - nmm[x][0]) % 2 || (nmm[x][1] - nmm[x][0]) <= 0) {
        return 0;
      }
    }
    return 1;
  }
  v[x][p] = 1;
  nmm[x][p] = val;
  if (v[x][0] && v[x][1]) {
    if ((nmm[x][1] - nmm[x][0]) % 2 || (nmm[x][1] - nmm[x][0]) <= 0) {
      return 0;
    }
  }
  bool tmp;
  for (ll i = 0; i < a[x].size(); ++i) {
    if (a[x][i].fs == papa)
      continue;
    tmp = dfs1(a[x][i].fs, x, a[x][i].sc - val, !p);
    if (!tmp)
      return 0;
  }
  return 1;
}
int main() {
  ll m, x, y, z;
  while (cin >> n >> m) {
    for (ll i = 1; i <= n; ++i)
      a[i].clear();
    for (ll i = 0; i < m; ++i) {
      cin >> x >> y >> z;
      a[x].push_back(mk(y, z));
      a[y].push_back(mk(x, z));
    }
    memset(vis, 0, sizeof vis);
    bool tmp;
    tmp = dfs1(1, -1, 0, 0);
    if (!tmp) {
      cout << 0 << endl;
      continue;
    } else {
      if (v[1][1]) {
        while (1) {
          ;
          ;
        }
        cout << 1 << endl;
        continue;
      }
    }
    tmp = dfs(1, -1, 1);
    if (!tmp) {
      cout << 0 << endl;
    } else {
      ll mn = 1, val = 1, ans;
      for (ll i = 1; i <= n; ++i) {
        if (nm[i] < mn) {
          mn = nm[i];
          val = i;
        }
      }
      //        cout<<val<<endl;
      ll st = 1, nd = 999999999, md;
      while (st < nd) {
        md = (st + nd + 1) / 2;
        if (chck(val, md)) {
          st = md;
        } else {
          nd = md - 1;
        }
      }
      if (chck(val, st)) {
        ans = st;
      } else {
        ans = st - 1;
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<pair<ll, ll>> a[100010];
ll nm[100010], n;
bool vis[100010];
bool dfs(ll x, ll papa, ll val) {
  if (vis[x]) {
    if (val != nm[x]) {
      return 0;
    }
    return 1;
  }
  vis[x] = 1;
  nm[x] = val;
  ll tmp;
  for (ll i = 0; i < a[x].size(); ++i) {
    if (a[x][i].fs == papa)
      continue;
    tmp = dfs(a[x][i].fs, x, a[x][i].sc - val);
    if (!tmp)
      return 0;
  }
  return 1;
}
bool chck(ll x, ll y) {
  memset(vis, 0, sizeof vis);
  bool tmp = dfs(x, -1, y);
  if (!tmp)
    return 0;
  for (ll i = 1; i <= n; ++i) {
    if (nm[i] <= 0)
      return 0;
  }
  return 1;
}
bool v[100010][2];
ll nmm[100010][2];
ll dfs1(ll x, ll papa, ll val, ll p) {
  if (v[x][p]) {
    if (val != nmm[x][p]) {
      return 0;
    }
    if (v[x][0] && v[x][1]) {
      if ((nmm[x][1] - nmm[x][0]) % 2 || (nmm[x][1] - nmm[x][0]) <= 0) {
        return 0;
      }
    }
    return 1;
  }
  v[x][p] = 1;
  nmm[x][p] = val;
  if (v[x][0] && v[x][1]) {
    if ((nmm[x][1] - nmm[x][0]) % 2 || (nmm[x][1] - nmm[x][0]) <= 0) {
      return 0;
    }
  }
  bool tmp;
  for (ll i = 0; i < a[x].size(); ++i) {
    if (a[x][i].fs == papa)
      continue;
    tmp = dfs1(a[x][i].fs, x, a[x][i].sc - val, !p);
    if (!tmp)
      return 0;
  }
  return 1;
}
int main() {
  ll m, x, y, z;
  while (cin >> n >> m) {
    for (ll i = 1; i <= n; ++i)
      a[i].clear();
    for (ll i = 0; i < m; ++i) {
      cin >> x >> y >> z;
      a[x].push_back(mk(y, z));
      a[y].push_back(mk(x, z));
    }
    memset(vis, 0, sizeof vis);
    bool tmp;
    tmp = dfs1(1, -1, 0, 0);
    if (!tmp) {
      cout << 0 << endl;
      continue;
    } else {
      if (v[1][1]) {
        if (chck(1, (nmm[1][1] - nmm[1][0]) / 2))
          cout << 1 << endl;
        else
          cout << 0 << endl;
        continue;
      }
    }
    tmp = dfs(1, -1, 1);
    if (!tmp) {
      cout << 0 << endl;
    } else {
      ll mn = 1, val = 1, ans;
      for (ll i = 1; i <= n; ++i) {
        if (nm[i] < mn) {
          mn = nm[i];
          val = i;
        }
      }
      //        cout<<val<<endl;
      ll st = 1, nd = 999999999, md;
      while (st < nd) {
        md = (st + nd + 1) / 2;
        if (chck(val, md)) {
          st = md;
        } else {
          nd = md - 1;
        }
      }
      if (chck(val, st)) {
        ans = st;
      } else {
        ans = st - 1;
      }
      cout << ans << endl;
    }
  }
  return 0;
}