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: Wrong Answer)

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=200005,inf=1047483647;
struct node { int y,next; } a[N<<1];
struct node1 { int x,y,z; } b[N];
struct node2 { int z,b; } t[N<<2];
int s[N],C[N],top[N],dfn[N],h[N],fa[N],head[N],i,j,k,n,m,T,l,r,x,y,z;
char c;

inline char getc()
{
	#define VV 10000000
	static char s[VV],*l=s,*r=s;
	if (l==r)
		l=s,r=s+fread(s,1,VV,stdin);
	return l==r?0:*l++;
}

int read(){ int z=0; do c=getc(); while (c<'0'||c>'9'); while (c>='0'&&c<='9') z*=10,z+=c-'0',c=getc(); return z; }

bool cmp(node1 a,node1 b){ return a.z<b.z; }

void jb(int x,int y)
{
	a[++k]=(node){y,head[x]},head[x]=k,
	a[++k]=(node){x,head[y]},head[y]=k;
}

void dfs1(int w)
{
	s[w]=1,h[w]=h[fa[w]]+1;
	for (register int j=head[w];j;j=a[j].next)
	{
		int c=a[j].y;
		if (c!=fa[w])
		{
			fa[c]=w,dfs1(c),s[w]+=s[c];
			if (s[c]>s[C[w]]) C[w]=c;
		}
	}
}

void dfs2(int w)
{
	dfn[w]=++T;
	if (C[w])
		top[C[w]]=top[w],dfs2(C[w]);
	for (register int j=head[w];j;j=a[j].next)
	{
		int c=a[j].y;
		if (c!=fa[w]&&c!=C[w])
			top[c]=c,dfs2(c);
	}
}

void push(int w,int z)
{
	t[w].z+=z,t[w].b+=z;
}

void pushdown(int w)
{
	int &z=t[w].b;
	if (z)
		push(w<<1,z),push(w<<1|1,z),z=0;
}

void update(int w)
{
	t[w].z=min(t[w<<1].z,t[w<<1|1].z);
}

void motree(int w,int L,int R)
{
	if (l<=L&&r>=R)
	{
		push(w,z);
		return;
	}
	pushdown(w);
	int mid=L+R>>1;
	if (l<=mid)
		motree(w<<1,L,mid);
	if (r>mid)
		motree(w<<1|1,mid+1,R);
	update(w);
}

int qtree(int w,int L,int R)
{
	if (l<=L&&r>=R) return t[w].z;
	pushdown(w);
	int mid=L+R>>1;
	return min(l<=mid?qtree(w<<1,L,mid):inf,r>mid?qtree(w<<1|1,mid+1,R):inf);
}

int getlca(int x,int y)
{
	while (top[x]!=top[y])
	{
		if (h[top[x]]<h[top[y]])
			swap(x,y);
		x=fa[top[x]];
	}
	return h[x]<h[y]?x:y;
}

void mt(int x,int y,int D)
{
	z=D;
	while (top[x]!=top[y])
	{
		if (h[top[x]]<h[top[y]])
			swap(x,y);
		l=dfn[top[x]],r=dfn[x],motree(1,1,n),
		x=fa[top[x]];
	}
	if (x==y) return;
	if (h[x]>h[y]) swap(x,y);
	l=dfn[x]+1,y=dfn[y],motree(1,1,n);
}

bool qt(int x,int y)
{
	while (top[x]!=top[y])
	{
		if (h[top[x]]<h[top[y]])
			swap(x,y);
		l=dfn[top[x]],r=dfn[x];
		if (!qtree(1,1,n)) return 0;
		x=fa[top[x]];
	}
	if (x==y) return 1;
	if (h[x]>h[y]) swap(x,y);
	l=dfn[x]+1,y=dfn[y];
	if (qtree(1,1,n)) return 0;
	return 1;
}

bool solve()
{
	for (i=1;i<n;++i)
	{
		x=b[i].x,y=b[i].y;
		mt(x,y,-1);
		if (qt(x,y))
			return 0;
	}
	return 1;
}

int main()
{
	//freopen("1.in","r",stdin);
	n=read(),k=1;
	for (i=1;i<n;++i) jb(read(),read());
	k=1;
	dfs1(1),top[1]=1,dfs2(1);
	//for (i=1;i<=n;++i) printf("%d %d %d %d %d %d\n",i,fa[i],s[i],h[i],top[i],C[i]);
	for (i=1;i<n;++i)
		x=read(),y=read(),b[i]=(node1){x,y,h[x]+h[y]-(h[getlca(x,y)]<<1)},mt(x,y,1);
	sort(b+1,b+n,cmp);
	puts(solve()?"YES":"NO");
	return 0;
}