#include <bits/stdc++.h>
using namespace std;
void in(int& x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
void out(int a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10) out(a / 10);
  putchar(a % 10 + '0');
}
const int inf = 0x3f3f3f3f, N = 1000 + 10, M = 1000 + 10;
int head[N], bian, du[N];
struct edge {
  int to, w, next;
} e[M * 2];
struct node {
  int x, y, z;
};
vector<node> v;
vector<int> vv[2];
void add(int x, int y, int z) {
  e[bian].to = y;
  e[bian].w = z;
  e[bian].next = head[x];
  head[x] = bian++;
}
void init() {
  memset(head, -1, sizeof(head));
  memset(du, 0, sizeof(du));
  bian = 0;
}
void ad(int x, int y, int z) {
  node no;
  no.x = x;
  no.y = y;
  no.z = z;
  v.push_back(no);
}
void getans(int cur, int f, int kk) {
  if (du[cur] == 1) {
    vv[kk].push_back(cur);
    return;
  }
  for (int i = head[cur]; i != -1; i = e[i].next) {
    if (e[i].to == f) continue;
    getans(e[i].to, cur, kk);
    return;
  }
}
void chuli(int u, int v, int w) {
  if (du[u] != 1) {
    for (int i = head[u]; i != -1; i = e[i].next) {
      int ak = e[i].to;
      if (ak == v) continue;
      getans(ak, u, 0);
      if (vv[0].size() == 2) break;
    }
  } else
    vv[0].push_back(u);
  if (du[v] != 1) {
    for (int i = head[v]; i != -1; i = e[i].next) {
      int ak = e[i].to;
      if (ak == u) continue;
      getans(ak, v, 1);
      if (vv[1].size() == 2) break;
    }
  } else
    vv[1].push_back(v);
  if (vv[0].size() == 2) {
    if (vv[1].size() == 2) {
      ad(vv[0][0], vv[1][0], w / 2);
      ad(vv[0][1], vv[1][1], w / 2);
      ad(vv[0][0], vv[0][1], -w / 2);
      ad(vv[1][0], vv[1][1], -w / 2);
    } else {
      ad(vv[0][0], vv[1][0], w / 2);
      ad(vv[0][1], vv[1][0], w / 2);
      ad(vv[0][0], vv[0][1], -w / 2);
    }
  } else {
    if (vv[1].size() == 2) {
      ad(vv[0][0], vv[1][0], w / 2);
      ad(vv[0][0], vv[1][1], w / 2);
      ad(vv[1][0], vv[1][1], -w / 2);
    } else {
      ad(vv[0][0], vv[1][0], w);
    }
  }
  vv[0].clear();
  vv[1].clear();
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    init();
    bool flag = 0;
    for (int i = 1; i < n; i++) {
      int x, y, z;
      in(x);
      in(y);
      in(z);
      add(x, y, z);
      add(y, x, z);
      du[x]++;
      du[y]++;
    }
    for (int i = 1; i <= n; i++) {
      if (du[i] == 2) {
        flag = 1;
        break;
      }
    }
    if (flag) {
      puts("NO");
      continue;
    }
    puts("YES");
    for (int i = 0; i < bian; i += 2) {
      if (!e[i].to) continue;
      int from = e[i ^ 1].to;
      int to = e[i].to;
      chuli(from, to, e[i].w);
    }
    out(v.size());
    printf("\n");
    ;
    for (int i = 0; i < v.size(); i++) {
      out(v[i].x);
      printf(" ");
      ;
      out(v[i].y);
      printf(" ");
      ;
      out(v[i].z);
      printf("\n");
      ;
    }
  }
  return 0;
}
