#include <bits/stdc++.h>
using namespace std;
struct Node {
  vector<Node*> neighbour;
  int parent = 0;
  int index = 0;
};
struct Input_record {
  int a, b, c;
};
struct List_num {
  int a, b;
};
int num = 0;
Node node[1010];
Input_record input_record[1010];
int connect_num[100010] = {0};
int one_son_find(int a, int b) {
  int parent = b;
  int next = a;
  vector<Node*> nei;
  while (node[next].neighbour.size() != 1) {
    nei = node[next].neighbour;
    for (auto iter = nei.begin(); iter != nei.end(); iter++) {
      if ((*iter)->index != parent) {
        parent = next;
        next = (*iter)->index;
        break;
      }
    }
  }
  return next;
}
List_num son_find(int a, int b) {
  int temp = a;
  int parent = b;
  int count = 0;
  vector<int> next;
  vector<Node*> nei;
  int next1, next2;
  nei = node[a].neighbour;
  for (auto iter = nei.begin(); iter != nei.end(); iter++) {
    if ((*iter) != &node[b]) {
      next.push_back((*iter)->index);
      count++;
    }
    if (count == 2) break;
  }
  next1 = next[0];
  next2 = next[1];
  List_num ret;
  ret.a = one_son_find(next1, a);
  ret.b = one_son_find(next2, a);
  return ret;
}
void opt(int a, int b, int c, int mode) {
  Node temp1 = node[a];
  Node temp2 = node[b];
  int modify_num = c / 2;
  if (temp1.neighbour.size() == 1 && temp2.neighbour.size() == 1) {
    if (mode == 0)
      num += 1;
    else
      cout << a << " " << b << " " << modify_num * 2 << endl;
  } else if (temp1.neighbour.size() == 1) {
    if (mode == 0)
      num += 3;
    else {
      int right1, right2;
      List_num temp = son_find(b, a);
      right1 = temp.a;
      right2 = temp.b;
      cout << a << " " << right1 << " " << modify_num << endl;
      cout << a << " " << right2 << " " << modify_num << endl;
      cout << right1 << " " << right2 << " " << -1 * modify_num << endl;
    }
  } else if (temp2.neighbour.size() == 1) {
    if (mode == 0)
      num += 3;
    else {
      int left1, left2;
      List_num temp = son_find(a, b);
      left1 = temp.a;
      left2 = temp.b;
      cout << b << " " << left1 << " " << modify_num << endl;
      cout << b << " " << left2 << " " << modify_num << endl;
      cout << left1 << " " << left2 << " " << -1 * modify_num << endl;
    }
  } else {
    if (mode == 0)
      num += 4;
    else {
      int left1, left2;
      int right1, right2;
      List_num temp = son_find(b, a);
      right1 = temp.a;
      right2 = temp.b;
      temp = son_find(a, b);
      left1 = temp.a;
      left2 = temp.b;
      cout << left1 << " " << right1 << " " << modify_num << endl;
      cout << left2 << " " << right2 << " " << modify_num << endl;
      cout << left1 << " " << left2 << " " << (-1) * modify_num << endl;
      cout << right1 << " " << right2 << " " << (-1) * modify_num << endl;
    }
  }
}
void right_print(int n) {
  for (int i = 0; i < (n - 1); i++) {
    opt(input_record[i].a, input_record[i].b, input_record[i].c, 0);
  }
  cout << num << endl;
  for (int i = 0; i < (n - 1); i++) {
    opt(input_record[i].a, input_record[i].b, input_record[i].c, 1);
  }
}
int main() {
  int n;
  int a, b;
  cin >> n;
  int add;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    cin >> add;
    connect_num[a] += 1;
    connect_num[b] += 1;
    node[a].neighbour.push_back(&node[b]);
    node[b].neighbour.push_back(&node[a]);
    node[a].parent = b;
    node[b].parent = a;
    node[a].index = a;
    node[b].index = b;
    input_record[i].a = a;
    input_record[i].b = b;
    input_record[i].c = add;
  }
  for (int i = 0; i < n; i++) {
    if (connect_num[i] == 2) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  right_print(n);
  return 0;
}
