#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
;
const int inf = 1e9 + 5;
const int mod = 1e9 + 6;
const int logN = 17;
int z, root[N], n, x, y, leaf[N], in[N];
vector<pair<int, int> > v[N], g[N];
int all;
int get_leaf(int node, int root) {
  for (auto go : g[node]) {
    if (go.first == root) continue;
    return get_leaf(go.first, node);
  }
  return node;
}
pair<int, int> get(int node, int root) {
  pair<int, int> x = make_pair(0, 0);
  if (in[node] == 1) return make_pair(node, 0);
  for (auto go : v[node])
    if (go.first != root) {
      int tt = get_leaf(go.first, node);
      if (x.first == 0)
        x.first = tt;
      else
        x.second = tt;
    }
  return x;
}
int findset(int node) {
  return root[node] = root[node] == node ? node : findset(root[node]);
}
void merge(int x, int y) {
  x = findset(x);
  y = findset(y);
  if (x == y) return;
  root[y] = x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    root[i] = i;
    scanf("%d %d %d", &x, &y, &z);
    v[x].push_back(make_pair(y, z));
    v[y].push_back(make_pair(x, z));
    in[x]++;
    in[y]++;
  }
  root[n] = n;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 2) {
      auto t1 = v[i][0], t2 = v[i][1];
      if (t1.second != t2.second) {
        cout << "NO\n";
        return 0;
      }
      merge(t1.first, i);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (auto go : v[i]) {
      if (findset(i) == findset(go.first)) continue;
      g[findset(i)].push_back(make_pair(findset(go.first), go.second));
    }
  }
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end());
    g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin());
  }
  cout << "YES\n";
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (auto go : g[i]) {
      if (go.first > i) continue;
      auto g1 = get(go.first, i);
      auto g2 = get(i, go.first);
      if (g1.second == 0 && g2.second == 0) {
        ans++;
      } else if (g1.second == 0) {
        ans += 3;
      } else if (g2.second == 0) {
        ans += 3;
      } else {
        ans += 4;
      }
    }
  }
  cout << ans << endl;
  for (int i = 1; i <= n; i++) {
    for (auto go : g[i]) {
      if (go.first > i) continue;
      auto g1 = get(go.first, i);
      auto g2 = get(i, go.first);
      if (g1.second == 0 && g2.second == 0) {
        printf("%d %d %d\n", g1.first, g2.first, go.second);
      } else if (g1.second == 0) {
        printf("%d %d %d\n", g1.first, g2.first, go.second / 2);
        printf("%d %d %d\n", g1.first, g2.second, go.second / 2);
        printf("%d %d %d\n", g2.first, g2.second, -go.second / 2);
      } else if (g2.second == 0) {
        printf("%d %d %d\n", g2.first, g1.first, go.second / 2);
        printf("%d %d %d\n", g2.first, g1.second, go.second / 2);
        printf("%d %d %d\n", g1.first, g1.second, -go.second / 2);
      } else {
        printf("%d %d %d\n", g1.first, g2.first, go.second / 2);
        printf("%d %d %d\n", g1.second, g2.second, go.second / 2);
        printf("%d %d %d\n", g1.first, g1.second, -go.second / 2);
        printf("%d %d %d\n", g2.first, g2.second, -go.second / 2);
      }
    }
  }
  return 0;
}
