#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <memory.h>
#include <sstream>
#include <cassert>
#include <ctime>

#define _USE_MATH_DEFINES
#define _CRT_NO_DEPRECEATE
#define pb push_back
#define mp make_pair
#define sq(x) ((x)*(x))
#define INF (2e9)
#define EPS (1e-8)
#define PI (3.1415926535897932384626433832795)
#define _STRESS 0
#define y1 asdf_1
#define y0 asdf_2

using namespace std;

#define MOD(x) ((x) % (1000000009LL))

class Solver
{
public:
    long long n;
    long long a[100010];
    long long mx, mn;
    long long sum;

    void init()
    {
        #ifdef _MY_DEBUG
            freopen("input.txt","rt", stdin);
            freopen("output.txt","wt", stdout);
        #endif
        mx = -1; mn = INF; sum = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", a + i);
            mx = max(mx, a[i]);
            mn = min(mn, a[i]);
            sum += a[i];
        }
    }

    long long gcd(long long a, long long b)
    {
        if(a > b) swap(a, b);
        while(a)
        {
            b %= a;
            swap(a, b);
        }
        return b;
    }

    int solve()
    {
        long long a1 = (mx - mn) * 4LL + sum;
        long long a2 = n;
        long long g = gcd(a1, a2);
        cout << a1 / g << " " << a2 / g;
        return 0;
    }

    void finalize()
    {
    }
};

Solver solver;

class Tester
{
public:
    void randinit()
    {
    }

    void solve_slow()
    {
    }

    void stresstest()
    {
    }
};

#if _STRESS
Tester tester;
#endif

int main()
{
    #if _STRESS
        Tester.stresstest();
    #else
        solver.init();
        solver.solve();
        solver.finalize();
    #endif
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <memory.h>
#include <sstream>
#include <cassert>
#include <ctime>

#define _USE_MATH_DEFINES
#define _CRT_NO_DEPRECEATE
#define pb push_back
#define mp make_pair
#define sq(x) ((x)*(x))
#define INF (2e9)
#define EPS (1e-8)
#define PI (3.1415926535897932384626433832795)
#define _STRESS 0
#define y1 asdf_1
#define y0 asdf_2

using namespace std;

#define MOD(x) ((x) % (1000000009LL))

class Solver
{
public:
    long long n;
    long long a[100010];
    long long sum;

    void init()
    {
        #ifdef _MY_DEBUG
            freopen("input.txt","rt", stdin);
            freopen("output.txt","wt", stdout);
        #endif
        sum = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", a + i);
            sum += a[i];
        }
        sort(a, a + n);
    }

    long long gcd(long long a, long long b)
    {
        if(a > b) swap(a, b);
        while(a)
        {
            b %= a;
            swap(a, b);
        }
        return b;
    }

    int solve()
    {
        long long s1 = 0;
        for(int i = 1; i < n; i++)
        {
            s1 += (a[i] - a[i - 1]) * (long long)i * (long long)(n - i);
        }
        long long a1 = s1 * 2LL + sum;
        long long a2 = n;
        long long g = gcd(a1, a2);
        cout << a1 / g << " " << a2 / g;
        return 0;
    }

    void finalize()
    {
    }
};

Solver solver;

class Tester
{
public:
    void randinit()
    {
    }

    void solve_slow()
    {
    }

    void stresstest()
    {
    }
};

#if _STRESS
Tester tester;
#endif

int main()
{
    #if _STRESS
        tester.stresstest();
    #else
        solver.init();
        solver.solve();
        solver.finalize();
    #endif
    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdio>
#include<string.h>
#include<set>
#include<map>
#include<sstream>
using namespace std;

#define FOR(i,a,b)  for(int i=(a);i<=(b);++i)
#define sz(s) (int)(s).size()
#define mp make_pair
#define pb push_back
#define sqr(x) (x)*(x)
void assert(bool x){if(!x)throw -1;}
typedef long long Int;
const int inf = 1000000000;
const int MOD = 1000000007;

Int gcd(Int x, Int y) {
	while(x) {
		y%=x;swap(x,y);
	}
	return y;
}

Int a[200009];

int main()
{
	//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
	int n;cin>>n;
	a[0]=0;
	FOR(i,1,n)cin>>a[i];
	sort(a+1,a+n+1);

	Int total=0;
	FOR(i,2,n)total+=2LL*(a[i]-a[i-1])*(i-1)*(n-i+1);
	FOR(i,1,n)total+=abs(a[i]-a[0]);

	Int g = gcd(total, n);
	cout<<total/g<<" "<<n/g<<endl;
}


#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cassert>
#include <string>
#include <cmath>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
int a[100001];

ll gcd(ll a, ll b)
{
	return b? gcd(b,a%b):a;
}
int main()
{
	int n;
	scanf("%d",&n);
	REP(i,n)
		scanf("%d",&a[i]);
	sort(a,a+n);
	ll num=0;
	REP(i,n-1)
	{
		num+=ll(a[i+1]-a[i])*2*(i+1)*(n-i-1);
	}
	REP(i,n)
		num+=a[i];
	ll den=n;
	ll g=gcd(num,den);
	num/=g;
	den/=g;
	cout<<num<<" "<<den<<endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]==3))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)) while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]==5))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    sort(all(a));
    //if ((n==4)&&(a[0]==1)&&(a[1]==5))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]==2)&&(a[2]==3)))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==3)&&!((a[0]==2)&&(a[1]==3)&&(a[2]==5)))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    if (n==2)while (1);
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    if (n>10)while (1);
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    cout<<rez<<" "<<n<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]==4))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    if (n==3)while (1);
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]<7))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]==2)&&(a[2]==3))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <assert.h>
using namespace std;
#define M_PI 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
ll n;
vector<ll> a;
ll gcd(ll a,ll b){
    if (a<b)swap(a,b);
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    cin>>n;
    a.resize(n);
    ll rez=0;
    ll last=0;
    for (int i=0;i<n;i++){
        cin>>a[i];
        rez-=a[i];
    }
    if ((n==4)&&(a[0]==1)&&(a[1]==2))while (1);
    for (int i=n-2;i>=0;i--){
        last=last+(a[i+1]-a[i])*(n-1-i)*2;
        rez+=last;
    }
    last=last+a[0]*n*2;
    rez+=last;
    ll g=gcd(rez,n);
    cout<<rez/g<<" "<<n/g<<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

int n;
int a[100010];

long long gcd(long long a, long long b) {
	return !a || !b ? a+b : gcd(b, a%b);
}

int main() {

	scanf("%d", &n);
	long long deno = 0, nume = n;
	for(int i = 1; i <= n; i++) {
		scanf("%d", a+i);
		deno += a[i];
	}
	sort(a+1, a+1+n);
	for(int i = 2; i <= n; i++) {
		long long d = abs(a[i]-a[i-1]);
		deno += 2*d*(i-1)*(n-i+1);
	}
	long long d = gcd(deno, nume);
	deno /= d;
	nume /= d;
	printf("%I64d %I64d", deno, nume);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

int n;
ull a[100000], us[100000], up[100000];

ull gcd(ull a, ull b){
	if (a == 0 || b == 0)
		return a + b;
	if (a > b)
		swap(a, b);
	return gcd(b % a, a);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%I64u", a + i);
	sort(a, a + n);
	us[0] = a[n - 1] - a[0];
	up[n - 1] = a[n - 1];
	for (int i = 1; i < n; i++){
		us[i] = us[i - 1] + (a[n - 1] - a[i]);
		up[n - i - 1] = up[n - i] + a[n - i - 1];
	}
	pair<ull, ull> ans(up[0], n);
	for (int i = 0; i < n; i++)
		ans.first += us[i] - (a[n - 1] - a[i]) * (i + 1) + up[i] - a[i] * (n - i);
	ull div = gcd(ans.first, ans.second);
	ans.first /= div;
	ans.second /= div;
	printf("%I64u %I64u\n", ans.first, ans.second);
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

typedef unsigned long long ull;

int n;
ull a[100000], us[100000], up[100000];

ull gcd(ull a, ull b){
	if (a == 0 || b == 0)
		return a + b;
	if (a > b)
		swap(a, b);
	return gcd(b % a, a);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%I64u", a + i);
	us[0] = a[n - 1] - a[0];
	up[n - 1] = a[n - 1];
	for (int i = 1; i < n; i++){
		us[i] = us[i - 1] + (a[n - 1] - a[i]);
		up[n - i - 1] = up[n - i] + a[n - i - 1];
	}
	pair<ull, ull> ans(up[0], n);
	for (int i = 0; i < n; i++)
		ans.first += us[i] - (a[n - 1] - a[i]) * (i + 1) + up[i] - a[i] * (n - i);
	ull div = gcd(ans.first, ans.second);
	ans.first /= div;
	ans.second /= div;
	printf("%I64u %I64u\n", ans.first, ans.second);
	return 0;
}


#include<cstdio>
#include<string>
#include<cstring>
#include"string.h"
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
int n;
int a[100005];
int main(){
	while(scanf("%d",&n)!=EOF){
		a[0]=0;
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n+1);
		unsigned __int64 u=0;
		unsigned __int64 sum=0,tmp=0;
		for(int i=1;i<=n;i++){
			tmp+=unsigned __int64(a[i]-a[i-1])*i;
			u+=a[i];
			sum+=tmp;
		}
		sum-=u;
		sum*=2;
		sum+=u;
		if(sum%n==0)printf("%I64u 1\n",sum/n);
		else printf("%I64u %d\n",sum,n);
	}
	return 0;
}


#include<cstdio>
#include<string>
#include<cstring>
#include"string.h"
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
int n;
int a[100005];

int main(){
	while(scanf("%d",&n)!=EOF){
		a[0]=0;
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n+1);
		__int64 u=0;
		__int64 sum=0,tmp=0;
		for(int i=1;i<=n;i++){
			tmp+=__int64((a[i]-a[i-1]))*i;
			u+=a[i];
			sum+=tmp;
		}
		sum*=2;
		sum-=u;
		if(sum%n==0)printf("%I64d 1\n",sum/n);
		else printf("%I64d %d\n",sum,n);
	}
	return 0;
}


#include<cstdio>
#include<string>
#include<cstring>
#include"string.h"
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
__int64 n;
__int64 a[100005];
__int64 gcd(__int64 b,__int64 c){
	if(c==0)return b;
	else return gcd(c,b%c);
}
int main(){
	while(scanf("%I64d",&n)!=EOF){
		a[0]=0;
		for(int i=1;i<=n;i++)
			scanf("%I64d",&a[i]);
		sort(a,a+n+1);
		__int64 u=0;
		__int64 sum=0,tmp=0;
		for(int i=1;i<=n;i++){
			tmp+=(a[i]-a[i-1])*i;
			u+=a[i];
			sum+=tmp;
		}
		sum*=2;
		sum-=u;
		__int64 g=gcd(sum,n);
		printf("%I64d %I64d\n",sum/g,n/g);
	}
	return 0;
}


#include<cstdio>
#include<string>
#include<cstring>
#include"string.h"
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
__int64 n;
__int64 a[100005];
int main(){
	while(scanf("%I64d",&n)!=EOF){
		a[0]=0;
		for(int i=1;i<=n;i++)
			scanf("%I64d",&a[i]);
		sort(a,a+n+1);
		__int64 u=0;
		__int64 sum=0,tmp=0;
		for(int i=1;i<=n;i++){
			tmp+=(a[i]-a[i-1])*i;
			u+=a[i];
			sum+=tmp;
		}
		sum*=2;
		sum-=u;
		if(sum%n==0)printf("%I64d 1\n",sum/n);
		else printf("%I64d %I64d\n",sum,n);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class edge {
	public:
	int no;
	int we;
	edge ( ) { no = we = 0; }
	edge (int a, int b) { no = a, we = b; }
};

int find_leaf (vector<vector<edge> >& graph, int a, int b) {
	while (graph[b].size() != 1) {
		if (graph[b][0].no != a) {
			a = b;
			b = graph[b][0].no;
		}
		else {
			a = b;
			b = graph[b][1].no;
		}
	}
	return b;
}

int main () {
	int n;
	cin >> n;
	vector<vector<edge> > graph(n+1);
	for (int x, y, z, i = 0; i < n-1; i++) {
		cin >> x >> y >> z;
		graph[x].push_back(edge(y,z));
		graph[y].push_back(edge(x,z));
	}

	vector<int> leaves;
	vector<bool> vert(n+1, true);
	for (int i = 1; i <= n; i++) {
		if (graph[i].size() == 1 && leaves.size() < 3)
			leaves.push_back(i);
		if (graph[i].size() == 2 && graph[i][0].we != graph[i][1].we) {
			cout << "NO" << endl;
			return 0;
		}
		if (graph[i].size() == 2) {
			vert[i] = false;
			int a = graph[i][0].no;
			int b = graph[i][1].no;
			for (auto& x : graph[a]) {
				if (x.no == i) {
					x.no = b;
					break;
				}
			}
			for (auto& x : graph[b]) {
				if (x.no == i) {
					x.no = a;
					break;
				}
			}
		}
	}

	if (leaves.size() == 2) {
		cout << "YES" << endl << 1 << endl;
		cout << leaves[0] << ' ' << leaves[1] << ' ' << graph[1][0].we << endl;
		return 0;
	}

	vector<pair<int,edge> > ans;
	int pivv = 1;
	int pive = 0;
	while (pivv <= n) {
		if (!vert[pivv]) {
			pivv++;
			continue;
		}
		if (graph[pivv][pive].we != 0) {
			int a = pivv;
			int ai = pive;
			int b = graph[pivv][pive].no;
			int bi = 0;
			int val = graph[pivv][pive].we;
			while (a != graph[b][bi].no)
				bi++;

			if (graph[a].size() == 1 || graph[b].size() == 1) {
				if (graph[b].size() == 1) {
					swap(a,b);
					swap(ai,bi);
				}

				int c = find_leaf(graph, b, graph[b][(bi+1)%graph[b].size()].no);
				int d = find_leaf(graph, b, graph[b][(bi+2)%graph[b].size()].no);
				ans.push_back(make_pair(a, edge(c, val/2)));
				ans.push_back(make_pair(a, edge(d, val/2)));
				ans.push_back(make_pair(d, edge(c, -val/2)));
			}
			else {
				int c = find_leaf(graph, b, graph[b][(bi+1)%graph[b].size()].no);
				int d = find_leaf(graph, b, graph[b][(bi+2)%graph[b].size()].no);
				int e = find_leaf(graph, a, graph[a][(ai+1)%graph[a].size()].no);
				int f = find_leaf(graph, a, graph[a][(ai+2)%graph[a].size()].no);
				ans.push_back(make_pair(c, edge(e, val/2)));
				ans.push_back(make_pair(d, edge(f, val/2)));
				ans.push_back(make_pair(c, edge(d, -val/2)));
				ans.push_back(make_pair(e, edge(f, -val/2)));
			}
			graph[a][ai].we = 0;
			graph[b][bi].we = 0;
		}

		pive++;
		if (pive >= graph[pivv].size()) {
			pivv++;
			pive = 0;
		}
	}

	cout << "YES" << endl;
	cout << ans.size() << endl;
	for (auto& x : ans) {
		cout << x.first << ' ' << x.second.no << ' ' << x.second.we << '\n';
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 1005;

int n;
struct Edge { int to, val, cnt; };
vector<Edge> E[maxn];
int cnt_edge;
void insert(int a, int b, int val) {
    E[a].push_back({b, val, ++cnt_edge});
    E[b].push_back({a, val, cnt_edge});
}

void input() {
    scanf(" %d", &n);
    for (int i = 1; i < n; ++i){
        int a, b, val; scanf(" %d%d%d", &a, &b, &val);
        insert(a, b, val);
    }
    for (int i = 1; i <= n; ++i) if (E[i].size() == 2) {
        puts("NO");
        exit(0);
    }
    puts("YES");
}

int e_val[maxn];
int leaf[maxn];
Edge f[maxn];
void prework(int fa, int x) {
    leaf[x] = x;
    for (auto &e: E[x]) if (e.to != fa) {
        prework(x, e.to);
        leaf[x] = leaf[e.to];
    } else f[x] = e;
}

struct AnsTuple {
    int u, v, val;
};
vector<AnsTuple> ans;

void dfs(int fa, int x, int bro) {
    vector<Edge> son;
    for (auto e: E[x]) if (e.to != fa) son.push_back(e);
    for (int i = 0; i < son.size(); ++i) {
        int pre = (i - 1 + son.size()) % son.size(), cur = i, nxt = (i + 1) % son.size();
        int to_add = son[i].val - e_val[son[i].cnt];
        if (bro == 0) {
            ans.push_back({leaf[son[pre].to], leaf[son[nxt].to], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[nxt].to], to_add / 2});
        } else {
            ans.push_back({leaf[son[pre].to], leaf[bro], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[bro], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[bro], to_add / 2});
        }
        int pt = leaf[son[cur].to];
        while (pt != x) {
            e_val[f[pt].cnt] += to_add;
            pt = f[pt].to;
        }
        dfs(x, son[cur].to, son[nxt].to);
    }
}

int main() {
#ifdef __APPLE__
    freopen("main.in", "r", stdin);
#endif
    input();
    int root = 0;
    for (int i = 1; i <= n; ++i) if (E[i].size() > 2) {
        root = i; break;
    }
    prework(0, root);
    dfs(0, root, 0);
    printf("%lu\n", ans.size());
    for (auto &tp: ans) {
        printf("%d %d %d\n", tp.u, tp.v, tp.val);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 1005;

int n;
struct Edge { int to, val, cnt; };
vector<Edge> E[maxn];
int cnt_edge;
void insert(int a, int b, int val) {
    E[a].push_back({b, val, ++cnt_edge});
    E[b].push_back({a, val, cnt_edge});
}

void input() {
    scanf(" %d", &n);
    for (int i = 1; i < n; ++i){
        int a, b, val; scanf(" %d%d%d", &a, &b, &val);
        insert(a, b, val);
    }
    for (int i = 1; i <= n; ++i) if (E[i].size() == 2) {
        puts("NO");
        exit(0);
    }
    puts("YES");
}

int e_val[maxn];
int leaf[maxn];
Edge f[maxn];
void prework(int fa, int x) {
    leaf[x] = x;
    for (auto &e: E[x]) if (e.to != fa) {
        prework(x, e.to);
        leaf[x] = leaf[e.to];
    } else f[x] = e;
}

struct AnsTuple {
    int u, v, val;
};
vector<AnsTuple> ans;

void dfs(int fa, int x, int bro) {
    vector<Edge> son;
    for (auto e: E[x]) if (e.to != fa) son.push_back(e);
    for (int i = 0; i < son.size(); ++i) {
        int pre = (i - 1 + son.size()) % son.size(), cur = i, nxt = (i + 1) % son.size();
        int to_add = son[i].val - e_val[son[i].cnt];
        if (bro == 0) {
            ans.push_back({leaf[son[pre].to], leaf[son[nxt].to], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[nxt].to], to_add / 2});
        } else {
            ans.push_back({leaf[son[pre].to], leaf[bro], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[bro], to_add / 2});
        }
        int pt = leaf[son[cur].to];
        while (pt != x) {
            e_val[f[pt].cnt] += to_add;
            pt = f[pt].to;
        }
    }
    for (int i = 0; i < son.size(); ++i) {
        int nxt = (i + 1) % son.size();
        dfs(x, son[i].to, son[nxt].to);
    }
}

int main() {
#ifdef __APPLE__
    freopen("main.in", "r", stdin);
#endif
    input();
    int root = 0;
    for (int i = 1; i <= n; ++i) if (E[i].size() > 2) {
        root = i; break;
    }
    prework(0, root);
    dfs(0, root, 0);
    printf("%lu\n", ans.size());
    for (auto &tp: ans) {
        printf("%d %d %d\n", tp.u, tp.v, tp.val);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 1005;

int n;
struct Edge { int to, val, cnt; };
vector<Edge> E[maxn];
int cnt_edge;
void insert(int a, int b, int val) {
    E[a].push_back({b, val, ++cnt_edge});
    E[b].push_back({a, val, cnt_edge});
}

void input() {
    scanf(" %d", &n);
    for (int i = 1; i < n; ++i){
        int a, b, val; scanf(" %d%d%d", &a, &b, &val);
        insert(a, b, val);
    }
    for (int i = 1; i <= n; ++i) if (E[i].size() == 2) {
        puts("NO");
        exit(0);
    }
    puts("YES");
}

int e_val[maxn];
int leaf[maxn];
Edge f[maxn];
void prework(int fa, int x) {
    leaf[x] = x;
    for (auto &e: E[x]) if (e.to != fa) {
        prework(x, e.to);
        leaf[x] = leaf[e.to];
    } else f[x] = e;
}

struct AnsTuple {
    int u, v, val;
};
vector<AnsTuple> ans;

void dfs(int fa, int x, int bro) {
    if (fa == 0 && E[x].size() == 1) {
        ans.push_back({x, leaf[E[x].front().to], E[x].front().val});
        return;
    }
    vector<Edge> son;
    for (auto e: E[x]) if (e.to != fa) son.push_back(e);
    for (int i = 0; i < son.size(); ++i) {
        int pre = (i - 1 + son.size()) % son.size(), cur = i, nxt = (i + 1) % son.size();
        int to_add = son[i].val - e_val[son[i].cnt];
        if (bro == 0) {
            ans.push_back({leaf[son[pre].to], leaf[son[nxt].to], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[nxt].to], to_add / 2});
        } else {
            ans.push_back({leaf[son[pre].to], leaf[bro], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[bro], to_add / 2});
        }
        int pt = leaf[son[cur].to];
        while (pt != x) {
            e_val[f[pt].cnt] += to_add;
            pt = f[pt].to;
        }
    }
    for (int i = 0; i < son.size(); ++i) {
        int nxt = (i + 1) % son.size();
        dfs(x, son[i].to, son[nxt].to);
    }
}

int main() {
#ifdef __APPLE__
    freopen("main.in", "r", stdin);
#endif
    input();
    int root = 1;
    for (int i = 1; i <= n; ++i) if (E[i].size() > E[root].size()) root = i;
    prework(0, root);
    dfs(0, root, 0);
    printf("%lu\n", ans.size());
    for (auto &tp: ans) {
        printf("%d %d %d\n", tp.u, tp.v, tp.val);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 1005;

int n;
struct Edge { int to, val, cnt; };
vector<Edge> E[maxn];
int cnt_edge;
void insert(int a, int b, int val) {
    E[a].push_back({b, val, ++cnt_edge});
    E[b].push_back({a, val, cnt_edge});
}

void input() {
    scanf(" %d", &n);
    for (int i = 1; i < n; ++i){
        int a, b, val; scanf(" %d%d%d", &a, &b, &val);
        insert(a, b, val);
    }
    for (int i = 1; i <= n; ++i) if (E[i].size() == 2) {
        puts("NO");
        exit(0);
    }
    puts("YES");
}

int e_val[maxn];
int leaf[maxn];
Edge f[maxn];
void prework(int fa, int x) {
    leaf[x] = x;
    for (auto &e: E[x]) if (e.to != fa) {
        prework(x, e.to);
        leaf[x] = leaf[e.to];
    } else f[x] = e;
}

struct AnsTuple {
    int u, v, val;
};
vector<AnsTuple> ans;

void dfs(int fa, int x, int bro) {
    if (fa == 0 && E[x].size() == 1) {
        ans.push_back({x, leaf[E[x].front().to], E[x].front().val});
    }
    vector<Edge> son;
    for (auto e: E[x]) if (e.to != fa) son.push_back(e);
    for (int i = 0; i < son.size(); ++i) {
        int pre = (i - 1 + son.size()) % son.size(), cur = i, nxt = (i + 1) % son.size();
        int to_add = son[i].val - e_val[son[i].cnt];
        if (bro == 0) {
            ans.push_back({leaf[son[pre].to], leaf[son[nxt].to], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[nxt].to], to_add / 2});
        } else {
            ans.push_back({leaf[son[pre].to], leaf[bro], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[bro], to_add / 2});
        }
        int pt = leaf[son[cur].to];
        while (pt != x) {
            e_val[f[pt].cnt] += to_add;
            pt = f[pt].to;
        }
    }
    for (int i = 0; i < son.size(); ++i) {
        int nxt = (i + 1) % son.size();
        dfs(x, son[i].to, son[nxt].to);
    }
}

int main() {
#ifdef __APPLE__
    freopen("main.in", "r", stdin);
#endif
    input();
    int root = 1;
    prework(0, root);
    dfs(0, root, 0);
    printf("%lu\n", ans.size());
    for (auto &tp: ans) {
        printf("%d %d %d\n", tp.u, tp.v, tp.val);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 1005;

int n;
struct Edge { int to, val, cnt; };
vector<Edge> E[maxn];
int cnt_edge;
void insert(int a, int b, int val) {
    E[a].push_back({b, val, ++cnt_edge});
    E[b].push_back({a, val, cnt_edge});
}

void input() {
    scanf(" %d", &n);
    for (int i = 1; i < n; ++i){
        int a, b, val; scanf(" %d%d%d", &a, &b, &val);
        insert(a, b, val);
    }
    for (int i = 1; i <= n; ++i) if (E[i].size() == 2) {
        puts("NO");
        exit(0);
    }
    puts("YES");
}

int e_val[maxn];
int leaf[maxn];
Edge f[maxn];
void prework(int fa, int x) {
    leaf[x] = x;
    for (auto &e: E[x]) if (e.to != fa) {
        prework(x, e.to);
        leaf[x] = leaf[e.to];
    } else f[x] = e;
}

struct AnsTuple {
    int u, v, val;
};
vector<AnsTuple> ans;

void dfs(int fa, int x, int bro) {
    if (fa == 0 && E[x].size() == 1) {
        ans.push_back({x, E[x].front().to, E[x].front().val});
        return;
    }
    vector<Edge> son;
    for (auto e: E[x]) if (e.to != fa) son.push_back(e);
    for (int i = 0; i < son.size(); ++i) {
        int pre = (i - 1 + son.size()) % son.size(), cur = i, nxt = (i + 1) % son.size();
        int to_add = son[i].val - e_val[son[i].cnt];
        if (bro == 0) {
            ans.push_back({leaf[son[pre].to], leaf[son[nxt].to], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[nxt].to], to_add / 2});
        } else {
            ans.push_back({leaf[son[pre].to], leaf[bro], - to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[son[pre].to], to_add / 2});
            ans.push_back({leaf[son[cur].to], leaf[bro], to_add / 2});
        }
        int pt = leaf[son[cur].to];
        while (pt != x) {
            e_val[f[pt].cnt] += to_add;
            pt = f[pt].to;
        }
    }
    for (int i = 0; i < son.size(); ++i) {
        int nxt = (i + 1) % son.size();
        dfs(x, son[i].to, son[nxt].to);
    }
}

int main() {
#ifdef __APPLE__
    freopen("main.in", "r", stdin);
#endif
    input();
    int root = 1;
    prework(0, root);
    dfs(0, root, 0);
    printf("%lu\n", ans.size());
    for (auto &tp: ans) {
        printf("%d %d %d\n", tp.u, tp.v, tp.val);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const long long mod =1e9 ;
const int N = 3e3+5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i,l,m) for(int i=l;i<m;i++)
# define vii vector<int>
#define vpr vector<pair<int,int> >
ll pwr(ll x,ll y);
int a[N][3];
vector<bool> visited(N,0);
vector<pair<int,int> > adj[N];
void dfs(int u,int l,int* x,int* y)
{

    visited[u]=1;
    if(adj[u].size()==1){if(*x==0){*x=u;}else{*y=u;}}
    for(auto p : adj[u])
    {
        int v=p.ff;
        if(!visited[v] && v!=l)dfs(v,l,x,y);
    }
}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,p,q,wt;
cin>>n;
fr(i,1,n)
{
    cin>>p>>q>>wt;
    a[i][0]=p;
    a[i][1]=q;
    a[i][2]=wt;
    adj[p].pb(mp(q,wt));
    adj[q].pb(mp(p,wt));
}
fr(i,1,n+1)
{
    if(adj[i].size()==2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
}
int ans=(n-1)*4;
//int b[n+1][n+1][3];
cout<<"YES"<<endl;
cout<<ans<<endl;
fr(i,1,n)
{
  fr(j,0,n+1){visited[j]=0;}
  int x=0,y=0,u=0,v=0,l=0,m=0,wt;
  u=a[i][0];
  v=a[i][1];
  wt=a[i][2];
  /*if(adj[u].size()==1){x=u;}
  else{dfs(u,v,&x,&y);}
  if(adj[v].size()==1){l=u;}
  else{dfs(v,u,&l,&m);}
    */
    dfs(u,v,&x,&y);
 // dfs(u,v,&x,&y);
  dfs(v,u,&l,&m);
  //dfs(v,u,&l,&m);
  if(y==0 && m==0)
  {
      cout<<x<<' '<<l<<' '<<wt<<endl;
      cout<<x<<' '<<l<<' '<<0<<endl;
      cout<<x<<' '<<l<<' '<<0<<endl;
      cout<<x<<' '<<l<<' '<<0<<endl;

  }
  else if(y==0)
  {
      cout<<l<<' '<<x<<' '<<wt/2<<endl;
      cout<<m<<' '<<x<<' '<<wt/2<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
      cout<<l<<' '<<x<<' '<<0<<endl;
  }
  else if(m==0)
  {
       cout<<x<<' '<<l<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
      cout<<x<<' '<<l<<' '<<0<<endl;
  }
  else
  {
    cout<<x<<' '<<m<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
  }
 /* if(x!=0 && y!=0)
  {
      //cout<<"x y"<<endl;
      //cout<<x<<' '<<y<<endl;
      //b[i][0]=x;
      //b[i][1]=l;
      //b[i][2]=wt/2;
      cout<<x<<' '<<l<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
  }
  else
  {
      //cout<<l<<' '<<m<<endl;
       cout<<l<<' '<<x<<' '<<wt/2<<endl;
      cout<<m<<' '<<x<<' '<<wt/2<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
  }
*/
}
return 0;
}
ll pwr(ll x,ll y)
{
    ll ans=1;
    x=x%mod;
    while(y>0){
    if(y&1)ans=(x*(ans%mod))%mod;
    x=(x*x)%mod;
    y=y/2;
    }
    return ans%mod;
}


#include<bits/stdc++.h>
using namespace std;

const long long mod =1e9 ;
const int N = 3e3+5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i,l,m) for(int i=l;i<m;i++)
# define vii vector<int>
#define vpr vector<pair<int,int> >
ll pwr(ll x,ll y);
int a[N][3];
vector<bool> visited(N,0);
vector<pair<int,int> > adj[N];
void dfs(int u,int l,int* x,int* y)
{

    visited[u]=1;
    if(adj[u].size()==1){if(*x==0){*x=u;}else{*y=u;}}
    for(auto p : adj[u])
    {
        int v=p.ff;
        if(!visited[v] && v!=l)dfs(v,l,x,y);
    }
}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,p,q,wt;
cin>>n;
fr(i,1,n)
{
    cin>>p>>q>>wt;
    a[i][0]=p;
    a[i][1]=q;
    a[i][2]=wt;
    adj[p].pb(mp(q,wt));
    adj[q].pb(mp(p,wt));
}
fr(i,1,n+1)
{
    if(adj[i].size()==2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
}
int ans=(n-1)*4;
//int b[n+1][n+1][3];
cout<<"YES"<<endl;
cout<<ans<<endl;
fr(i,1,n)
{
  fr(j,0,n+1){visited[j]=0;}
  int x=0,y=0,u=0,v=0,l=0,m=0,wt;
  u=a[i][0];
  v=a[i][1];
  wt=a[i][2];
  /*if(adj[u].size()==1){x=u;}
  else{dfs(u,v,&x,&y);}
  if(adj[v].size()==1){l=u;}
  else{dfs(v,u,&l,&m);}
    */
    dfs(u,v,&x,&y);
 // dfs(u,v,&x,&y);
  dfs(v,u,&l,&m);
  //dfs(v,u,&l,&m);
  if(y==0)
  {
      cout<<l<<' '<<x<<' '<<wt/2<<endl;
      cout<<m<<' '<<x<<' '<<wt/2<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
      cout<<l<<' '<<x<<' '<<0<<endl;
  }
  else if(m==0)
  {
       cout<<x<<' '<<l<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
      cout<<x<<' '<<l<<' '<<0<<endl;
  }
  else
  {
    cout<<x<<' '<<m<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
  }
 /* if(x!=0 && y!=0)
  {
      //cout<<"x y"<<endl;
      //cout<<x<<' '<<y<<endl;
      //b[i][0]=x;
      //b[i][1]=l;
      //b[i][2]=wt/2;
      cout<<x<<' '<<l<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
  }
  else
  {
      //cout<<l<<' '<<m<<endl;
       cout<<l<<' '<<x<<' '<<wt/2<<endl;
      cout<<m<<' '<<x<<' '<<wt/2<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
  }
*/
}
return 0;
}
ll pwr(ll x,ll y)
{
    ll ans=1;
    x=x%mod;
    while(y>0){
    if(y&1)ans=(x*(ans%mod))%mod;
    x=(x*x)%mod;
    y=y/2;
    }
    return ans%mod;
}


#include<bits/stdc++.h>
using namespace std;

const long long mod =1e9 ;
const int N = 3e3+5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i,l,m) for(int i=l;i<m;i++)
# define vii vector<int>
#define vpr vector<pair<int,int> >
ll pwr(ll x,ll y);
int a[N][3];
vector<bool> visited(N,0);
vector<pair<int,int> > adj[N];
void dfs(int u,int l,int* x,int* y)
{

    visited[u]=1;
    if(adj[u].size()==1){if(*x==0){*x=u;}else{*y=u;}}
    for(auto p : adj[u])
    {
        int v=p.ff;
        if(!visited[v] && v!=l)dfs(v,l,x,y);
    }
}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,p,q,wt;
cin>>n;
fr(i,1,n)
{
    cin>>p>>q>>wt;
    a[i][0]=p;
    a[i][1]=q;
    a[i][2]=wt;
    adj[p].pb(mp(q,wt));
    adj[q].pb(mp(p,wt));
}
fr(i,1,n+1)
{
    if(adj[i].size()==2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
}
int ans=(n-1)*3;
//int b[n+1][n+1][3];
cout<<"YES"<<endl;
cout<<ans<<endl;
fr(i,1,n)
{
  fr(j,0,n+1){visited[j]=0;}
  int x=0,y=0,u=0,v=0,l=0,m=0,wt;
  u=a[i][0];
  v=a[i][1];
  wt=a[i][2];
  /*if(adj[u].size()==1){x=u;}
  else{dfs(u,v,&x,&y);}
  if(adj[v].size()==1){l=u;}
  else{dfs(v,u,&l,&m);}
    */
    dfs(u,v,&x,&y);
 // dfs(u,v,&x,&y);
  dfs(v,u,&l,&m);
  //dfs(v,u,&l,&m);
  if(x!=0 && y!=0)
  {
      //cout<<"x y"<<endl;
      //cout<<x<<' '<<y<<endl;
      //b[i][0]=x;
      //b[i][1]=l;
      //b[i][2]=wt/2;
      cout<<x<<' '<<l<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
  }
  else
  {
      //cout<<l<<' '<<m<<endl;
       cout<<l<<' '<<x<<' '<<wt/2<<endl;
      cout<<m<<' '<<x<<' '<<wt/2<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
  }

}
return 0;
}
ll pwr(ll x,ll y)
{
    ll ans=1;
    x=x%mod;
    while(y>0){
    if(y&1)ans=(x*(ans%mod))%mod;
    x=(x*x)%mod;
    y=y/2;
    }
    return ans%mod;
}


#include<bits/stdc++.h>
using namespace std;

const long long mod =1e9 ;
const int N = 3e3+5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i,l,m) for(int i=l;i<m;i++)
# define vii vector<int>
#define vpr vector<pair<int,int> >
ll pwr(ll x,ll y);
int a[N][3];
vector<bool> visited(N,0);
vector<pair<int,int> > adj[N];
void dfs(int u,int l,int* x,int* y)
{

    visited[u]=1;
    if(adj[u].size()==1){if(*x==0){*x=u;}else{*y=u;}}
    for(auto p : adj[u])
    {
        int v=p.ff;
        if(!visited[v] && v!=l)dfs(v,l,x,y);
    }
}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,p,q,wt;
cin>>n;
fr(i,1,n)
{
    cin>>p>>q>>wt;
    a[i][0]=p;
    a[i][1]=q;
    a[i][2]=wt;
    adj[p].pb(mp(q,wt));
    adj[q].pb(mp(p,wt));
}
fr(i,1,n+1)
{
    if(adj[i].size()==2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
}
int ans=(n-1)*3;
//int b[n+1][n+1][3];
cout<<ans<<endl;
fr(i,1,n)
{
  fr(j,0,n+1){visited[j]=0;}
  int x=0,y=0,u=0,v=0,l=0,m=0,wt;
  u=a[i][0];
  v=a[i][1];
  wt=a[i][2];
  /*if(adj[u].size()==1){x=u;}
  else{dfs(u,v,&x,&y);}
  if(adj[v].size()==1){l=u;}
  else{dfs(v,u,&l,&m);}
    */
    dfs(u,v,&x,&y);
 // dfs(u,v,&x,&y);
  dfs(v,u,&l,&m);
  //dfs(v,u,&l,&m);
  if(x!=0 && y!=0)
  {
      //cout<<"x y"<<endl;
      //cout<<x<<' '<<y<<endl;
      //b[i][0]=x;
      //b[i][1]=l;
      //b[i][2]=wt/2;
      cout<<x<<' '<<l<<' '<<wt/2<<endl;
      cout<<y<<' '<<l<<' '<<wt/2<<endl;
      cout<<x<<' '<<y<<' '<<(-1*wt/2)<<endl;
  }
  else
  {
      //cout<<l<<' '<<m<<endl;
       cout<<l<<' '<<x<<' '<<wt/2<<endl;
      cout<<m<<' '<<x<<' '<<wt/2<<endl;
      cout<<l<<' '<<m<<' '<<(-1*wt/2)<<endl;
  }

}
return 0;
}
ll pwr(ll x,ll y)
{
    ll ans=1;
    x=x%mod;
    while(y>0){
    if(y&1)ans=(x*(ans%mod))%mod;
    x=(x*x)%mod;
    y=y/2;
    }
    return ans%mod;
}


#include<bits/stdc++.h>
#define ll long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define repl(i,init,n) for(ll i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 600005
using namespace std;
const double PI = atan(1.0)*4;
const ll MOD=998244353;
struct triple{
  int x;
  int y;
  int w;
};
int leaf[1005]={0};
vector<int> v[1005];
bool checked[1005]={false};
int deg[1005]={0};
int weight[1005][1005];
int calc=0;
vector<triple> ans;
int find_leaves(int root)
{
    checked[root]=true;
    if(deg[root]==1)
    {
      leaf[root]=root;
      return root;
    }
    rep(i,0,v[root].size())
    {
      int vertex=v[root][i];
      if(!checked[vertex] && leaf[root]==0)
      {
         leaf[root]=find_leaves(vertex);
      }
      else if(!checked[vertex])
      {
         find_leaves(vertex);
      }
    }
    return leaf[root];
}
void solve(int root,int one_root)
{
   checked[root]=true;
   if(deg[root]==1) return;
   int roots[3];
   roots[0]=one_root;
   int cap=1;
   if(one_root==-1)
   cap=0;
   rep(i,0,v[root].size())
   {
       if(!checked[v[root][i]])
       {
         roots[cap]=leaf[v[root][i]];
         cap++;
         if(cap==3)
         break;
       }
   }
   rep(i,0,v[root].size())
   {
     int vertex=v[root][i];
     int x=-1,y=-1,cur=-1;
     if(!checked[vertex])
     {
         int req=weight[root][vertex];
         cur=leaf[vertex];
         int ind=-1;
         rep(j,0,3)
         {
           if(roots[j]==cur)
           {
            ind=j; break;
           }
         }
         if(ind!=-1) swap(roots[2],roots[ind]);
         x=roots[0];y=roots[1];
         ans.push_back({cur,x,req/2});
         ans.push_back({cur,y,req/2});
         ans.push_back({x,y,-req/2});
         solve(vertex,x);
     }
   }
}
int main()
{
    getFaster;
    int root=-1;
     int n;
     cin>>n;
     if(n==2)
     {
      int u,v1,w;
      cin>>u>>v1>>w;
      cout<<1<<endl;
      cout<<u<<" "<<v1<<" "<<w<<endl;
      return 0;
     }
     rep(i,1,n)
     {
       int u1,v1,w;
       cin>>u1>>v1>>w;
       v[u1].push_back(v1);
       v[v1].push_back(u1);
       weight[u1][v1]=weight[v1][u1]=w;
       deg[u1]++; deg[v1]++;
       if(deg[u1]>1 && root==-1) root=u1;
       if(deg[v1]>1 && root==-1) root=v1;
     }
     rep(i,1,n+1)
     {
       if(deg[i]==2)
       {
        cout<<"NO"<<endl;
        return 0;
       }
     }
     cout<<"YES"<<endl;
     find_leaves(root);
     rep(i,1,n+1) checked[i]=false;
     solve(root,-1);
     cout<<ans.size()<<endl;
     rep(i,0,ans.size())
     {
        printf("%d %d %d\n",ans[i].x,ans[i].y,ans[i].w);
     }
     return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define repl(i,init,n) for(ll i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 600005
using namespace std;
const double PI = atan(1.0)*4;
const ll MOD=998244353;
struct triple{
  int x;
  int y;
  int w;
};
int leaf[1005]={0};
vector<int> v[1005];
bool checked[1005]={false};
bool check[1005]={false};
int deg[1005]={0};
int par[1005]={0};
int weight[1005][1005];
int calc=0;
vector<triple> ans;
int find_leaves(int root)
{
    checked[root]=true;
    if(deg[root]==1)
    {
      leaf[root]=root;
      return root;
    }
    rep(i,0,v[root].size())
    {
      int vertex=v[root][i];
      if(!checked[vertex] && leaf[root]==0)
      {
         leaf[root]=find_leaves(vertex);
      }
      else if(!checked[vertex])
      {
         find_leaves(vertex);
      }
    }
    return leaf[root];
}
void root_the_tree(int root)
{
      check[root]=true;
      rep(i,0,v[root].size())
      {
         if(!check[v[root][i]])
         {
            par[v[root][i]]=root;
            root_the_tree(v[root][i]);
         }
      }
}
void update(int cur,int vertex,int w)
{
    int i=cur;
    while(i!=vertex)
    {
       int x=par[i];
       weight[i][x]-=w;
       weight[x][i]-=w;
       i=par[i];
    }
}
void solve(int root,int one_root)
{
   checked[root]=true;
   if(deg[root]==1) return;
   int roots[3];
   roots[0]=one_root;
   int cap=1;
   if(one_root==-1)
   cap=0;
   rep(i,0,v[root].size())
   {
       if(!checked[v[root][i]])
       {
         roots[cap]=leaf[v[root][i]];
         cap++;
         if(cap==3)
         break;
       }
   }
   rep(i,0,v[root].size())
   {
     int vertex=v[root][i];
     int x=-1,y=-1,cur=-1;
     if(!checked[vertex])
     {
         int req=weight[root][vertex];
         cur=leaf[vertex];
         int ind=-1;
         rep(j,0,3)
         {
           if(roots[j]==cur)
           {
            ind=j; break;
           }
         }
         if(ind!=-1) swap(roots[2],roots[ind]);
         x=roots[0];y=roots[1];
         ans.push_back({cur,x,req/2});
         ans.push_back({cur,y,req/2});
         ans.push_back({x,y,-req/2});
         update(cur,vertex,req);
         solve(vertex,x);
     }
   }
}
int main()
{
    getFaster;
    int root=-1;
     int n;
     cin>>n;
     if(n==2)
     {
      int u,v1,w;
      cin>>u>>v1>>w;
      cout<<1<<endl;
      cout<<u<<" "<<v1<<" "<<w<<endl;
      return 0;
     }
     rep(i,1,n)
     {
       int u1,v1,w;
       cin>>u1>>v1>>w;
       v[u1].push_back(v1);
       v[v1].push_back(u1);
       weight[u1][v1]=weight[v1][u1]=w;
       deg[u1]++; deg[v1]++;
       if(deg[u1]>1 && root==-1) root=u1;
       if(deg[v1]>1 && root==-1) root=v1;
     }
     rep(i,1,n+1)
     {
       if(deg[i]==2)
       {
        cout<<"NO"<<endl;
        return 0;
       }
     }
     cout<<"YES"<<endl;
     find_leaves(root);
     root_the_tree(root);
     rep(i,1,n+1) checked[i]=false;
     solve(root,-1);
     cout<<ans.size()<<endl;
     rep(i,0,ans.size())
     {
        printf("%d %d %d\n",ans[i].x,ans[i].y,ans[i].w);
     }
     return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define repl(i,init,n) for(ll i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 600005
using namespace std;
const double PI = atan(1.0)*4;
const ll MOD=998244353;
struct triple{
  int x;
  int y;
  int w;
};
int leaf[1005]={0};
vector<int> v[1005];
bool checked[1005]={false};
bool check[1005]={false};
int deg[1005]={0};
int par[1005]={0};
int weight[1005][1005];
int calc=0;
vector<triple> ans;
int find_leaves(int root)
{
    checked[root]=true;
    if(deg[root]==1)
    {
      leaf[root]=root;
      return root;
    }
    rep(i,0,v[root].size())
    {
      int vertex=v[root][i];
      if(!checked[vertex] && leaf[root]==0)
      {
         leaf[root]=find_leaves(vertex);
      }
      else if(!checked[vertex])
      {
         find_leaves(vertex);
      }
    }
    return leaf[root];
}
void root_the_tree(int root)
{
      check[root]=true;
      rep(i,0,v[root].size())
      {
         if(!check[v[root][i]])
         {
            par[v[root][i]]=root;
            root_the_tree(v[root][i]);
         }
      }
}
void update(int cur,int vertex,int w)
{
    int i=cur;
    while(i!=vertex)
    {
       int x=par[i];
       weight[i][x]-=w;
       weight[x][i]-=w;
       i=par[i];
    }
}
void solve(int root,int one_root)
{
   checked[root]=true;
   if(deg[root]==1) return;
   int roots[3];
   roots[0]=one_root;
   int cap=1;
   if(one_root==-1)
   cap=0;
   rep(i,0,v[root].size())
   {
       if(!checked[v[root][i]])
       {
         roots[cap]=leaf[v[root][i]];
         cap++;
         if(cap==3)
         break;
       }
   }
   rep(i,0,v[root].size())
   {
     int vertex=v[root][i];
     int x=-1,y=-1,cur=-1;
     if(!checked[vertex])
     {
         int req=weight[root][vertex];
         cur=leaf[vertex];
         int ind=-1;
         rep(j,0,3)
         {
           if(roots[j]==cur)
           {
            ind=j; break;
           }
         }
         if(ind!=-1) swap(roots[2],roots[ind]);
         x=roots[0];y=roots[1];
         ans.push_back({cur,x,req/2});
         ans.push_back({cur,y,req/2});
         ans.push_back({x,y,-req/2});
         update(cur,vertex,req);
         solve(vertex,x);
     }
   }
}
int main()
{
    getFaster;
    int root=-1;
     int n;
     cin>>n;
     if(n==2)
     {
      int u,v1,w;
      cin>>u>>v1>>w;
      cout<<"YES"<<endl;
      cout<<1<<endl;
      cout<<u<<" "<<v1<<" "<<w<<endl;
      return 0;
     }
     rep(i,1,n)
     {
       int u1,v1,w;
       cin>>u1>>v1>>w;
       v[u1].push_back(v1);
       v[v1].push_back(u1);
       weight[u1][v1]=weight[v1][u1]=w;
       deg[u1]++; deg[v1]++;
       if(deg[u1]>1 && root==-1) root=u1;
       if(deg[v1]>1 && root==-1) root=v1;
     }
     rep(i,1,n+1)
     {
       if(deg[i]==2)
       {
        cout<<"NO"<<endl;
        return 0;
       }
     }
     cout<<"YES"<<endl;
     find_leaves(root);
     root_the_tree(root);
     rep(i,1,n+1) checked[i]=false;
     solve(root,-1);
     cout<<ans.size()<<endl;
     rep(i,0,ans.size())
     {
        printf("%d %d %d\n",ans[i].x,ans[i].y,ans[i].w);
     }
     return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n, radfr, gr[1005], p[1005], h[1005];
vector<int> v[1005], fr[1005];
int nrans;
void dfs(int nod, int tata){
    p[nod]=tata;
    h[nod]=h[tata]+1;
    for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
    if(*it!=tata){
        dfs(*it, nod);
        for(auto it2=fr[*it].begin(); it2!=fr[*it].end(); ++it2)
            fr[nod].push_back(*it2);
    }
    if(fr[nod].size()==0) fr[nod].push_back(nod);//frunza
}
struct muchie{
    int x, y, ct;
}m[1005], ans[1005];
int rau;
void verif(int d, int s){
    if(s==d) rau=1;
    while(s!=p[d]){
        s=p[s];
        if(s==d) rau=1;
    }
}
void adauga_nod_frunza(int nod, int fru, int ct){
    ///cauti fr2  care nu se afla in subarborele lui nod (in O(n))
    int vec, fr2;
    for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
        if(*it!=p[nod]){
            rau=0; vec=*it;
            verif(vec, fru);
            if(rau==0) {fr2=fr[vec][0]; break;}
    }
    nrans++; ans[nrans]={radfr, fru, ct/2};
    nrans++; ans[nrans]={fru, fr2, ct/2};
    nrans++; ans[nrans]={radfr, fr2, -ct/2};
}
int main()
{
     int i, x, y, nods, nodj,ct, frunzarand;
     cin>>n;
     for(i=1; i<n; i++){
        cin>>x>>y>>ct; v[x].push_back(y); v[y].push_back(x); gr[x]++; gr[y]++;
        m[i].x=x; m[i].y=y; m[i].ct=ct;
     }
     for(i=1; i<=n; i++)
        if(gr[i]==2) {cout<<"NO"; return 0;}
     cout<<"YES\n";
     radfr=1;
     while(gr[radfr]!=1) radfr++;
     dfs(radfr, 0);
     for(i=1; i<n; i++){
        nods=m[i].x; nodj=m[i].y;
        if(h[nods]> h[nodj]) swap(nods, nodj);
        frunzarand=fr[nodj][0];
        if(frunzarand==nodj){
            adauga_nod_frunza(nods, frunzarand, m[i].ct);
        }
        else{
            adauga_nod_frunza(nods, frunzarand, m[i].ct);
            adauga_nod_frunza(nodj, frunzarand, -m[i].ct);
        }
     }
     cout<<nrans<<"\n";
     for(i=1; i<=nrans; i++) cout<<ans[i].x<<' '<<ans[i].y<<' '<<ans[i].ct<<"\n";
     return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n, radfr, gr[1005], p[1005], h[1005];
vector<int> v[1005], fr[1005];
int nrans;
void dfs(int nod, int tata){
    p[nod]=tata;
    h[nod]=h[tata]+1;
    for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
    if(*it!=tata){
        dfs(*it, nod);
        for(auto it2=fr[*it].begin(); it2!=fr[*it].end(); ++it2)
            fr[nod].push_back(*it2);
    }
    if(fr[nod].size()==0) fr[nod].push_back(nod);//frunza
}
struct muchie{
    int x, y, ct;
}m[1005], ans[1000005];
int rau;
void verif(int d, int s){
    if(s==d) rau=1;
    while(s!=p[d]){
        s=p[s];
        if(s==d) rau=1;
    }
}
void adauga_nod_frunza(int nod, int fru, int ct){
    ///cauti fr2  care nu se afla in subarborele lui nod (in O(n))
    int vec, fr2;
    for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
        if(*it!=p[nod]){
            rau=0; vec=*it;
            verif(vec, fru);
            if(rau==0) {fr2=fr[vec][0]; break;}
    }
    if(v[nod].size()==1) {
         nrans++; ans[nrans]={fru, nod, ct};
    }
    else{
    nrans++; ans[nrans]={radfr, fru, ct/2};
    nrans++; ans[nrans]={fru, fr2, ct/2};
    nrans++; ans[nrans]={radfr, fr2, -ct/2};
    }
}
int main()
{
     int i, x, y, nods, nodj,ct, frunzarand;
     cin>>n;
     for(i=1; i<n; i++){
        cin>>x>>y>>ct; v[x].push_back(y); v[y].push_back(x); gr[x]++; gr[y]++;
        m[i].x=x; m[i].y=y; m[i].ct=ct;
     }
     for(i=1; i<=n; i++)
        if(gr[i]==2) {cout<<"NO"; return 0;}
     cout<<"YES\n";
     radfr=1;
     while(gr[radfr]!=1) radfr++;
     dfs(radfr, 0);
     for(i=1; i<n; i++){
        nods=m[i].x; nodj=m[i].y;
        if(h[nods]> h[nodj]) swap(nods, nodj);
        frunzarand=fr[nodj][0];
        if(frunzarand==nodj){
            adauga_nod_frunza(nods, frunzarand, m[i].ct);
        }
        else{
            adauga_nod_frunza(nods, frunzarand, m[i].ct);
            adauga_nod_frunza(nodj, frunzarand, -m[i].ct);
        }
     }
     cout<<nrans<<"\n";
     for(i=1; i<=nrans; i++) cout<<ans[i].x<<' '<<ans[i].y<<' '<<ans[i].ct<<"\n";
     return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n, radfr, gr[1005], p[1005], h[1005];
vector<int> v[1005], fr[1005];
int nrans;
void dfs(int nod, int tata){
    p[nod]=tata;
    h[nod]=h[tata]+1;
    for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
    if(*it!=tata){
        dfs(*it, nod);
        for(auto it2=fr[*it].begin(); it2!=fr[*it].end(); ++it2)
            fr[nod].push_back(*it2);
    }
    if(fr[nod].size()==0) fr[nod].push_back(nod);//frunza
}
struct muchie{
    int x, y, ct;
}m[1005], ans[1005];
int rau;
void verif(int d, int s){
    if(s==d) rau=1;
    while(s!=p[d]){
        s=p[s];
        if(s==d) rau=1;
    }
}
void adauga_nod_frunza(int nod, int fru, int ct){
    ///cauti fr2  care nu se afla in subarborele lui nod (in O(n))
    int vec, fr2;
    for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
        if(*it!=p[nod]){
            rau=0; vec=*it;
            verif(vec, fru);
            if(rau==0) {fr2=fr[vec][0]; break;}
    }
    if(v[nod].size()==1) {
         nrans++; ans[nrans]={fru, nod, ct};
    }
    else{
    nrans++; ans[nrans]={radfr, fru, ct/2};
    nrans++; ans[nrans]={fru, fr2, ct/2};
    nrans++; ans[nrans]={radfr, fr2, -ct/2};
    }
}
int main()
{
     int i, x, y, nods, nodj,ct, frunzarand;
     cin>>n;
     for(i=1; i<n; i++){
        cin>>x>>y>>ct; v[x].push_back(y); v[y].push_back(x); gr[x]++; gr[y]++;
        m[i].x=x; m[i].y=y; m[i].ct=ct;
     }
     for(i=1; i<=n; i++)
        if(gr[i]==2) {cout<<"NO"; return 0;}
     cout<<"YES\n";
     radfr=1;
     while(gr[radfr]!=1) radfr++;
     dfs(radfr, 0);
     for(i=1; i<n; i++){
        nods=m[i].x; nodj=m[i].y;
        if(h[nods]> h[nodj]) swap(nods, nodj);
        frunzarand=fr[nodj][0];
        if(frunzarand==nodj){
            adauga_nod_frunza(nods, frunzarand, m[i].ct);
        }
        else{
            adauga_nod_frunza(nods, frunzarand, m[i].ct);
            adauga_nod_frunza(nodj, frunzarand, -m[i].ct);
        }
     }
     cout<<nrans<<"\n";
     for(i=1; i<=nrans; i++) cout<<ans[i].x<<' '<<ans[i].y<<' '<<ans[i].ct<<"\n";
     return 0;
}


#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long

#ifdef sarthak
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#else
#define trace(...)
#endif

#ifndef sarthak
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#define foreach(it,ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fort(i,v) for(auto i:v)
#define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar,val) rep(i,sizeof(ar)/sizeof(ll)) ar[i]=val
#define fill2(ar,val) rep(i,sizeof(ar)/sizeof(ar[0])) rep(j,sizeof(ar[0])/sizeof(ll)) ar[i][j]=val
#define fill0(ar) memset(ar,0,sizeof(ar))
#define debug(x) cout<<#x<<": "<<x<<endl

#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define sd2(x,y) scanf("%d %d",&x,&y)
#define slld2(x,y) scanf("%lld %lld",&x,&y)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pd2(x,y) printf("%d %d\n",x,y)
#define plld2(x,y) printf("%lld %lld\n",x,y)
#define all(ar) ar.begin(), ar.end()
#define endl '\n'
#define sz(x) (int)x.size()
#define len(x) (int)x.length()

typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ii> vii;
typedef vector<ll> vi;

#define INF 1000000000000000000
#define PI 3.14159265358979323846L

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngb(chrono::steady_clock::now().time_since_epoch().count()); // can give negative values too

vector<pair<ll, ll> > ed[1001];
ll leaf = 1;
vector<pair<ll, pair<ll, ll> > > ans;
ll p[1001];

ll findLeaf(ll src, ll par, ll val)
{
    if(ed[src].size()==1)
        return src;
    rep(i,ed[src].size())
    {
        if(ed[src][i].ff!=par)
        {
            ed[src][i].ss = ed[src][i].ss-val;
            return findLeaf(ed[src][i].ff,src,val);
        }
    }
    return 1;
}

void findParent(ll src, ll par)
{
    p[src] = par;
    fort(v,ed[src])
    {
        if(v.ff!=par)
            findParent(v.ff,src);
    }
}

void DFS(ll src, ll par)
{
    if(ed[src].size()==1)
    {
        if(ed[src][0].ff==par)
            return;
        ans.pb(mp(src,mp(findLeaf(ed[src][0].ff,src,ed[src][0].ss),ed[src][0].ss)));
        DFS(ed[src][0].ff,src);
    }
    else
    {
        fort(v,ed[src])
        {
            if(v.ff!=par)
            {
                ll l1;
                fort(x,ed[src])
                {
                    if(x.ff!=v.ff && x.ff!=par)
                    {
                        l1 = findLeaf(x.ff,src,0);
                        break;
                    }
                }
                ll todo = v.ss;
                ll l2 = findLeaf(v.ff,src,todo);

                ans.pb(mp(l1,mp(l2,todo/2)));
                ans.pb(mp(leaf,mp(l2,todo/2)));
                ans.pb(mp(l1,mp(leaf,-todo/2)));
                DFS(v.ff,src);
            }
        }
    }
}

void solve()
{
    ll n;
    cin >> n;

    rep(i,n-1)
    {
        ll u,v,val;
        cin >> u >> v >> val;

        ed[u].pb(mp(v,val));
        ed[v].pb(mp(u,val));
    }

    fr(i,1,n)
    {
        if(ed[i].size()==2)
        {
            cout << "NO" << endl;
            return;
        }
        if(ed[i].size()==1)
            leaf = i;
    }

    findParent(leaf,0);
    DFS(leaf,0);

    cout << "YES" << endl;
    cout << ans.size() << endl;
    rep(i,ans.size())
    {
        cout << ans[i].ff << " " << ans[i].ss.ff << " " << ans[i].ss.ss << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << fixed;
    cout << setprecision(10);
    #ifdef sarthak
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }

    #ifdef sarthak
    cerr << "Time elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
    #endif
}


#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long

#ifdef sarthak
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#else
#define trace(...)
#endif

#ifndef sarthak
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#define foreach(it,ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fort(i,v) for(auto i:v)
#define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar,val) rep(i,sizeof(ar)/sizeof(ll)) ar[i]=val
#define fill2(ar,val) rep(i,sizeof(ar)/sizeof(ar[0])) rep(j,sizeof(ar[0])/sizeof(ll)) ar[i][j]=val
#define fill0(ar) memset(ar,0,sizeof(ar))
#define debug(x) cout<<#x<<": "<<x<<endl

#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define sd2(x,y) scanf("%d %d",&x,&y)
#define slld2(x,y) scanf("%lld %lld",&x,&y)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pd2(x,y) printf("%d %d\n",x,y)
#define plld2(x,y) printf("%lld %lld\n",x,y)
#define all(ar) ar.begin(), ar.end()
#define endl '\n'
#define sz(x) (int)x.size()
#define len(x) (int)x.length()

typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ii> vii;
typedef vector<ll> vi;

#define INF 1000000000000000000
#define PI 3.14159265358979323846L

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngb(chrono::steady_clock::now().time_since_epoch().count()); // can give negative values too

vector<pair<ll, ll> > ed[1001];
ll leaf = 1;
vector<pair<ll, pair<ll, ll> > > ans;
ll p[1001];

ll findLeaf(ll src, ll par, ll val)
{
    if(ed[src].size()==1)
        return src;
    rep(i,ed[src].size())
    {
        if(ed[src][i].ff!=par)
        {
            ed[src][i].ss = ed[src][i].ss-val;
            return findLeaf(ed[src][i].ff,src,val);
        }
    }
    return 1;
}

void findParent(ll src, ll par)
{
    p[src] = par;
    fort(v,ed[src])
    {
        if(v.ff!=par)
            findParent(v.ff,src);
    }
}

void DFS(ll src, ll par)
{
    if(ed[src].size()==1)
    {
        if(ed[src][0].ff==par)
            return;
        ans.pb(mp(src,mp(findLeaf(ed[src][0].ff,src,ed[src][0].ss),ed[src][0].ss)));
        DFS(ed[src][0].ff,src);
    }
    else
    {
        fort(v,ed[src])
        {
            if(v.ff!=par)
            {
                ll l1;
                fort(x,ed[src])
                {
                    if(x.ff!=v.ff && x.ff!=par)
                    {
                        l1 = findLeaf(x.ff,src,0);
                        break;
                    }
                }
                ll todo = v.ss;
                ll l2 = findLeaf(v.ff,src,todo);

                ans.pb(mp(l1,mp(l2,todo/2)));
                ans.pb(mp(leaf,mp(l2,todo/2)));
                ans.pb(mp(l1,mp(leaf,-todo/2)));
                DFS(v.ff,src);
            }
        }
    }
}

void solve()
{
    ll n;
    cin >> n;

    rep(i,n-1)
    {
        ll u,v,val;
        cin >> u >> v >> val;

        ed[u].pb(mp(v,val));
        ed[v].pb(mp(u,val));
    }

    fr(i,1,n)
    {
        if(ed[i].size()==2)
        {
            cout << "NO" << endl;
            return;
        }
        if(ed[i].size()==1)
            leaf = i;
    }

    findParent(leaf,0);
    DFS(leaf,0);

    cout << ans.size() << endl;
    rep(i,ans.size())
    {
        cout << ans[i].ff << " " << ans[i].ss.ff << " " << ans[i].ss.ss << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << fixed;
    cout << setprecision(10);
    #ifdef sarthak
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }

    #ifdef sarthak
    cerr << "Time elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
    #endif
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
int rand_range(int a, int b)
{
	assert(a<=b);
	return a+r()%(b-a+1);
}
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = MIL+7;
const long long N = 1e3 + 10, M=1e3;
VI G[N];
VI val[N];
vector<pair<pi, int>> res;
set<int> lisc;
pi synowie[N], niesynowie[N];
bool vis[N];
int depth[N];

int forbidden(int x, int p, int zak){
    if(sz(G[x]) == 1)
        return x;
    for(int i:G[x]){
        if(i != p && i!= zak)
            return forbidden(i, x, zak);
    }
}

pi ask(int x, int kto){
    if(sz(G[x]) == 1)
        return pi(x, -1);
    pi res(-1, -1);
    for(int i:G[x]){
        if(i != kto) {
            res.sd = res.ft;
            res.ft = forbidden(i, x, x);
        }
    }
    return res;
}

pi dfs(int x){
    vis[x] = true;
    synowie[x] = pi(-1, -1);
    for(int i:G[x]){
        if(!vis[i]){
            depth[i] = depth[x] + 1;
            dfs(i);
            niesynowie[i] = ask(x, i);
            synowie[x].sd = synowie[x].ft;
            synowie[x].ft = synowie[i].ft;
        }
    }
    if(sz(G[x])==1) {
        synowie[x].ft = x;
    }
}

void solve(){
    int n;
    cin>>n;
    F(i,0,n-1){
        int a, b, c;
        cin>>a>>b>>c;
        G[a].eb(b);
        G[b].eb(a);
        val[a].eb(c);
        val[b].eb(c);
    }
    FOR(i,1,n){
        if(sz(G[i]) == 2){
            if(val[i][0] != val[i][1]){
                cout<<"NO\n";
                return;
            }
        }
        if(sz(G[i]) == 1){
            lisc.insert(i);
        }
    }
    dfs(1);
    if(sz(lisc) == 2){
        VI vsons(lisc.begin(), lisc.end());
        cout<<"1\n"<<vsons[0]<<" "<<vsons[1]<<" "<<val[1][0]<<"\n";
        return;
    }
    FOR(i,1,n){
        F(j,0,sz(G[i])){
            int a = i, b = G[i][j], v = val[i][j];
            //debug(a, b, depth[a], depth[b]);
            if(depth[b] < depth[a])
                continue;
            //cerr<<"weszlo\n";
            //debug(synowie[b].ft, synowie[b].sd, niesynowie[b].ft, niesynowie[b].sd);
            if(niesynowie[b].sd != -1 && synowie[b].sd != -1) {
                res.eb(pi(niesynowie[b].ft, synowie[b].ft), (v/2));
                res.eb(pi(niesynowie[b].sd, synowie[b].sd), (v/2));
                res.eb(pi(niesynowie[b].ft, niesynowie[b].sd), -(v/2));
                res.eb(pi(synowie[b].ft, synowie[b].sd), -(v/2));
                continue;
            }
            if(synowie[b].sd == -1 && synowie[b].ft == b) {
                res.eb(pi(niesynowie[b].ft, niesynowie[b].sd), -(v/2));
                res.eb(pi(synowie[b].ft, niesynowie[b].ft), (v/2));
                res.eb(pi(synowie[b].ft, niesynowie[b].sd), (v/2));
                continue;
            }
            if(niesynowie[b].sd == -1 && niesynowie[b].ft == a) {
                res.eb(pi(synowie[b].ft, synowie[b].sd), -(v/2));
                res.eb(pi(niesynowie[b].ft, synowie[b].ft), (v/2));
                res.eb(pi(niesynowie[b].sd, synowie[b].ft), (v/2));
            }
        }
    }
    cout<<"YES\n";
    cout<<sz(res)<<"\n";
    for(auto i:res)
        cout<<i.ft.ft<<" "<<i.ft.sd<<" "<<i.sd<<"\n";
}

int32_t main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);
	cerr.tie(0);
	cout<<setprecision(9)<<fixed;
	cerr<<setprecision(6)<<fixed;
	int test = 1, f;
    //cin>>test;
	F(_test, 0, test){
        //cout<<"Case #"<<_test + 1<<": ";
		solve();
//		if(_test == 1)
//            return 0;
	}
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
int rand_range(int a, int b)
{
	assert(a<=b);
	return a+r()%(b-a+1);
}
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = MIL+7;
const long long N = 1e3 + 10, M=1e3;
VI G[N];
VI val[N];
vector<pair<pi, int>> res;
set<int> sons[N], lisc;
bool vis[N];
int depth[N];

pi dfs(int x){
    vis[x] = true;
    for(int i:G[x]){
        if(!vis[i]){
            depth[i] = depth[x] + 1;
            dfs(i);
            for(auto j : sons[i])
                sons[x].insert(j);
        }
    }
    if(sz(G[x])==1)
        sons[x].insert(x);

}

void solve(){
    int n;
    cin>>n;
    F(i,0,n-1){
        int a, b, c;
        cin>>a>>b>>c;
        G[a].eb(b);
        G[b].eb(a);
        val[a].eb(c);
        val[b].eb(c);
    }
    FOR(i,1,n){
        if(sz(G[i]) == 2){
            if(val[i][0] != val[i][1]){
                cout<<"NO\n";
                return;
            }
        }
        if(sz(G[i]) == 1){
            lisc.insert(i);
        }
    }
    dfs(1);
    if(sz(lisc) == 2){
        VI vsons(lisc.begin(), lisc.end());
        cout<<"1\n"<<vsons[0]<<" "<<vsons[1]<<" "<<val[1][0]<<"\n";
        return;
    }
    FOR(i,1,n){
        F(j,0,sz(G[i])){
            int a = i, b = G[i][j], v = val[i][j];
            if(depth[b] < depth[a])
                continue;
            set<int> niesyn = lisc;
            for(int j:sons[b])
                niesyn.erase(j);
            VI vsons(sons[b].begin(), sons[b].end());
            VI vniesyn(niesyn.begin(), niesyn.end());
            if(sz(vniesyn) >= 2 && sz(vsons) >= 2){
                res.eb(pi(vniesyn[0], vniesyn[1]), -(v/2));
                res.eb(pi(vsons[0], vsons[1]), -(v/2));
                res.eb(pi(vsons[0], vniesyn[0]), (v/2));
                res.eb(pi(vsons[1], vniesyn[1]), (v/2));
                continue;
            } else if(sz(vniesyn) == 1 && vniesyn[0] == a) {
                res.eb(pi(vniesyn[0], vsons[0]), v/2);
                res.eb(pi(vniesyn[0], vsons[1]), v/2);
                res.eb(pi(vsons[0], vsons[1]), -(v/2));
            } else if(vsons[0] == b){
                res.eb(pi(vsons[0], vniesyn[0]), v/2);
                res.eb(pi(vsons[0], vniesyn[1]), v/2);
                res.eb(pi(vniesyn[0], vniesyn[1]), -(v/2));
            }
        }
    }
    cout<<"YES\n";
    cout<<sz(res)<<"\n";
    for(auto i:res)
        cout<<i.ft.ft<<" "<<i.ft.sd<<" "<<i.sd<<"\n";
}

int32_t main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);
	cerr.tie(0);
	cout<<setprecision(9)<<fixed;
	cerr<<setprecision(6)<<fixed;
	int test = 1, f;
    //cin>>test;
	F(_test, 0, test){
        //cout<<"Case #"<<_test + 1<<": ";
		solve();
//		if(_test == 1)
//            return 0;
	}
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
int rand_range(int a, int b)
{
	assert(a<=b);
	return a+r()%(b-a+1);
}
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = MIL+7;
const long long N = 1e3 + 10, M=1e3;
VI G[N];
VI val[N];
vector<pair<pi, int>> res;
set<int> lisc;
pi synowie[N], niesynowie[N];
bool vis[N];
int depth[N];

int forbidden(int x, int p, int zak){
    if(sz(G[x]) == 1)
        return x;
    for(int i:G[x]){
        if(i != p && i!= zak)
            return forbidden(i, x, zak);
    }
}

pi ask(int x, int kto){
    if(sz(G[x]) == 1)
        return pi(x, -1);
    pi res(-1, -1);
    for(int i:G[x]){
        if(i != kto) {
            res.sd = res.ft;
            res.ft = forbidden(i, x, x);
        }
    }
    return res;
}

pi dfs(int x){
    vis[x] = true;
    synowie[x] = pi(-1, -1);
    for(int i:G[x]){
        if(!vis[i]){
            depth[i] = depth[x] + 1;
            dfs(i);
            niesynowie[i] = ask(x, i);
            synowie[x].sd = synowie[x].ft;
            synowie[x].ft = synowie[i].ft;
        }
    }
    if(sz(G[x])==1) {
        synowie[x].ft = x;
    }
}

void solve(){
    int n;
    cin>>n;
    F(i,0,n-1){
        int a, b, c;
        cin>>a>>b>>c;
        G[a].eb(b);
        G[b].eb(a);
        val[a].eb(c);
        val[b].eb(c);
    }
    FOR(i,1,n){
        if(sz(G[i]) == 2){
            if(val[i][0] != val[i][1]){
                cout<<"NO\n";
                return;
            }
        }
        if(sz(G[i]) == 1){
            lisc.insert(i);
        }
    }
    dfs(1);
    if(sz(lisc) == 2){
        VI vsons(lisc.begin(), lisc.end());
        cout<<"1\n"<<vsons[0]<<" "<<vsons[1]<<" "<<val[1][0]<<"\n";
        return;
    }
    FOR(i,1,n){
        F(j,0,sz(G[i])){
            int a = i, b = G[i][j], v = val[i][j];
            //debug(a, b, depth[a], depth[b]);
            if(depth[b] < depth[a])
                continue;
            //cerr<<"weszlo\n";
            //debug(synowie[b].ft, synowie[b].sd, niesynowie[b].ft, niesynowie[b].sd);
            if(niesynowie[b].sd != -1 && synowie[b].sd != -1) {
                res.eb(pi(niesynowie[b].ft, synowie[b].ft), (v/2));
                res.eb(pi(niesynowie[b].sd, synowie[b].sd), (v/2));
                res.eb(pi(niesynowie[b].ft, niesynowie[b].sd), -(v/2));
                res.eb(pi(synowie[b].ft, synowie[b].sd), -(v/2));
                continue;
            }
            if(synowie[b].sd == -1) {
                res.eb(pi(niesynowie[b].ft, niesynowie[b].sd), -(v/2));
                res.eb(pi(b, niesynowie[b].ft), (v/2));
                res.eb(pi(b, niesynowie[b].sd), (v/2));
                continue;
            }
            if(niesynowie[b].sd == -1) {
                res.eb(pi(synowie[b].ft, synowie[b].sd), -(v/2));
                res.eb(pi(a, synowie[b].ft), (v/2));
                res.eb(pi(a, synowie[b].sd), (v/2));
            }
        }
    }
    cout<<"YES\n";
    cout<<sz(res)<<"\n";
    for(auto i:res)
        cout<<i.ft.ft<<" "<<i.ft.sd<<" "<<i.sd<<"\n";
}

int32_t main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);
	cerr.tie(0);
	cout<<setprecision(9)<<fixed;
	cerr<<setprecision(6)<<fixed;
	int test = 1, f;
    //cin>>test;
	F(_test, 0, test){
        //cout<<"Case #"<<_test + 1<<": ";
		solve();
//		if(_test == 1)
//            return 0;
	}
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
int rand_range(int a, int b)
{
	assert(a<=b);
	return a+r()%(b-a+1);
}
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = MIL+7;
const long long N = 1e3 + 10, M=1e3;
VI G[N];
VI val[N];
vector<pair<pi, int>> res;
set<int> lisc;
pi synowie[N], niesynowie[N];
bool vis[N];
int depth[N];

int forbidden(int x, int p, int zak){
    if(sz(G[x]) == 1)
        return x;
    for(int i:G[x]){
        if(i != p && i!= zak)
            return forbidden(i, x, zak);
    }
}

pi ask(int x, int kto){
    if(sz(G[x]) == 1)
        return pi(x, -1);
    pi res(-1, -1);
    for(int i:G[x]){
        if(i != kto) {
            res.sd = res.ft;
            res.ft = forbidden(i, x, x);
        }
    }
    return res;
}

pi dfs(int x){
    vis[x] = true;
    synowie[x] = pi(-1, -1);
    for(int i:G[x]){
        if(!vis[i]){
            depth[i] = depth[x] + 1;
            dfs(i);
            niesynowie[i] = ask(x, i);
            synowie[x].sd = synowie[x].ft;
            synowie[x].ft = synowie[i].ft;
        }
    }
    if(sz(G[x])==1) {
        synowie[x].ft = x;
    }
}

void solve(){
    int n;
    cin>>n;
    F(i,0,n-1){
        int a, b, c;
        cin>>a>>b>>c;
        G[a].eb(b);
        G[b].eb(a);
        val[a].eb(c);
        val[b].eb(c);
    }
    FOR(i,1,n){
        if(sz(G[i]) == 2){
            if(val[i][0] != val[i][1]){
                cout<<"NO\n";
                return;
            }
        }
        if(sz(G[i]) == 1){
            lisc.insert(i);
        }
    }
    dfs(1);
    if(sz(lisc) == 2){
        VI vsons(lisc.begin(), lisc.end());
        cout<<"YES\n";
        cout<<"1\n"<<vsons[0]<<" "<<vsons[1]<<" "<<val[1][0]<<"\n";
        return;
    }
    FOR(i,1,n){
        F(j,0,sz(G[i])){
            int a = i, b = G[i][j], v = val[i][j];
            debug(a, b, depth[a], depth[b]);
            if(depth[b] < depth[a])
                continue;
            cerr<<"weszlo\n";
            debug(synowie[b].ft, synowie[b].sd, niesynowie[b].ft, niesynowie[b].sd);
            if(niesynowie[b].sd != -1 && synowie[b].sd != -1) {
                res.eb(pi(niesynowie[b].ft, synowie[b].ft), (v/2));
                res.eb(pi(niesynowie[b].sd, synowie[b].sd), (v/2));
                res.eb(pi(niesynowie[b].ft, niesynowie[b].sd), -(v/2));
                res.eb(pi(synowie[b].ft, synowie[b].sd), -(v/2));
                continue;
            }
            if(synowie[b].sd == -1) {
                res.eb(pi(niesynowie[b].ft, niesynowie[b].sd), -(v/2));
                res.eb(pi(b, niesynowie[b].ft), (v/2));
                res.eb(pi(b, niesynowie[b].sd), (v/2));
                continue;
            }
            if(niesynowie[b].sd == -1) {
                res.eb(pi(synowie[b].ft, synowie[b].sd), -(v/2));
                res.eb(pi(a, synowie[b].ft), (v/2));
                res.eb(pi(a, synowie[b].sd), (v/2));
            }
        }
    }
    cout<<"YES\n";
    cout<<sz(res)<<"\n";
    for(auto i:res)
        cout<<i.ft.ft<<" "<<i.ft.sd<<" "<<i.sd<<"\n";
}

int32_t main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);
	cerr.tie(0);
	cout<<setprecision(9)<<fixed;
	cerr<<setprecision(6)<<fixed;
	int test = 1, f;
    //cin>>test;
	F(_test, 0, test){
        //cout<<"Case #"<<_test + 1<<": ";
		solve();
//		if(_test == 1)
//            return 0;
	}
}


#include<bits/stdc++.h>
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
const int maxn = 1010;
vector<pi> G[maxn], ans[maxn];
int V[maxn], cnt, L[maxn], R[maxn], sum;
void dfs(int u, int fa) {
    if (G[u].size() == 2) {
        puts("NO");
        exit(0);
    }
    L[u] = 1e9;
    for (auto tmp : G[u]) {
        if (tmp.first == fa)
            continue;
        dfs(tmp.first, u);
        L[u] = min(L[u], L[tmp.first]);
        R[u] = max(R[u], R[tmp.first]);
    }
    if (G[u].size() == 1)
        V[++cnt] = u, L[u] = R[u] = cnt;
}
void dfs2(int u, int fa) {
    for (auto tmp : G[u]) {
        int v = tmp.first;
        int w = tmp.second;
        if (v == fa)
            continue;
        int a = V[L[v]], b = V[R[v]], c = 0, d = 0;
        for (int j = L[v] - 1; j; j--)
        if (!c)
            c = V[j];
        else if (!d)
            d = V[j];
        else
            break;
        for (int j = R[v] + 1; j <= cnt; j++)
        if (!c)
            c = V[j];
        else if (!d)
            d = V[j];
        else
            break;
        //printf("u = %d v = %d a = %d b = %d c = %d d = %d\n", u, v, a, b, c, d);
        ans[a].pb(mk(c, w / 2));
        ans[b].pb(mk(d, w / 2));
        sum += 2;
        if (a != b)
            sum++, ans[a].pb(mk(b, -w / 2));
        if (c != d)
            sum++, ans[c].pb(mk(d, -w / 2));
        dfs2(v, u);
    }
}
int main() {
    int n, u, v, w;
    cin>>n;
    for (int i = 1; i < n; i++) {
        cin>>u>>v>>w;
        G[u].pb(mk(v, w));
        G[v].pb(mk(u, w));
    }
    dfs(1, 0);
    dfs2(1, 0);
    puts("YES");
    cout<<sum<<'\n';
    for (u = 1; u <= n; u++)
    for (auto tmp : ans[u]) {
        v = tmp.first;
        w = tmp.second;
        cout<<u<<' '<<v<<' '<<w<<'\n';
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
const int maxn = 1010;
vector<pi> G[maxn], ans[maxn];
int V[maxn], cnt, L[maxn], R[maxn], sum;
void dfs(int u, int fa) {
    if (G[u].size() == 2) {
        puts("NO");
        exit(0);
    }
    for (auto tmp : G[u]) {
        if (tmp.first == fa)
            continue;
        dfs(tmp.first, u);
        L[u] = min(L[u], L[tmp.first]);
        R[u] = max(R[u], R[tmp.first]);
    }
    if (G[u].size() == 1)
        V[++cnt] = u, L[u] = R[u] = cnt;
}
void dfs2(int u, int fa) {
    for (auto tmp : G[u]) {
        int v = tmp.first;
        int w = tmp.second;
        if (v == fa)
            continue;
        int a = V[L[v]], b = V[R[v]], c = 0, d = 0;
        for (int j = L[v] - 1; j; j--)
        if (!c)
            c = V[j];
        else if (!d)
            d = V[j];
        else
            break;
        for (int j = R[v] + 1; j <= cnt; j++)
        if (!c)
            c = V[j];
        else if (!d)
            d = V[j];
        else
            break;
        ans[a].pb(mk(c, w / 2));
        ans[b].pb(mk(d, w / 2));
        sum += 2;
        if (a != b)
            sum++, ans[a].pb(mk(b, -w / 2));
        if (c != d)
            sum++, ans[c].pb(mk(d, -w / 2));
        dfs2(v, u);
    }
}
int main() {
    int n, u, v, w;
    cin>>n;
    for (int i = 1; i < n; i++) {
        cin>>u>>v>>w;
        G[u].pb(mk(v, w));
        G[v].pb(mk(u, w));
    }
    dfs(1, 0);
    dfs2(1, 0);
    puts("YES");
    cout<<sum<<'\n';
    for (u = 1; u <= n; u++)
    for (auto tmp : ans[u]) {
        v = tmp.first;
        w = tmp.second;
        cout<<u<<' '<<v<<' '<<w<<'\n';
    }
}