Language: C++

#include <bits/stdc++.h>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 2e3 + 5;
const double eps = 1e-9;
using namespace std;
int dp[MAXN][2];
int par[MAXN][2];
vi adj[MAXN];
set<pii> vis[2], tmp[2];
int pa[20][MAXN], d[MAXN];
vii ans;
pii qry[MAXN];

void dfs(int u, int p)
{
    for (int v: adj[u])
        if (v != p)
        {
            pa[0][v] = u;
            d[v] = d[u] + 1;
            dfs(v, u);
        }
}

int lca(int u, int v)
{
    if (d[u] > d[v])
        swap(u, v);
    int di = d[v] - d[u];
    for (int i = 0; i < 17; i++)
        if ((di >> i) & 1)
            v = pa[i][v];
    if (v == u)
        return v;
    for (int i = 16; i >= 0; i--)
        if (pa[i][u] != pa[i][v])
            u = pa[i][u], v = pa[i][v];
    if (v == u)
        return v;
    return pa[0][v];
}

int main()
{
    int n, m;
    ni(n), ni(m);
    for (int i = 1; i < n; i++)
    {
        int u, v;
        ni(u), ni(v);
        u--, v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(0, -1);
    for (int i = 1; i < 17; i++)
        for (int j = 0; j < n; j++)
            pa[i][j] = pa[i - 1][pa[i - 1][j]];
    for (int i = 0; i < m; i++)
    {
        int u, v;
        ni(u), ni(v);
        qry[i] = {u, v};
        u--, v--;
        if (i == 0)
        {
            int l = lca(u, v);
            for (int j = u; j != l; j = pa[0][j])
                vis[0].insert({j, pa[0][j]}), vis[1].insert({pa[0][j], j}), dp[i][0]++, dp[i][1]++;
            for (int j = v; j != l; j = pa[0][j])
                vis[0].insert({pa[0][j], j}), vis[1].insert({j, pa[0][j]}), dp[i][0]++, dp[i][1]++;
        }
        else
        {
            int l = lca(u, v);
            int c0 = 0, c1 = 0, c2 = 0, c3 = 0;
            for (int j = u; j != l; j = pa[0][j])
            {
                if (vis[0].find({j, pa[0][j]}) == vis[0].end())
                    c0++;
                if (vis[0].find({pa[0][j], j}) == vis[0].end())
                    c1++;
                if (vis[1].find({j, pa[0][j]}) == vis[1].end())
                    c2++;
                if (vis[1].find({pa[0][j], j}) == vis[1].end())
                    c3++;
                tmp[0].insert({j, pa[0][j]}), tmp[1].insert({pa[0][j], j});
            }
            for (int j = v; j != l; j = pa[0][j])
            {
                if (vis[0].find({pa[0][j], j}) == vis[0].end())
                    c0++;
                if (vis[0].find({j, pa[0][j]}) == vis[0].end())
                    c1++;
                if (vis[1].find({pa[0][j], j}) == vis[1].end())
                    c2++;
                if (vis[1].find({j, pa[0][j]}) == vis[1].end())
                    c3++;
                tmp[0].insert({pa[0][j], j}), tmp[1].insert({j, pa[0][j]});
            }
            dp[i][0] = max(dp[i - 1][0] + c0, dp[i - 1][1] + c2);
            if (dp[i - 1][0] + c0 < dp[i - 1][1] + c2)
                par[i][0] = 1;
            else
                par[i][0] = 0;
            dp[i][1] = max(dp[i - 1][0] + c1, dp[i - 1][1] + c3);
            if (dp[i - 1][0] + c1 < dp[i - 1][1] + c3)
                par[i][1] = 1;
            else
                par[i][1] = 0;
            vis[0] = tmp[0];
            vis[1] = tmp[1];
            tmp[0].clear(), tmp[1].clear();
        }
    }
    pri(max(dp[m - 1][0], dp[m - 1][1]));
    int nx = 0;
    if (dp[m - 1][0] < dp[m - 1][1])
        swap(qry[m - 1].se, qry[m - 1].fi), nx = par[m - 1][1];
    else
        nx = par[m - 1][0];
    for (int i = m - 2; i >= 0; i--)
    {
        if (nx == 1)
            swap(qry[i].se, qry[i].fi)
        nx = par[i][nx];
    }
    for (int i = 0; i < m; i++)
        printf("%d %d\n", qry[i].fi, qry[i].se);
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:143:39: error: expected ';' before 'nx'
  143 |             swap(qry[i].se, qry[i].fi)
      |                                       ^
      |                                       ;
  144 |         nx = par[i][nx];
      |         ~~