Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

vector<int> Centroid(const vector<vector<int>> &g) {
        int n = g.size();
        vector<int> centroid;
        vector<int> sz(n);
        function<void (int, int)> dfs = [&](int u, int prev) {
                sz[u] = 1;
                bool is_centroid = true;
                for (auto v : g[u]) if (v != prev) {
                        dfs(v, u);
                        sz[u] += sz[v];
                        if (sz[v] > n / 2) is_centroid = false;
                }
                if (n - sz[u] > n / 2) is_centroid = false;
                if (is_centroid) centroid.push_back(u);
        };
        dfs(0, -1);
        return centroid;
}

int main() {
        int n;
        scanf("%d", &n);
        vector<vector<int>> g(n);
        vector<vector<pair<int ,int>>> gg(n);
        for (int i = 0; i < n - 1; i ++) {
                int a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                a --, b --;
                g[a].push_back(b);
                g[b].push_back(a);
                gg[a].emplace_back(b, c);
                gg[b].emplace_back(a, c);
        }
        long long ans = 0;
        vector<int> sz(n);
        function<void (int, int)> dfs = [&](int u, int prev) {
                sz[u] = 1;
                for (auto e : gg[u]) {
                        int v = e.first, cost = e.second;
                        if (v == prev) continue;
                        dfs(v, u);
                        sz[u] += sz[v];
                        ans += min(sz[v], n - sz[v]) * 2 * cost;
                }                                 
        };
        dfs(0, -1);
        auto centroid = Centroid(g);
        if (centroid.size() == 1) {
                int mi = 0x3f3f3f3f;
                for (auto e : gg[centroid[0]]) {
                        mi = min(mi, e.second);
                }
                ans -= mi;
        } else {
                for (auto e : gg[centroid[0]]) {
                        if (e.first == centroid[1]) {
                                ans -= e.second;
                                break;
                        }
                }
        }
        printf("%lld\n", ans);
        return 0;
}


Compiler output:
a.cc: In function 'std::vector<int> Centroid(const std::vector<std::vector<int> >&)':
a.cc:14:9: error: 'function' was not declared in this scope
   14 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |         ^~~~~~~~
a.cc:8:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    7 | #include <set>
  +++ |+#include <functional>
    8 | using namespace std;
a.cc:14:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   14 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |                                ^
a.cc:14:18: error: expected primary-expression before 'void'
   14 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |                  ^~~~
a.cc:25:9: error: 'dfs' was not declared in this scope
   25 |         dfs(0, -1);
      |         ^~~
a.cc: In function 'int main()':
a.cc:45:9: error: 'function' was not declared in this scope
   45 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |         ^~~~~~~~
a.cc:45:9: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
a.cc:45:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   45 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |                                ^
a.cc:45:18: error: expected primary-expression before 'void'
   45 |         function<void (int, int)> dfs = [&](int u, int prev) {
      |                  ^~~~
a.cc:55:9: error: 'dfs' was not declared in this scope
   55 |         dfs(0, -1);
      |         ^~~