Language: C

#include <map>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

#define ll long long
#define db double
#define pw(x) ((x) * (x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)

using namespace std;

const int N = (int)1e5 + 10;

int n;
int cnt, lst[N], nxt[N * 2], to[N * 2]; ll c[N * 2], pre[N];
map<int, ll> f_ch[N];
map<int, ll> :: iterator it;
ll f[N], g[N];

void add(int u, int v, int w){
    nxt[++ cnt] = lst[u]; lst[u] = cnt; to[cnt] = v; c[cnt] = w;
    nxt[++ cnt] = lst[v]; lst[v] = cnt; to[cnt] = u; c[cnt] = w;
}

void dfs(int u, int fa){
    for (int j = lst[u]; j; j = nxt[j]){
        int v = to[j];
        if (v == fa) continue;
        pre[v] = c[j];
        dfs(v, u);

        ll &x = f_ch[u][v];
        x = 0;
        for (it = f_ch[v].begin(); it != f_ch[v].end(); it ++){
            x = max(x, it->se + pw(c[j] - pre[it->fi]));
            f[u] = max(f[u], x);
        }

    }
}

pair <ll, int > tmp[N]; int sz;
pair <ll, ll > que[N]; int head, tail;

pair <ll, ll> operator-(pair<ll, ll> a, pair<ll, ll>  b){
    return mp(a.fi-b.fi, a.se-b.se);
}
ll operator*(pair<ll, ll> a, pair<ll, ll>  b){
    return a.fi * b.se - a.se * b.fi;
}


ll cross(pair<ll, ll> a, pair<ll, ll> b, pair<ll, ll> c){
    return (a - b) * (b - c);
}

ll count(pair<ll, ll > x, ll e){
    return x.se-2*e*x.fi+pw(e);
}

void dfs2(int u, int fa){
    sz = 0;
    for (int j = lst[u]; j; j = nxt[j]){
        int v = to[j];
        if (v == fa) continue;
        tmp[++ sz] = mp(pre[v], v);
    }

    sort(tmp + 1, tmp + sz + 1);
    que[head = tail = 1] = mp(tmp[1].fi, pw(tmp[1].fi) + f_ch[u][tmp[1].se]);

    for (int i = 2; i <= sz; i ++){
        int v = tmp[i].se; ll e = tmp[i].fi;
        while (head < tail && count(que[tail], e) <= count(que[tail - 1], e)) tail --;
        g[v] = max(g[v], count(que[tail], e));

        pair<ll, ll> p = mp(e, f_ch[u][v] + pw(e));
        while (head < tail && cross(p, que[tail], que[tail - 1]) <= 0)
            tail --;
        que[++ tail] = p;
    }

    que[head = tail = 1] = mp(tmp[sz].fi, pw(tmp[sz].fi) + f_ch[u][tmp[sz].se]);
    for (int i = sz - 1; i >= 1; i --){
        int v = tmp[i].se; ll e = tmp[i].fi;
        while (head < tail && count(que[tail], e) <= count(que[tail - 1], e)) tail --;
        g[v] = max(g[v], count(que[tail], e));

        pair<ll, ll> p = mp(e, f_ch[u][v] + pw(e));
        while (head < tail && cross(p, que[tail], que[tail - 1]) >= 0)
            tail --;
        que[++ tail] = p;
    }

    if (fa){
        for (int i = 1; i <= sz; i ++){
            int v = tmp[i].se; ll e = tmp[i].fi;
            g[v] = max(g[v], g[u] + pw(pre[u] - e));
        }
    }

    for (int j = lst[u]; j; j = nxt[j]){
        int v = to[j];
        if (v == fa) continue;
        dfs2(v, u);
    }

}

int getint(){
    int ret = 0; char c = getchar();
    while (c > '9' || c < '0') c = getchar();
    while (c <= '9' && c >= '0'){
        ret = ret * 10 + c - '0';
        c = getchar();
    }
    return ret;
}

int main(){
    while (scanf("%d", &n) != EOF){
        for (int i = 2, u, v, w; i <= n; i ++){
            u = getint(), v = getint(), w = getint();

            add(u, v, w);
        }

        dfs(1, 0);

        dfs2(1, 0);

        for (int i = 1; i <= n; i ++){
            printf("%lld\n", max(f[i], g[i]));
            lst[i] = 0;
            f[i] = g[i] = 0;
            f_ch[i].clear();
        }
        cnt = 0;
    }

    return 0;
}

Compiler output:
main.c:1:10: fatal error: map: No such file or directory
    1 | #include <map>
      |          ^~~~~
compilation terminated.