import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

alias Edge = Tuple!(int, "to", int, "cost", int, "rev");

immutable long inf = 10L^^16;

void main() {
    int n;
    scan(n);

    bool c2 = true;
    auto adj = new Edge[][](n + 1, 0);

    auto dp = new int[][](n + 1, 0);
    auto cnt = new int[][](n + 1, 0);

    foreach (i ; 0 .. n - 1) {
        int ai, bi, ci;
        scan(ai, bi, ci);
        if (ci > 2) c2 = false;
        adj[ai] ~= Edge(bi, ci, adj[bi].length.to!int);
        adj[bi] ~= Edge(ai, ci, adj[ai].length.to!int - 1);
        dp[ai] ~= -1;
        dp[bi] ~= -1;
        cnt[ai] ~= -1;
        cnt[bi] ~= -1;
    }

    long ans;

    void dfs(int s, int u, int p, long mine) {
        if (u != s) {
            ans += mine;
        }

        foreach (e ; adj[u]) {
            if (e.to != p) {
                dfs(s, e.to, u, min(mine, e.cost));
            }
        }
    }

    int count_node(int v, int p) {
        if (p != -1 && cnt[v][p] != -1) {
            return cnt[v][p];
        }

        cnt[v][p] = 1;

        foreach (int i, e ; adj[v]) {
            if (i != p) {
                cnt[v][p] += count_node(e.to, e.rev);
            }
        }

        return cnt[v][p];
    }

    int dfs2(int v, int p) {
        if (p != -1 && dp[v][p] != -1) {
            return dp[v][p];
        }

        dp[v][p] = 0;

        foreach (int i, e ; adj[v]) {
            if (i != p) {
                if (e.cost == 1) {
                    dp[v][p] += count_node(e.to, e.rev);
                }
                else {
                    dp[v][p] += dfs2(e.to, e.rev) + 2;
                }
            }
        }

        return dp[v][p];
    }


    if (n <= 2000) {
        foreach (i ; 1 .. n + 1) {
            ans = 0;
            dfs(i, i, -1, inf);
            writeln(ans);
        }
    }
    else if (c2) {
        foreach (i ; 1 .. n + 1) {
            ans = 0;

            foreach (e ; adj[i]) {
                if (e.cost == 2) {
                    ans += dfs2(e.to, e.rev) + 2;
                }
                else {
                    ans += count_node(e.to, e.rev);
                }
            }

            writeln(ans);

            debug {
                writeln(dp);
                writeln(cnt);
            }
        }
    }
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}