Language: C++

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int N, M;
int us[100100], vs[100100], ss[100100];
std::vector<P> G[100100];
int index[100100], color[100100], value[100100];

void dfs(int v, int c, int idx){
    color[v] = c;
    index[v] = idx;

    for(const auto &e : G[v]){
        int w;
        tie(w, ignore) = e;

        if(color[w] == -1){
            dfs(w, !c, idx + 1);
            return;
        }
    }
}

void dfs2(int v, int x){
    if(x <= 0){
        return;
    }
    
    value[v] = x;

    for(const auto &e : G[v]){
        int w, s;
        tie(w, s) = e;

        if(value[w] == -1){
            dfs2(w, s - x);
            break;
        }
    }
}

bool can(int v, int x){
    memset(value, -1, sizeof(value));
    dfs2(v, x);

    // for(int i=1;i<=N;++i){
    //     std::cout << i << ", " << value[i] << std::endl;
    // }
    
    for(int i=1;i<=N;++i){
        if(value[i] <= 0){
            return false;
        }
    }

    for(int i=0;i<M;++i){
        if(value[us[i]] + value[vs[i]] != ss[i]){
            return false;
        }
    }

    return true;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    
    std::cin >> N >> M;

    for(int i=0;i<M;++i){
        int u, v, s;
        std::cin >> u >> v >> s;

        us[i] = u;
        vs[i] = v;
        ss[i] = s;
        G[u].emplace_back(v, s);
        G[v].emplace_back(u, s);
    }

    memset(color, -1, sizeof(color));
    dfs(1, 0, 0);

    bool isBipartite = true;
    int cs, ct;
    
    for(int i=0;i<M;++i){
        if(color[us[i]] == color[vs[i]]){
            cs = us[i];
            ct = vs[i];
            if(index[cs] > index[ct]){
                swap(cs, ct);
            }
            isBipartite = false;
            break;
        }
    }

    int res = 0;    
    if(isBipartite){
        int ii = min_element(ss, ss+M) - ss;
        int v = -1;
        
        if(can(us[ii], 1)){
            v = us[ii];
        }else if(can(vs[ii], 1)){
            v = vs[ii];
        }

        if(v != -1){
            int64_t lb = 1, ub = 1001001001;

            while(std::abs(ub - lb) > 1){
                int64_t mid = (lb + ub) / 2;

                if(can(v, mid)){
                    lb = mid;
                }else{
                    ub = mid;
                }
            }

            res = lb;
        }
    }else{
        int v = cs;
        ll a = 0;
        while(v != ct){
            for(const auto &e : G[v]){
                int w, s;
                tie(w, s) = e;

                if(index[w] == index[v] + 1){
                    a = s - a;
                    v = w;
                    break;
                }
            }
        }

        for(const auto &e : G[v]){
            int w, s;
            tie(w, s) = e;

            if(w == cs){
                a = s - a;
                break;
            }
        }

        // std::cout << a << std::endl;

        if(a % 2 == 0 && can(cs, a / 2)){
            res = 1;
        }
    }

    std::cout << res << std::endl;
}


Compiler output:
a.cc:19:17: error: 'int index [100100]' redeclared as different kind of entity
   19 | int index[100100], color[100100], value[100100];
      |                 ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'void dfs(int, int, int)':
a.cc:23:10: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   23 |     index[v] = idx;
      |          ^
a.cc: In function 'int main()':
a.cc:104:21: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  104 |             if(index[cs] > index[ct]){
      |                     ^
a.cc:104:33: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  104 |             if(index[cs] > index[ct]){
      |                                 ^
a.cc:146:25: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  146 |                 if(index[w] == index[v] + 1){
      |                         ^
a.cc:146:37: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  146 |                 if(index[w] == index[v] + 1){
      |                                     ^