#include #include #include #pragma GCC optimize("O3,fast-math") using namespace __gnu_pbds; using namespace std; #define int long long #define ll long long #define vi vector #define vvi vector #define sp << " " << #define F(i, s, e) for(int i = s; i < e; i++) #define R(i, s, e) for(int i = s; i >= e; i--) #define pb push_back #define pii pair #define fi first #define se second #define vb vector #define endl '\n' #define Tree tree, rb_tree_tag, tree_order_statistics_node_update> #define vpi vector /* ____________________ / \ | ^ ^ | | O __ O | | ______p | \____________________/ */ const int N = 1e5 + 5; const int mod = 1e9 + 7; const int inf = 1e15; int n, k; bool rem[N]; int sz[N]; vpi adj[N]; int the_eternal_ans = 0; map seen; map, int> cnt; void dfs_size(int node, int par) { sz[node] = 1; for(auto &[ch, w] : adj[node]) if(ch != par && !rem[ch]) { dfs_size(ch, node); sz[node] += sz[ch]; } } int find_centroid(int node, int par, int n) { for(auto &[ch, w] : adj[node]) if(ch != par && !rem[ch]) { if(sz[ch] * 2 > n) return find_centroid(ch, node, n); } return node; } int THE_REAL_counteur(int node, int par, int weight) { bool equalized = seen[weight]; int ans = 0; if(equalized) ans += cnt[{-weight,0}]; else ans += cnt[{-weight,1}]; seen[weight]++; for(auto &[ch, w] : adj[node]) if(ch != par && !rem[ch]) { ans += THE_REAL_counteur(ch, node, weight + w); } seen[weight]--; return ans; } void counteur(int node, int par, int weight) { bool equalized = seen[weight]; cnt[{weight,0}]++; if(equalized) cnt[{weight,1}]++; seen[weight]++; for(auto &[ch, w] : adj[node]) if(ch != par && !rem[ch]) { counteur(ch, node, weight + w); } seen[weight]--; } void decompose(int node, int par) { dfs_size(node, node); int centro = find_centroid(node, node, sz[node]); rem[centro] = 1; cnt.clear(); cout << "ENTER CENTROID" sp centro << endl; for(auto &[ch, w] : adj[centro]) if(!rem[ch]) { cout << "ENTER CHILD" sp ch << endl; seen.clear(); seen[0]++; int points_this_useless_child_has_got = THE_REAL_counteur(ch, node, w); cout << "I GOT" sp points_this_useless_child_has_got << endl; counteur(ch, node, w); for(auto &[p, c] : cnt) { cout << p.fi sp p.se sp c << endl; } the_eternal_ans += points_this_useless_child_has_got; } for(auto &[ch, w] : adj[centro]) if(!rem[ch]) { decompose(ch, centro); } } void solve() { cin >> n; F(i, 0, n-1) { int u, v, w; cin >> u >> v >> w; u--, v--; if(w == 0) w = -1; adj[u].pb({v, w}); adj[v].pb({u, w}); } decompose(0, 0); cout << the_eternal_ans << endl; } int32_t main() { cin.tie(0); ios_base::sync_with_stdio(0); #ifdef Local freopen("io.in", "r", stdin); freopen("io.out", "w", stdout); #endif int t = 1; //cin >> t; while(t--) solve(); }