aboutsummaryrefslogtreecommitdiff
path: root/2015/December/Platinum/maxflow.cpp
blob: 88f3cda51061ed5fe9bf1152c71ce5a239477169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <algorithm>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;

int N, ans = 0, pre[50005], d[50005] = { 0 }, L[20][50005] = { 0 };
vector<int> G[50005];

void dfs(int u, int p) {
	d[u] = d[p] + 1;
	L[0][u] = p;
	for (int i = 0; (1 << i) < N; i++) if (L[i][u]) L[i + 1][u] = L[i][L[i][u]];
	for (int v : G[u]) if (v != p) dfs(v, u);
}

int lca(int u, int v) {
	if (d[u] > d[v]) swap(u, v);
	for (int i = log2(N); i >= 0; i--) if (d[v] - (1 << i) >= d[u]) v = L[i][v];
	if (u == v) return u;
	for (int i = log2(N); i >= 0; i--) if (L[i][u] && L[i][u] != L[i][v]) u = L[i][u], v = L[i][v];
	return L[0][u];
}

void solve(int u, int p) {
	for (int v : G[u]) {
		if (v != p) {
			solve(v, u);
			pre[u] += pre[v];
		}
	}
	ans = max(pre[u], ans);
}

int main() {
	ifstream cin("maxflow.in");
	ofstream cout("maxflow.out");

	int K;
	cin >> N >> K;
	for (int i = 0; i < N - 1; i++) {
		int x, y;
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	dfs(1, 0);

	while (K--) {
		int s, t;
		cin >> s >> t;
		pre[s]++, pre[t]++;
		pre[L[0][lca(s, t)]]--, pre[lca(s, t)]--;
	}

	solve(1, 0);
	cout << ans << endl;
}