#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long

#define db(args...) { \
     string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
     stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout << '\n';}
void err(istream_iterator<string> it){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << ' ';
    err(++it, args...);
}

void solve() {
    int n, q;
    cin >> n >> q;

    vector<vector<int>> e(n + 1, vector<int>());
    for (int i = 1, u, v; i < n; i++) {
        cin >> u >> v;
        e[u].push_back(v);
        e[v].push_back(u);
    }

    vector<int> dep(n + 1);

    const int M = 20;
    vector<vector<int>> f(M + 1, vector<int>(n + 1));

    auto dfs = [&] (auto &&self, int u, int fa)->void {
        for (auto v : e[u]) {
            if (v == fa) continue;
            f[0][v] = u;
            dep[v] = dep[u] + 1;
            self(self, v, u);
        }
    };
    dfs(dfs, 1, 0);
    for (int j = 1; j <= M; j++) {
        for (int x = 1; x <= n; x++) {
            int y = f[j - 1][x];
            f[j][x] = f[j - 1][y];
        }
    }

    auto jump = [&] (int x, int len) {
        for (int j = 0; j <= M; j++) {
            if ((len >> j) & 1) {
                x = f[j][x];
            }
        }
        return x;
    };

    auto lca = [&] (int x, int y) {
        if (dep[x] < dep[y]) swap(x, y);
        x = jump(x, dep[x] - dep[y]);
        assert(dep[x] == dep[y]);
        if (x == y) return x;
        for (int j = M; j >= 0; j--) {
            int nx = f[j][x], ny = f[j][y];
            if (nx == ny) continue;
            x = nx, y = ny;
        }
        return f[0][x];
    };

    vector<int> tag(n + 1);
    auto add = [&] (int x, int y) {
        int l = lca(x, y);
        tag[x]++, tag[y]++, tag[l]--, tag[f[0][l]]--;
    };

    while (q--) {
        int x, y;
        cin >> x >> y;
        add(x, y);
    }

    vector<int> sum(n + 1);
    auto dfs2 = [&] (auto &&self, int u, int fa)->void {
        for (auto v : e[u]) {
            if (v == fa) continue;
            self(self, v, u);
            sum[u] += sum[v];
        }
        sum[u] += tag[u];
    };
    dfs2(dfs2, 1, 0);
    cout << *max_element(sum.begin(), sum.end());
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

/*
g++ 1.cpp -Wall -std=c++20 -o 1 && 1 < in.txt > out.txt
g++ 1.cpp -Wall -std=c++20 -o 1 && 1
 */

0 条评论

目前还没有评论...

信息

ID
442
时间
ms
内存
MiB
难度
5
标签
递交数
22
已通过
13
上传者