#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
    bool done_something = false;
    stringstream res;
    res << "[";
    for (; begin_iter != end_iter and counter; ++begin_iter) {
        done_something = true;
        counter--;
        res << *begin_iter << ", ";
    }
    string str = res.str();
    if (done_something) {
        str.pop_back();
        str.pop_back();
    }
    str += "]";
    return str;
}

vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
    vector<int> ord(size);
    for (int i = 0; i < size; i++) ord[i] = i;
    sort(ord.begin(), ord.end(), compare);
    return ord;
}

template <typename T>
bool MinPlace(T& a, const T& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool MaxPlace(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename S, typename T>
ostream& operator <<(ostream& out, const pair<S, T>& p) {
    out << "{" << p.first << ", " << p.second << "}";
    return out;
}

template <typename T>
ostream& operator <<(ostream& out, const vector<T>& v) {
    out << "[";
    for (int i = 0; i < (int)v.size(); i++) {
        out << v[i];
        if (i != (int)v.size()-1) out << ", ";
    }
    out << "]";
    return out;
}

#define INF 1<<30
#define eps 1e-9

#if DEBUG && !ONLINE_JUDGE
    ifstream input_from_file("input.txt");
    #define cin input_from_file

    #define dbg_var(x) clog << #x  << ": " << x << endl;
    #define dbg_array(x, len) clog << #x << ": " << print_iterable(x, x+len, -1) << endl;
#else
    #define dbg_var(x) 
    #define dbg_vec(x)
    #define dbg_array(x, len)
#endif

///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////

class Dsu {
  public:
    void Join(int a, int b) {
        if (!zero_based) {
            a--;
            b--;
        }
        a = WhichGroup_(a);
        b = WhichGroup_(b);
        if (a == b) {
            // Already in the same group
            return;
        }
        if (sizes[a] > sizes[b]) {
            // After this we can assume sizes[a] <= sizes[b]
            swap(a, b);
        }
        // Joining a in b
        sizes[b] += sizes[a];
        ancestor[a] = b;
        groups_number--;
    }

    bool SameGroup(int a, int b) const {
        if (!zero_based) {
            a--;
            b--;
        }
        return WhichGroup_(a) == WhichGroup_(b);
    }

    // The function WhichGroup most likely should not be among the public API.
    // It is not really meaningful and is implementation-dependent.
    // At the same time, if someone wants to modify the algorithm, it might be
    // handy.
    int WhichGroup(int a) const {
        if (!zero_based) a--;
        return WhichGroup_(a) + (!zero_based);
    }

    int GroupSize(int a) const {
        if (!zero_based) a--;
        return sizes[WhichGroup_(a)];
    }

    int GroupsNumber() const {
        return groups_number;
    }

    Dsu(int set_size, bool zero_based = true) :
        ancestor(set_size),
        sizes(set_size),
        zero_based(zero_based),
        groups_number(set_size) {
        for (int i = 0; i < set_size; i++) {
            ancestor[i] = i;
            sizes[i] = 1;
        }
    }
  private:
    mutable vector<int> ancestor; // This is not always the root of the respective tree.
    vector<int> sizes; // This is correct only for indexes that are roots.
    bool zero_based;
    int groups_number;

    int WhichGroup_(int a) const {
        if (ancestor[a] != a) {
            ancestor[a] = WhichGroup_(ancestor[a]);
        }
        return ancestor[a];
    }
};

// Start 23.18
// Versione 0: 23.34
// Accepted: 23.44 (esattamente la versione 0)

int main() {
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    Dsu dsu(N);
    vector<int> vertex(N);
    for (int i = 0; i < N; i++) vertex[i] = i;
    queue<pair<int,int>> joinable;
    vector<set<int>> a1(N), a2(N);
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        a1[a].insert(b);
        a1[b].insert(a);
    }
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        a2[a].insert(b);
        a2[b].insert(a);
        if (a1[a].count(b)) joinable.push({a, b});
    }
    while (!joinable.empty()) {
        auto p = joinable.front();
        joinable.pop();
        int a = p.first;
        int b = p.second;
        if (dsu.SameGroup(a, b)) continue;
        a = dsu.WhichGroup(a);
        b = dsu.WhichGroup(b);
        dsu.Join(a, b);
        int boss = dsu.WhichGroup(a);
        if (boss != a) swap(a, b);
        a1[a].erase(b);
        a2[a].erase(b);
        a1[b].erase(a);
        a2[b].erase(a);
        
        for (int v : a1[b]) {
            a1[v].erase(b);
            if (dsu.SameGroup(a, v)) continue;
            a1[a].insert(v);
            a1[v].insert(a);
            if (a2[a].count(v)) joinable.push({a, v});
        }
        a1[b].clear();
        for (int v : a2[b]) {
            a2[v].erase(b);
            if (dsu.SameGroup(a, v)) continue;
            a2[a].insert(v);
            a2[v].insert(a);
            if (a1[a].count(v)) joinable.push({a, v});
        }
        a2[b].clear();
    }
    cout << ((dsu.GroupsNumber() == 1) ? "YES" : "NO") << "\n";
}

Measured memory_bytes: 29824