Problem:
Score : 1400 points

Problem Statement

There is a tree with N vertices numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Initially, each edge is painted blue.
Takahashi will convert this blue tree into a red tree, by performing the following operation N-1 times:

Select a simple path that consists of only blue edges, and remove one of those edges.

Then, span a new red edge between the two endpoints of the selected path.

His objective is to obtain a tree that has a red edge connecting vertices c_i and d_i, for each i.

Determine whether this is achievable.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i,b_i,c_i,d_i ≤ N

a_i ≠ b_i

c_i ≠ d_i

Both input graphs are trees.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}

Output

Print YES if the objective is achievable; print NO otherwise.

Sample Input 1

3
1 2
2 3
1 3
3 2

Sample Output 1

YES

The objective is achievable, as below:

First, select the path connecting vertices 1 and 3, and remove a blue edge 1-2. Then, span a new red edge 1-3.

Next, select the path connecting vertices 2 and 3, and remove a blue edge 2-3. Then, span a new red edge 2-3.

Sample Input 2

5
1 2
2 3
3 4
4 5
3 4
2 4
1 4
1 5

Sample Output 2

YES

Sample Input 3

6
1 2
3 5
4 6
1 6
5 1
5 3
1 4
2 6
4 3
5 6

Sample Output 3

NO

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#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;

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];
queue<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(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].front()]) tr[k].pop();
	ret=tr[k].empty()?0:tr[k].front(); // 0 : don't know which edge
	if (l==r) return K=k,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,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,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;
	mn[K]=sz;
	while (K>>=1) pushup(K);
	erase(F[e],T[e]);
}


int main()
{
	file();
	int x,y;
	read(n);
	rep(i,1,n-1) read(x,y),make_edge(x,y);
	dfs1(1,0);dfs2(1,0,1);
	rep(i,1,n-1) read(x,y),add(x,y,i),F[i]=x,T[i]=y;
	rep(i,1,n-1) del_mn();
	puts("YES");
	return 0;
}