Language: C++

#include<bits/stdc++.h>
namespace my_std{
    using namespace std;
    #define pii pair<int,int>
    #define fir first
    #define sec second
    #define MP make_pair
    #define rep(i,x,y) for (int i=(x);i<=(y);i++)
    #define drep(i,x,y) for (int i=(x);i>=(y);i--)
    #define go(x) for (int i=head[x];i;i=edge[i].nxt)
    #define sz 101010 
    typedef long long ll;
    template<typename T>
    inline void read(T& t)
    {
        t=0;char f=0,ch=getchar();
        double d=0.1;
        while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
        while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
        if(ch=='.')
        {
            ch=getchar();
            while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();
        }
        t=(f?-t:t);
    }
    template<typename T,typename... Args>
    inline void read(T& t,Args&... args){read(t); read(args...);}
//	void file()
//	{
//		#ifndef ONLINE_JUDGE
//		freopen("a.txt","r",stdin);
//		#endif
//	}
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
//	clock_t t=clock();

int n;
struct hh{int t,nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t)
{
    edge[++ecnt]=(hh){t,head[f]};
    head[f]=ecnt;
    edge[++ecnt]=(hh){f,head[t]};
    head[t]=ecnt;
}
int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
#define v edge[i].t
void dfs1(int x,int fa)
{
    dep[x]=dep[::fa[x]=fa]+1;size[x]=1;
    go(x) if (v!=fa)
    {
        dfs1(v,x);
        size[x]+=size[v];
        if (size[v]>size[son[x]]) son[x]=v;
    }
}
void dfs2(int x,int fa,int tp)
{
    top[x]=tp;dfn[x]=++cnt;
    if (son[x]) dfs2(son[x],x,tp);
    go(x) if (v!=fa&&v!=son[x]) dfs2(v,x,v);
}
#undef v

bool del[sz];
int F[sz],T[sz];
int mn[sz<<2],tag[sz<<2];
vector<int>tr[sz<<2];
#define ls k<<1
#define rs k<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
void Add(int k,int t){tag[k]+=t;mn[k]+=t;}
void pushdown(int k){int &t=tag[k]; if (!t) return; Add(ls,t); Add(rs,t); t=0; return;}
void pushup(int k){mn[k]=min(mn[ls],mn[rs]);}
void add(int k,int l,int r,int x,int y,int t)
{
    if (x<=l&&r<=y) return tr[k].push_back(t),Add(k,1);
    pushdown(k);
    int mid=(l+r)>>1;
    if (x<=mid) add(lson,x,y,t);
    if (y>mid) add(rson,x,y,t);
    pushup(k);
}
void change(int k,int l,int r,int x,int y,int s)
{
    if (x<=l&&r<=y) return Add(k,s);
    pushdown(k);
    int mid=(l+r)>>1;
    if (x<=mid) change(lson,x,y,s);
    if (y>mid) change(rson,x,y,s);
    pushup(k);
}
int K;
int getmn(int k,int l,int r)
{
    if (mn[k]>1) return -1; // -1 : don't have 1
    int ret;
    while (!tr[k].empty()&&del[tr[k].back()]) tr[k].pop_back();
    ret=tr[k].empty()?0:tr[k].back(); // 0 : don't know which edge
    if (l==r) return K=l,ret;
    pushdown(k);
    int mid=(l+r)>>1,cur;
    cur=getmn(lson); if (cur!=-1) { if (cur) return cur; if (ret) return ret; }
    cur=getmn(rson); if (cur!=-1) { if (cur) return cur; if (ret) return ret; }
    return ret;
}

void add(int x,int y,int t)
{
    while (top[x]!=top[y])
    {
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        add(1,1,n-1,dfn[top[x]],dfn[x],t);
        x=fa[top[x]];
    }
    if (dfn[x]>dfn[y]) swap(x,y);
    if (x!=y) add(1,1,n-1,dfn[x]+1,dfn[y],t);
}
void erase(int x,int y)
{
    while (top[x]!=top[y])
    {
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        change(1,1,n-1,dfn[top[x]],dfn[x],-1);
        x=fa[top[x]];
    }
    if (dfn[x]>dfn[y]) swap(x,y);
    if (x!=y) change(1,1,n-1,dfn[x]+1,dfn[y],-1);
}
void del_mn()
{
    if (mn[1]!=1) puts("NO"),exit(0);
    int e=getmn(1,1,n-1);
    del[e]=1;
    change(1,1,n-1,K,K,sz);
    erase(F[e],T[e]);
}


int main()
{
//	file();
    int x,y;
    scanf("%d",&n);
    rep(i,1,n-1) scanf("%d %d",&x,&y),make_edge(x,y);
    dfs1(1,0);dfs2(1,0,1);
    rep(i,1,n-1) scanf("%d %d",&x,&y),add(x,y,i),F[i]=x,T[i]=y;
    rep(i,1,n-1) del_mn();
    puts("YES");
//	cout<<1.0*(clock()-t)/CLOCKS_PER_SEC;
    return 0;
}

Compiler output:
a.cc: In function 'void dfs1(int, int)':
a.cc:54:30: error: reference to 'size' is ambiguous
   54 |     dep[x]=dep[::fa[x]=fa]+1;size[x]=1;
      |                              ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:50:5: note:                 'int size [101010]'
   50 | int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
      |     ^~~~
a.cc:58:9: error: reference to 'size' is ambiguous
   58 |         size[x]+=size[v];
      |         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:50:5: note:                 'int size [101010]'
   50 | int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
      |     ^~~~
a.cc:58:18: error: reference to 'size' is ambiguous
   58 |         size[x]+=size[v];
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:50:5: note:                 'int size [101010]'
   50 | int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
      |     ^~~~
a.cc:59:13: error: reference to 'size' is ambiguous
   59 |         if (size[v]>size[son[x]]) son[x]=v;
      |             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:50:5: note:                 'int size [101010]'
   50 | int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
      |     ^~~~
a.cc:59:21: error: reference to 'size' is ambiguous
   59 |         if (size[v]>size[son[x]]) son[x]=v;
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:50:5: note:                 'int size [101010]'
   50 | int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
      |     ^~~~