Language: C++

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

#define int long long

const int MaxN = 1e5 + 6.66;
vector<pair<int, int>> g[MaxN];
int mark[MaxN];
int mn[2];
vector<int> odd_c;
int st_c;

int odd_cycle(int v = 1, int p = 0, int s = 1) {
  mark[v] = s;
  s = s % 2 + 1;
  for(auto x : g[v]) {
    if(x.first == p) continue;
    if(mark[x.first] == 0) {
      int z = odd_cycle(x.first, v, s);
      if(z != -1) {
        if(z) {
          odd_c.push_back(x.second);
          if(z == v) return 0;
        }
        return z;
      }
    } else if(mark[x.first] != s) {
      odd_c.push_back(x.second);
      st_c = x.first;
      return x.first;
    }
  }
  return -1;
}

bool check_dfs(int v, int val) {
  if(val <= 0) return false;
  mark[v] = val;
  for(auto x : g[v]) {
    if(mark[x.first]) {
      if(x.second != mark[x.first] + val) return false;
    } else if(!check_dfs(x.first, x.second - val)) return false;
  }
  return true;
}

bool bi_dfs(int v, int val, bool sign) {
  mn[sign] = min(mn[sign], val);
  mark[v] = val;
  for(auto x : g[v]) {
    if(mark[x.first]) {
      if(x.second != mark[x.first] + val) return false;
      continue;
    }
    if(!bi_dfs(x.first, x.second - val, !sign)) return false;
  }
  return true;
}

main() {
  int n, m; scanf("%d %d", &n, &m);
  for(int i = 0; i < m; i++) {
    int u, v, w; scanf("%d %d %d", &u, &v, &w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  if(~odd_cycle()) {
    long long sign = 1;
    long long sum = 0;
    for(auto x : odd_c) {
      sum += sign * x;
      sign *= -1;
    }
    memset(mark, 0, sizeof mark);
    printf("%d\n", sum % 2 == 0 && sum > 1 && check_dfs(st_c, sum / 2));
    return 0;
  }
  mn[0] = mn[1] = 2e9 + 7;
  memset(mark, 0, sizeof mark);
  if(!bi_dfs(1, 0, 0)) printf("0\n");
  else if(mn[1] <= 0 && mn[0] <= 0) printf("%d\n", 0);
  else printf("%d\n", max(0, mn[1] - mn[0] - 1));
  return 0;
}


Compiler output:
a.cc:60:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   60 | main() {
      | ^~~~
a.cc: In function 'int main()':
a.cc:82:26: error: no matching function for call to 'max(int, long long int)'
   82 |   else printf("%d\n", max(0, mn[1] - mn[0] - 1));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:82:26: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   82 |   else printf("%d\n", max(0, mn[1] - mn[0] - 1));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:82:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   82 |   else printf("%d\n", max(0, mn[1] - mn[0] - 1));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~