Language: C++

#include <bits/stdc++.h>
#define rint register int
#define fo(i, n) for(rint i = 1; i <= (n); i ++)
#define out(x) cerr << #x << " = " << x << "\n"
#define type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); ++ it)
using namespace std;
// by piano
template<typename tp> inline void read(tp &x) {
  x = 0;char c = getchar(); bool f = 0;
  for(; c < '0' || c > '9'; f |= (c == '-'), c = getchar());
  for(; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
  if(f) x = -x;
}

struct Edge {
	int nxt, to;
}e[M << 1];
int head[N], e_cnt = 1;
inline void add(int u, int v) {
	e[++ e_cnt] = (Edge) {head[u], v}; head[u] = e_cnt; ++ du[u]; ++ du[v];
}

inline void Add(int _s, int _t, int opt) {
	++ tot; s[tot] = _s; t[tot] = _t; rev[tot] = opt; 
	c[_s].push_back(tot); c[_t].push_back(tot);
}

inline void dfs(int u, int fat, int t) {
	if(u == t) return ;
	for(int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if(v != fat) ++ cnt[i >> 1], dfs(v, u, t);
	}
}

inline void doit(void) {
 	int u = 0, fat = 0;
	for(int i = 1; i <= n; i ++) if(du[i] == 1) {u = i; break;}
	for(int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to; if(!used[v]) {fat = v; -- du[fat]; break;}
	}
	used[u] = 1;
	int sz = (int) c[u].size();
	if(!u || !sz) return ;
	if(sz == 1) {
		int o = c[u][0];
		if(s[o] == u) Add(fat, t[o], 0);
		else if(t[o] == u) Add(s[o], fat, 0);
		int tp = tot;
		doit(); 
		if(rev[tp]) rev[o] = 1;		
	}
	else {
		int o = c[u][0];
		for(int i = 1; i < sz; i ++) {
			int p = c[u][i];
			int s1 = s[o], t1 = t[o];
			int s2 = s[p], t2 = t[p];
			if(t1 == u) swap(s1, t1), re ^= 1;
			if(t2 == u) swap(s2, t2), re ^= 1;
			
		}
	}
}

main(void) {
	read(n); read(m);
	for(int i = 1; i <= n - 1; i ++) {
		int x, y; read(x); read(y);
		add(x, y); add(y, x);
	}
	for(int i = 1; i <= m; i ++) {
		int s, t; read(s); read(t); Add(s, t, 0); dfs(s, 0, t);
	}
	int ans = 0;
	for(int i = 1; i <= n - 1; i ++) ans += min(cnt[i], 2);
	doit();
	cout << ans << "\n";
}

Compiler output:
a.cc:18:4: error: 'M' was not declared in this scope
   18 | }e[M << 1];
      |    ^
a.cc:19:10: error: 'N' was not declared in this scope
   19 | int head[N], e_cnt = 1;
      |          ^
a.cc: In function 'void add(int, int)':
a.cc:21:9: error: 'e' was not declared in this scope
   21 |         e[++ e_cnt] = (Edge) {head[u], v}; head[u] = e_cnt; ++ du[u]; ++ du[v];
      |         ^
a.cc:21:31: error: 'head' was not declared in this scope; did you mean 'read'?
   21 |         e[++ e_cnt] = (Edge) {head[u], v}; head[u] = e_cnt; ++ du[u]; ++ du[v];
      |                               ^~~~
      |                               read
a.cc:21:64: error: 'du' was not declared in this scope; did you mean 'u'?
   21 |         e[++ e_cnt] = (Edge) {head[u], v}; head[u] = e_cnt; ++ du[u]; ++ du[v];
      |                                                                ^~
      |                                                                u
a.cc: In function 'void Add(int, int, int)':
a.cc:25:12: error: 'tot' was not declared in this scope
   25 |         ++ tot; s[tot] = _s; t[tot] = _t; rev[tot] = opt;
      |            ^~~
a.cc:25:17: error: 's' was not declared in this scope; did you mean '_s'?
   25 |         ++ tot; s[tot] = _s; t[tot] = _t; rev[tot] = opt;
      |                 ^
      |                 _s
a.cc:25:30: error: 't' was not declared in this scope; did you mean '_t'?
   25 |         ++ tot; s[tot] = _s; t[tot] = _t; rev[tot] = opt;
      |                              ^
      |                              _t
a.cc:25:43: error: 'rev' was not declared in this scope
   25 |         ++ tot; s[tot] = _s; t[tot] = _t; rev[tot] = opt;
      |                                           ^~~
a.cc:26:9: error: 'c' was not declared in this scope
   26 |         c[_s].push_back(tot); c[_t].push_back(tot);
      |         ^
a.cc: In function 'void dfs(int, int, int)':
a.cc:31:21: error: 'head' was not declared in this scope; did you mean 'read'?
   31 |         for(int i = head[u]; i; i = e[i].nxt) {
      |                     ^~~~
      |                     read
a.cc:31:37: error: 'e' was not declared in this scope
   31 |         for(int i = head[u]; i; i = e[i].nxt) {
      |                                     ^
a.cc:33:33: error: 'cnt' was not declared in this scope; did you mean 'int'?
   33 |                 if(v != fat) ++ cnt[i >> 1], dfs(v, u, t);
      |                                 ^~~
      |                                 int
a.cc: In function 'void doit()':
a.cc:39:29: error: 'n' was not declared in this scope
   39 |         for(int i = 1; i <= n; i ++) if(du[i] == 1) {u = i; break;}
      |                             ^
a.cc:39:41: error: 'du' was not declared in this scope; did you mean 'u'?
   39 |         for(int i = 1; i <= n; i ++) if(du[i] == 1) {u = i; break;}
      |                                         ^~
      |                                         u
a.cc:40:21: error: 'head' was not declared in this scope; did you mean 'read'?
   40 |         for(int i = head[u]; i; i = e[i].nxt) {
      |                     ^~~~
      |                     read
a.cc:40:37: error: 'e' was not declared in this scope
   40 |         for(int i = head[u]; i; i = e[i].nxt) {
      |                                     ^
a.cc:41:38: error: 'used' was not declared in this scope
   41 |                 int v = e[i].to; if(!used[v]) {fat = v; -- du[fat]; break;}
      |                                      ^~~~
a.cc:41:60: error: 'du' was not declared in this scope; did you mean 'u'?
   41 |                 int v = e[i].to; if(!used[v]) {fat = v; -- du[fat]; break;}
      |                                                            ^~
      |                                                            u
a.cc:43:9: error: 'used' was not declared in this scope
   43 |         used[u] = 1;
      |         ^~~~
a.cc:44:24: error: 'c' was not declared in this scope
   44 |         int sz = (int) c[u].size();
      |                        ^
a.cc:48:20: error: 's' was not declared in this scope
   48 |                 if(s[o] == u) Add(fat, t[o], 0);
      |                    ^
a.cc:48:40: error: 't' was not declared in this scope
   48 |                 if(s[o] == u) Add(fat, t[o], 0);
      |                                        ^
a.cc:49:25: error: 't' was not declared in this scope
   49 |                 else if(t[o] == u) Add(s[o], fat, 0);
      |                         ^
a.cc:50:26: error: 'tot' was not declared in this scope
   50 |                 int tp = tot;
      |                          ^~~
a.cc:52:20: error: 'rev' was not declared in this scope
   52 |                 if(rev[tp]) rev[o] = 1;
      |                    ^~~
a.cc:58:34: error: 's' was not declared in this scope; did you mean 's1'?
   58 |                         int s1 = s[o], t1 = t[o];
      |                                  ^
      |                                  s1
a.cc:60:28: error: 't1' was not declared in this scope; did you mean 's1'?
   60 |                         if(t1 == u) swap(s1, t1), re ^= 1;
      |                            ^~
      |                            s1
a.cc:60:51: error: 're' was not declared in this scope
   60 |                         if(t1 == u) swap(s1, t1), re ^= 1;
      |                                                   ^~
a.cc:61:28: error: 't2' was not declared in this scope; did you mean 's2'?
   61 |                         if(t2 == u) swap(s2, t2), re ^= 1;
      |                            ^~
      |                            s2
a.cc:61:51: error: 're' was not declared in this scope
   61 |                         if(t2 == u) swap(s2, t2), re ^= 1;
      |                                                   ^~
a.cc: At global scope:
a.cc:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   67 | main(void) {
      | ^~~~
a.cc: In function 'int main()':
a.cc:68:14: error: 'n' was not declared in this scope; did you mean 'yn'?
   68 |         read(n); read(m);
      |              ^
      |              yn
a.cc:68:23: error: 'm' was not declared in this scope; did you mean 'tm'?
   68 |         read(n); read(m);
      |                       ^
      |                       tm
a.cc:77:53: error: 'cnt' was not declared in this scope; did you mean 'int'?
   77 |         for(int i = 1; i <= n - 1; i ++) ans += min(cnt[i], 2);
      |                                                     ^~~
      |                                                     int