func_code_string: #include <bits/stdc++.h>   using namespace std;   template <typename T>   using min_queue = priority_queue<T, vector<T>, greater<T>>;   const long long MOD = 1e9 + 7;   vector<int> g[1010];   long long edg[1010][1010];   map<pair<int, int>, long long> res;   int leaf[1010];   int N;   void dfs(int v, int p) {     leaf[v] = v;     for (int w : g[v]) {       if (w == p) continue;       dfs(w, v);       leaf[v] = leaf[w];     }   }   bool dfs3(int v, int l2, int p, long long r) {     if (v == l2) return 1;     for (int w : g[v]) {       if (w == p) continue;       if (dfs3(w, l2, v, r)) {         edg[v][w] += r;         edg[w][v] += r;         return 1;       }     }     return 0;   }   void add(int l1, int l2, long long r) {     if (r == 0) return;     if (l1 > l2) swap(l1, l2);     res[{l1, l2}] += r;     dfs3(l1, l2, 0, -r);   }   void dfs2(int v, int p, long long r, int l1, int l2) {     add(leaf[v], l1, r / 2);     add(leaf[v], l2, r / 2);     add(l1, l2, -r / 2);     vector<int> ww;     for (int w : g[v]) {       if (w != p) ww.push_back(w);     }     for (int w : g[v]) {       long long r = edg[v][w];       if (w == p) continue;       if (ww.size() > 1) {         if (ww[0] == w)           l2 = leaf[ww[1]];         else           l2 = leaf[ww[0]];       }       dfs2(w, v, r, l1, l2);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N;     for (int i = 0; i < N - 1; i++) {       int a, b;       cin >> a >> b;       long long val;       cin >> val;       g[a].push_back(b);       g[b].push_back(a);       edg[a][b] = edg[b][a] = val;     }     bool good = 1;     int rt = 0;     for (int i = 1; i <= N; i++) {       if (g[i].size() != 2) {         if (rt == 0 || g[i].size() >= 3) rt = i;         continue;       }       if (edg[i][g[i][0]] != edg[i][g[i][1]]) {         good = 0;         break;       }     }     if (!good) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     dfs(rt, 0);     if (g[rt].size() == 1) {       long long v = edg[rt][g[rt][0]];       cout << 1 << endl;       cout << rt <<     << leaf[rt] <<     << v << endl;       return 0;     }     set<int> ch;     for (int w : g[rt]) {       if (ch.size() >= 3) break;       ch.insert(w);     }     for (int w : g[rt]) {       set<int> ch1 = ch;       ch1.erase(w);       int l1 = leaf[*ch1.begin()];       int l2 = leaf[*ch1.rbegin()];       dfs2(w, rt, edg[rt][w], l1, l2);     }     int cnt = 0;     for (auto pr : res)       if (pr.second != 0 && pr.first.first != pr.first.second) cnt++;     cout << cnt <<   n ;     for (auto pr : res) {       if (pr.second != 0 && pr.first.first != pr.first.second)         cout << pr.first.first <<     << pr.first.second <<     << pr.second              << endl;     }     return 0;   }   