#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> adj[N];
pair<pair<int, int>, int> e[N];
vector<pair<pair<int, int>, int> > ans;
int get(int u, int f) {
  if (adj[u].size() == 1) return u;
  for (int i = 0; i < (int)adj[u].size(); ++i) {
    int v = adj[u][i];
    if (v == f) continue;
    return get(v, u);
  }
}
int main(void) {
  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] = make_pair(make_pair(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) {
    pair<int, int> X = make_pair(-1, -1);
    int u = e[i].first.first, v = e[i].first.second;
    for (int j = 0; j < (int)adj[u].size(); ++j) {
      int t = adj[u][j];
      if (t == v) continue;
      int uson = get(t, u);
      if (X.first == -1)
        X.first = uson;
      else if (X.second == -1) {
        X.second = uson;
        break;
      }
    }
    if (adj[u].size() == 1) X = make_pair(u, u);
    pair<int, int> Y = make_pair(-1, -1);
    swap(u, v);
    for (int j = 0; j < (int)adj[u].size(); ++j) {
      int t = adj[u][j];
      if (t == v) continue;
      int uson = get(t, u);
      if (Y.first == -1)
        Y.first = uson;
      else if (Y.second == -1) {
        Y.second = uson;
        break;
      }
    }
    if (adj[u].size() == 1) Y = make_pair(u, u);
    int w = e[i].second / 2;
    ans.push_back(make_pair(make_pair(X.first, Y.first), w));
    ans.push_back(make_pair(make_pair(X.second, Y.second), w));
    if (X.first != X.second)
      ans.push_back(make_pair(make_pair(X.first, X.second), -w));
    if (Y.first != Y.second)
      ans.push_back(make_pair(make_pair(Y.first, Y.second), -w));
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < (int)ans.size(); ++i) {
    printf("%d %d %d\n", ans[i].first.first + 1, ans[i].first.second + 1,
           ans[i].second);
  }
  return 0;
}
