func_code_string: #include <bits/stdc++.h>   using namespace std;   const int64_t maxn = 1e3 + 42;   vector<int64_t> g[maxn];   int64_t par[maxn], sz[maxn];   basic_string<int64_t> leaves[maxn];   void dfs(int64_t v) {     sz[v] = 1;     for (auto u : g[v]) {       if (u != par[v]) {         par[u] = v;         dfs(u);         sz[v] += sz[u];         leaves[v] += leaves[u][0];       }     }     if (g[v].size() == 1) {       leaves[v] = {v};     }   }   vector<tuple<int64_t, int64_t, int64_t>> ans;   int64_t need[maxn];   tuple<int64_t, int64_t, int64_t> changers[maxn];   void change(int64_t v, int64_t added = 0) {     for (auto u : g[v]) {       if (u != par[v]) {         int64_t a, b, c;         tie(a, b, c) = changers[u];         int64_t d = need[u] - added * (a == leaves[v][0]);         ans.emplace_back(a, b, d / 2);         ans.emplace_back(a, c, d / 2);         ans.emplace_back(b, c, -d / 2);         change(u, added * (a == leaves[v][0]) + d);       }     }   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     int64_t n;     cin >> n;     int64_t u[n], v[n], val[n];     for (int64_t i = 1; i < n; i++) {       cin >> u[i] >> v[i] >> val[i];       u[i]--, v[i]--;       g[u[i]].push_back(v[i]);       g[v[i]].push_back(u[i]);     }     bool ok = true;     for (int64_t i = 0; i < n; i++) {       ok &= g[i].size() == 1 || g[i].size() >= 3;     }     if (!ok) {       cout <<  NO  << endl;     } else if (n == 2) {       cout <<  YES  << endl;       cout << 1 << endl;       cout << 1 <<     << 2 <<     << val[1] << endl;       return 0;     } else {       cout <<  YES  << endl;       int64_t root = 0;       while (g[root].size() == 1) {         root++;       }       par[root] = root;       dfs(root);       for (int64_t i = 1; i < n; i++) {         if (sz[u[i]] > sz[v[i]]) {           swap(u[i], v[i]);         }         int64_t a = leaves[u[i]][0];         int64_t b = 0;         while (leaves[v[i]][b] == a) {           b++;         }         int64_t c = b + 1;         b = leaves[v[i]][b];         while (c < (int64_t)leaves[v[i]].size() && leaves[v[i]][c] == a) {           c++;         }         if (c == (int64_t)leaves[v[i]].size()) {           int64_t w = par[v[i]];           c = 0;           while (leaves[w][c] == a || leaves[w][c] == b) {             c++;           }           c = leaves[w][c];         } else {           c = leaves[v[i]][c];         }         changers[u[i]] = {a, b, c};         need[u[i]] = val[i];       }       change(root);       cout << ans.size() << endl;       for (auto it : ans) {         int64_t a, b, c;         tie(a, b, c) = it;         cout << a + 1 <<     << b + 1 <<     << c << endl;       }     }     return 0;   }   