Language: C++

int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
void jb(int x,int y){
	ne[++tot]=fi[x];
	fi[x]=tot;
	zz[tot]=y;
	flag[x][y]=1;
}
void dfs(int x,int y){
	fa[x]=y;in[x]=++l;
	for (int i=fi[x];i;i=ne[i])
		if (zz[i]!=y)dfs(zz[i],x);
	out[x]=++l;
}
int pd(int x,int y){
	return in[x]<=in[y]&&out[x]>=out[y];
}
void fg(int x,int y){
	ansx[++now]=x;ansy[now]=y;
	while (!pd(x,y)){
		ans+=flag[x][fa[x]];
		x=fa[x];
	}
	while (x!=y){
		ans+=flag[fa[y]][y];
		y=fa[y];
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		jb(x,y);jb(y,x);
	}
	dfs(1,0);
	for (int i=1;i<=m;i++){
		scanf("%d%d",&x,&y);
		int xx=x,yy=y,fflag=0;
		while (!pd(x,y)){
			if (flag[x][fa[x]]){
				fg(xx,yy);
				fflag=1;
				break;
			}
			else if (flag[fa[x]][x]){
				fg(yy,xx);
				fflag=1;
				break;
			}
			x=fa[x];
		}
		if (fflag)continue;
		while (y!=x){
			if (flag[y][fa[y]]){
				fg(yy,xx);
				break;
			}
			else if (flag[fa[y]][y]){
				fg(xx,yy);
				break;
			}
			y=fa[y];
		}
	}
	printf("%d\n",ans);
	for (int i=1;i<=now;i++)printf("%d %d\n",ansx[i],ansy[i]);
}


Compiler output:
a.cc:1:14: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |              ^
a.cc:1:22: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                      ^
a.cc:1:32: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                ^
a.cc:1:40: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                        ^
a.cc:1:46: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                              ^
a.cc:1:56: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                                        ^
a.cc:1:59: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                                           ^
a.cc:1:66: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                                                  ^
a.cc:1:74: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                                                          ^
a.cc:1:92: error: 'N' was not declared in this scope
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
      |                                                                                            ^
a.cc: In function 'void jb(int, int)':
a.cc:3:9: error: 'ne' was not declared in this scope; did you mean 'n'?
    3 |         ne[++tot]=fi[x];
      |         ^~
      |         n
a.cc:3:19: error: 'fi' was not declared in this scope
    3 |         ne[++tot]=fi[x];
      |                   ^~
a.cc:5:9: error: 'zz' was not declared in this scope
    5 |         zz[tot]=y;
      |         ^~
a.cc:6:9: error: 'flag' was not declared in this scope
    6 |         flag[x][y]=1;
      |         ^~~~
a.cc: In function 'void dfs(int, int)':
a.cc:9:9: error: 'fa' was not declared in this scope
    9 |         fa[x]=y;in[x]=++l;
      |         ^~
a.cc:9:17: error: 'in' was not declared in this scope; did you mean 'n'?
    9 |         fa[x]=y;in[x]=++l;
      |                 ^~
      |                 n
a.cc:10:20: error: 'fi' was not declared in this scope; did you mean 'i'?
   10 |         for (int i=fi[x];i;i=ne[i])
      |                    ^~
      |                    i
a.cc:10:30: error: 'ne' was not declared in this scope; did you mean 'n'?
   10 |         for (int i=fi[x];i;i=ne[i])
      |                              ^~
      |                              n
a.cc:11:21: error: 'zz' was not declared in this scope
   11 |                 if (zz[i]!=y)dfs(zz[i],x);
      |                     ^~
a.cc:12:9: error: 'out' was not declared in this scope
   12 |         out[x]=++l;
      |         ^~~
a.cc: In function 'int pd(int, int)':
a.cc:15:16: error: 'in' was not declared in this scope; did you mean 'n'?
   15 |         return in[x]<=in[y]&&out[x]>=out[y];
      |                ^~
      |                n
a.cc:15:30: error: 'out' was not declared in this scope
   15 |         return in[x]<=in[y]&&out[x]>=out[y];
      |                              ^~~
a.cc: In function 'void fg(int, int)':
a.cc:18:9: error: 'ansx' was not declared in this scope; did you mean 'ans'?
   18 |         ansx[++now]=x;ansy[now]=y;
      |         ^~~~
      |         ans
a.cc:18:23: error: 'ansy' was not declared in this scope; did you mean 'ans'?
   18 |         ansx[++now]=x;ansy[now]=y;
      |                       ^~~~
      |                       ans
a.cc:20:22: error: 'flag' was not declared in this scope
   20 |                 ans+=flag[x][fa[x]];
      |                      ^~~~
a.cc:20:30: error: 'fa' was not declared in this scope; did you mean 'fg'?
   20 |                 ans+=flag[x][fa[x]];
      |                              ^~
      |                              fg
a.cc:24:22: error: 'flag' was not declared in this scope
   24 |                 ans+=flag[fa[y]][y];
      |                      ^~~~
a.cc:24:27: error: 'fa' was not declared in this scope; did you mean 'fg'?
   24 |                 ans+=flag[fa[y]][y];
      |                           ^~
      |                           fg
a.cc: In function 'int main()':
a.cc:29:9: error: 'scanf' was not declared in this scope
   29 |         scanf("%d%d",&n,&m);
      |         ^~~~~
a.cc:39:29: error: 'flag' was not declared in this scope; did you mean 'fflag'?
   39 |                         if (flag[x][fa[x]]){
      |                             ^~~~
      |                             fflag
a.cc:39:37: error: 'fa' was not declared in this scope; did you mean 'fg'?
   39 |                         if (flag[x][fa[x]]){
      |                                     ^~
      |                                     fg
a.cc:49:27: error: 'fa' was not declared in this scope; did you mean 'fg'?
   49 |                         x=fa[x];
      |                           ^~
      |                           fg
a.cc:53:29: error: 'flag' was not declared in this scope; did you mean 'fflag'?
   53 |                         if (flag[y][fa[y]]){
      |                             ^~~~
      |                             fflag
a.cc:53:37: error: 'fa' was not declared in this scope; did you mean 'fg'?
   53 |                         if (flag[y][fa[y]]){
      |                                     ^~
      |                                     fg
a.cc:61:27: error: 'fa' was not declared in this scope; did you mean 'fg'?
   61 |                         y=fa[y];
      |                           ^~
      |                           fg
a.cc:64:9: error: 'printf' was not declared in this scope
   64 |         printf("%d\n",ans);
      |         ^~~~~~
a.cc:1:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | int now,ansx[N],ansy[N],tot,ne[N*2],fi[N],zz[N*2],flag[N][N],out[N],l,in[N],x,y,n,m,ans,fa[N];
a.cc:65:50: error: 'ansx' was not declared in this scope; did you mean 'ans'?
   65 |         for (int i=1;i<=now;i++)printf("%d %d\n",ansx[i],ansy[i]);
      |                                                  ^~~~
      |                                                  ans
a.cc:65:58: error: 'ansy' was not declared in this scope; did you mean 'ans'?
   65 |         for (int i=1;i<=now;i++)printf("%d %d\n",ansx[i],ansy[i]);
      |                                                          ^~~~
      |                                                          ans