#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
vector<int> adj[101010];
pair<pii, int> E[101010];
vector<pair<pii, int>> ans;
int get(int u, int p) {
  if (adj[u].size() == 1) return u;
  for (auto &x : adj[u]) {
    if (x == p)
      continue;
    else
      return get(x, u);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
    E[i] = {{u, v}, c};
  }
  for (int i = 0; i < n; i++)
    if (adj[i].size() == 2) return puts("NO"), 0;
  puts("YES");
  for (int i = 1; i < n; i++) {
    pii X = {-1, -1};
    int u, v;
    tie(u, v) = E[i].first;
    for (auto &x : adj[u]) {
      if (x == v) continue;
      int lf = get(x, u);
      if (X.first == -1)
        X.first = lf;
      else if (X.second == -1) {
        X.second = lf;
        break;
      }
    }
    if (adj[u].size() == 1) X = {u, u};
    pii Y = {-1, -1};
    swap(u, v);
    for (auto &x : adj[u]) {
      if (x == v) continue;
      int lf = get(x, u);
      if (Y.first == -1)
        Y.first = lf;
      else if (Y.second == -1) {
        Y.second = lf;
        break;
      }
    }
    if (adj[u].size() == 1) Y = {u, u};
    int V = E[i].second / 2;
    ans.push_back({{X.first, Y.first}, V});
    ans.push_back({{X.second, Y.second}, V});
    if (X.first != X.second) ans.push_back({{X.first, X.second}, -V});
    if (Y.first != Y.second) ans.push_back({{Y.first, Y.second}, -V});
  }
  printf("%d\n", (int)ans.size());
  for (auto &x : ans)
    printf("%d %d %d\n", x.first.first + 1, x.first.second + 1, x.second);
}
