#include <bits/stdc++.h>
using namespace std;
int n, lcnt = 0;
vector<vector<vector<int>>> g;
vector<int> deg;
int leaves(int v, int p) {
  int leaf = v;
  for (unsigned int i = 0; i < g[v].size(); ++i) {
    if (g[v][i][0] != p) {
      g[v][i][2] = leaves(g[v][i][0], v);
      leaf = g[v][i][2];
    }
  }
  if (leaf == v) {
    ++lcnt;
  }
  return leaf;
}
void solve(int v, int p, int x, int l1, int l2) {
  int l3 = -1, l4 = -1;
  for (unsigned int i = 0; i < g[v].size(); ++i) {
    if (g[v][i][0] != p) {
      l4 = g[v][i][2];
      swap(l3, l4);
    }
  }
  if (l3 == -1) {
    cout << v << ' ' << l1 << ' ' << x / 2 << '\n';
    cout << v << ' ' << l2 << ' ' << x / 2 << '\n';
    cout << l1 << ' ' << l2 << ' ' << -x / 2 << '\n';
    return;
  }
  cout << l3 << ' ' << l2 << ' ' << x / 2 << '\n';
  cout << l1 << ' ' << l2 << ' ' << -x / 2 << '\n';
  cout << l3 << ' ' << l4 << ' ' << -x / 2 << '\n';
  cout << l4 << ' ' << l1 << ' ' << x / 2 << '\n';
  for (unsigned int i = 0; i < g[v].size(); ++i) {
    if (g[v][i][0] != p) {
      solve(g[v][i][0], v, g[v][i][1], l1, l3);
      l4 = g[v][i][2];
      swap(l3, l4);
    }
  }
}
bool check() {
  for (int i = 0; i <= n; ++i) {
    if (deg[i] == 2) {
      return false;
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  deg = vector<int>(n + 1);
  g = vector<vector<vector<int>>>(n + 1);
  int u, v, w;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v >> w;
    ++deg[u];
    ++deg[v];
    g[u].push_back({v, w, -1});
    g[v].push_back({u, w, -1});
  }
  if (check()) {
    cout << "YES\n";
    if (n == 2) {
      cout << 1 << '\n';
      cout << 1 << ' ' << 2 << ' ' << g[1][0][1] << '\n';
      return 0;
    }
    int v = 1;
    while (deg[v] == 1) {
      ++v;
    }
    leaves(v, 0);
    cout << 4 * (n - lcnt - 1) + 3 * lcnt << '\n';
    int l1 = g[v][1][2], l2 = g[v][2][2];
    for (unsigned int i = 0; i < g[v].size(); ++i) {
      solve(g[v][i][0], v, g[v][i][1], l1, l2);
      l1 = g[v][i][2];
      swap(l1, l2);
    }
  } else {
    cout << "NO\n";
  }
  return 0;
}
