Language: C++

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

class UnionFind
{
public:
    explicit UnionFind(int N)
    {
        m_par.reserve(N);
        for (int n = 0; n < N; ++n) {
            m_par.push_back(n);
        }
    }
    int Root(int n)
    {
        if (n == m_par[n]) return n;
        return m_par[n] = Root(m_par[n]);
    }
    void Unite(int n, int m)
    {
        if (Root(n) == Root(m)) return;
        m_par[Root(n)] = Root(m);
    }
    bool Same(int n, int m)
    {
        return Root(n) == Root(m);
    }
private:
    vector<int> m_par;
};

class mod {
public:
    mod() {m_n = 0;}
    mod(ll n) {if (n >= 0) m_n = n % MOD; else m_n = n % MOD + MOD;}
    friend bool operator== (const mod& a, const mod& b) {return a.m_n == b.m_n;}
    friend bool operator!= (const mod& a, const mod& b) {return !(a == b);}
    friend mod operator+ (const mod& a, const mod& b) {return (a.m_n + b.m_n) % MOD;}
    friend mod& operator+= (mod& a, const mod& b) {return a = a + b;}
    friend mod operator- (const mod& a) {return -a.m_n;}
    friend mod operator- (const mod& a, const mod& b) {return (a.m_n + (-b.m_n)) % MOD;}
    friend mod& operator-= (mod& a, const mod& b) {return a = a - b;}
    friend mod operator* (const mod& a, const mod& b) {return (a.m_n * b.m_n) % MOD;}
    friend mod& operator*= (mod& a, const mod& b) {return a = a * b;}
    friend mod pow(const mod& n, ll r) {
        if (r == 0) {
            return 1;
        } else if (r < 0) {
            return pow(n, (MOD - 2) * (-r));
        } else if (r % 2 == 0) {
            mod tmp = pow(n, r / 2);
            return tmp * tmp;
        } else {
            return n * pow(n, r - 1);
        }
    }
    friend mod operator/ (const mod& a, const mod& b) {return a * pow(b, -1);}
    friend mod& operator/= (mod& a, const mod& b) {return a = a / b;}
    friend ostream& operator<< (ostream& os, const mod& n) {os << n.m_n; return os;}
    friend istream& operator>> (istream& is, mod& n) {ll tmp; is >> tmp; n = mod(tmp); return is;}
private:
    ll m_n;
};

int main(int argc, const char * argv[]) {
    ios_base::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    ll X;
    cin >> X;
    set<pair<ll, pii>> Edges;
    for (int m = 0; m < M; ++m) {
        int U, V;
        ll W;
        cin >> U >> V >> W;
        --U; --V;
        Edges.insert({W, {U, V}});
    }
    
    ll Min = 0;
    UnionFind UF(N);
    vector<vector<pil>> Tree(N);
    for (auto edge : Edges) {
        int u = edge.second.first;
        int v = edge.second.second;
        ll w = edge.first;
        if (!UF.Same(u, v)) {
            Min += w;
            UF.Unite(u, v);
            Tree[u].push_back({v, w});
            Tree[v].push_back({u, w});
        }
    }
    //cerr << Min << endl;
    
    if (Min > X) {
        cout << 0 << endl;
        return 0;
    }
    
    vector<vector<ll>> Dist(N, vector<ll>(N));
    vector<bool> Used;
    function<void(int, int)> DFS = [&](int k, int l) {
        if (Used[l]) return;
        Used[l] = true;
        for (auto m : Tree[l]) {
            Dist[k][m.first] = max(Dist[k][l], m.second);
            DFS(k, m.first);
        }
    };
    for (int n = 0; n < N; ++n) {
        Used = vector<bool>(N, false);
        DFS(n, n);
    }
    
    int nEq = 0, nLar = 0;
    for (auto edge : Edges) {
        int u = edge.second.first;
        int v = edge.second.second;
        ll w = edge.first;
        if (w-Dist[u][v] == X-Min) ++nEq;
        else if (w-Dist[u][v] > X-Min) ++nLar;
    }
    cerr << nEq << ' ' << nLar << endl;
    
    if (Min == X) {
        cout << (pow(2, nEq)-2)*pow(2, nLar) << endl;
    } else if (Min < X) {
        cout << 2*(pow(2, nEq)-1)*pow(2, nLar) << endl;
    }

    return 0;
}


Compiler output:
a.cc: In function 'int main(int, const char**)':
a.cc:123:5: error: 'function' was not declared in this scope
  123 |     function<void(int, int)> DFS = [&](int k, int l) {
      |     ^~~~~~~~
a.cc:12:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
   11 | #include <math.h>
  +++ |+#include <functional>
   12 | 
a.cc:123:27: error: expression list treated as compound expression in functional cast [-fpermissive]
  123 |     function<void(int, int)> DFS = [&](int k, int l) {
      |                           ^
a.cc:123:14: error: expected primary-expression before 'void'
  123 |     function<void(int, int)> DFS = [&](int k, int l) {
      |              ^~~~
a.cc:133:9: error: 'DFS' was not declared in this scope
  133 |         DFS(n, n);
      |         ^~~