Problem:
Score : 1500 points

Problem Statement

Takahashi loves walking on a tree.
The tree where Takahashi walks has N vertices numbered 1 through N.
The i-th of the N-1 edges connects Vertex a_i and Vertex b_i.

Takahashi has scheduled M walks. The i-th walk is done as follows:

The walk involves two vertices u_i and v_i that are fixed beforehand.

Takahashi will walk from u_i to v_i or from v_i to u_i along the shortest path.

The happiness gained from the i-th walk is defined as follows:

The happiness gained is the number of the edges traversed during the i-th walk that satisfies one of the following conditions:

In the previous walks, the edge has never been traversed.

In the previous walks, the edge has only been traversed in the direction opposite to the direction taken in the i-th walk.

Takahashi would like to decide the direction of each walk so that the total happiness gained from the M walks is maximized.
Find the maximum total happiness that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness.

Constraints

1 ≤ N,M ≤ 2000

1 ≤ a_i , b_i ≤ N

1 ≤ u_i , v_i ≤ N

a_i \neq b_i

u_i \neq v_i

The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{N-1} b_{N-1}
u_1 v_1
:
u_M v_M

Output

Print the maximum total happiness T that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness, in the following format:

T
u^'_1 v^'_1
:
u^'_M v^'_M

where (u^'_i,v^'_i) is either (u_i,v_i) or (v_i,u_i), which means that the i-th walk is from vertex u^'_i to v^'_i.

Sample Input 1

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

Sample Output 1

6
2 3
3 4
4 2

If we decide the directions of the walks as above, he gains the happiness of 2 in each walk, and the total happiness will be 6.

Sample Input 2

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

Sample Output 2

6
2 4
3 5
5 1

Sample Input 3

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

Sample Output 3

9
2 4
6 3
5 6
4 5

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lc (o<<1)
#define rc (o<<1|1)
#define mid ((l+r)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef double ld;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=2005;
int fa[N],d[N],pa[N],w[N],P[N],mark[N];
int u[N],v[N],len[N],W[N],n,m,a,b,ans,best;
vector<pii> id[N];
vi e[N];

void dfs(int u){
	REP(k,e[u].size()){
		int v=e[u][k];
		if(v!=fa[u]){
			fa[v]=u;
			d[v]=d[u]+1;
			dfs(v);
		}
	}
}

int lca(int a,int b){
	if(d[a]<d[b])swap(a,b);
	while(d[a]>d[b])a=fa[a];
	while(a!=b)a=fa[a],b=fa[b];
	return a;
}

int getpa(int x){
	if(pa[x]==x)return x;
	int p=getpa(pa[x]);
	return w[x]^=w[pa[x]],pa[x]=p;
}

void merge(int o,int t,int i){
	if(!mark[i]){
		REP(k,id[i].size())
			if(getpa(id[i][k].fi)!=getpa(o)){
				int a=id[i][k].fi,x=getpa(a);
				int b=o,y=getpa(b);
				w[y]=w[a]^w[b]^id[i][k].se^t^1;
				pa[y]=x;mark[i]=1;break;
			}
			else if(w[id[i][k].fi]^w[o]^id[i][k].se^t){
				mark[i]=1;break;
			}
	}
	id[i].pb(mp(o,t));
}

int main(){
	srand(19260817);
	read(n),read(m);
	rep(i,1,n-1){
		read(a),read(b);
		e[a].pb(b),e[b].pb(a);
	}
	d[1]=1,dfs(1);
	rep(i,1,m){
		read(u[i]),read(v[i]);
		len[i]=d[u[i]]+d[v[i]]-2*d[lca(u[i],v[i])];
		P[i]=i,pa[i]=i;
	}
	sort(P+1,P+m+1,[&](int a,int b){
		return len[a]>len[b];
	});
	for(;;){
		rep(i,1,m)pa[i]=i,w[i]=0;
		rep(i,1,n)mark[i]=0,id[i].clear();
		rep(i,1,m){
			int k=P[i],x=u[k],y=v[k];
			if(d[x]>d[y]){
				while(d[x]>d[y])
					merge(k,0,x),x=fa[x];
			}
			else{
				while(d[y]>d[x])
					merge(k,1,y),y=fa[y];
			}
			while(x!=y){
				merge(k,0,x),x=fa[x];
				merge(k,1,y),y=fa[y];
			}
		}
		ans=0;
		rep(i,2,n){
			int A=0,B=0;
			REP(k,id[i].size()){
				getpa(id[i][k].fi);
				A|=!(w[id[i][k].fi]^id[i][k].se),
				B|=w[id[i][k].fi]^id[i][k].se;
			}
			ans+=A+B;
		}
		if(ans>best){
			best=ans;
			memcpy(W,w,sizeof w);
		}
		random_shuffle(P+1,P+m+1);
		if(clock()>1.8*CLOCKS_PER_SEC)break;
	}
	printf("%d\n",best);
	return 0;
	rep(i,1,m){
		if(W[i])printf("%d %d\n",v[i],u[i]);
		else printf("%d %d\n",u[i],v[i]);
	}
	return 0;
}