Solution
- Note \ (s → t \) comprising the point \ (U \) a path, apparently all \ (s → t \) can be composed of a communication block (because the path may be split into \ (s → u, u → t \) ), and this communication block of the number of sides is able to \ (u \) the number of cities to carry out trade activities.
- Denote communication block \ (G (u) \) , apparently \ (G (u) \) can be seen as: communication with all points \ (s, t \) and \ (U \) of the minimum spanning tree
Consider open \ (n-\) trees were maintained segment tree \ (G (U) \) , for \ (s → t \) for each point on the path \ (U \) , \ (G (U) \) are to add \ (s, t \) of these two points, to the segment tree \ (DFS \) ordinal index, nodes \ (X \) maintains the following information (corresponding to the interval \ ([L, R & lt] \) ) :
\ (F (X): \) \ (G (U) \) in \ (DFS \) sequence in \ ([l, r] \ ) points (referred to a collection of these points is \ (H (x) \) ), together with the root node, the number of blocks composed of sides communication
\ (S (X): \) \ (H \) in \ (DFS \) sequence smallest dot
\ (T (X): \) \ (H \) in \ (DFS \) sequence the point of maximumNote \ (X \) is the son of the left and right respectively \ (X2, X3 \) , then under normal circumstances :
\(f(x)=f(x2)+f(x3)-deep(lca(t(x2), t(x3))\)
Of course, there are some \ (H (x2) \) or \ (H (x3) \) is the empty set if necessary determination Laid
\(s(x)=s(x2)\)
\(t(x)=t(x3)\)- In particular implementation, the \ (s → t \) do tree differential, i.e., split into: in \ (s, t \) occurs at a frequency \ (. 1 + \) , \ (LCA (S, T), FA (lca (s, t)) \) occurs at a frequency \ (- 1 \) (the above are the number of occurrences of points \ (s, t \) is the number of occurrences), thus the leaf nodes of the tree also record for each segment point number appears
- Then offline down , \ (DFS \) whole tree again, when backtracking, the segment tree merge son to that point, and perform point located \ (+ 1, -1 \) modified
- Note \ (rt [u] \) of \ (U \) root corresponding to the segment tree, \ (G (U) \) number of sides, ie \ (f (rt [u] ) - deep (lca (s (rt [u]), t (rt [u]))) \)
- Note that there is no limit \ (U <v \) , that is, the answer is divided by \ (2 \)
Sequence requirements Euler \ (LCA \) can do \ (O (nlogn) \) time complexity
Code
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
template <class t>
inline void read(t & res)
{
char ch;
while (ch = getchar(), !isdigit(ch));
res = ch ^ 48;
while (ch = getchar(), isdigit(ch))
res = res * 10 + (ch ^ 48);
}
const int e = 2e5 + 5;
vector<int>g[e], h[e];
ll ans;
int n, m, rt[e], logn[e], st[e][18], dep[e], nxt[e * 2], go[e * 2], num, adj[e], fa[e];
int dfn1[e], dfn2[e], pool;
struct node
{
int l, r, cnt, f, s, t;
}c[e * 30];
inline void add(int x, int y)
{
nxt[++num] = adj[x];
adj[x] = num;
go[num] = y;
nxt[++num] = adj[y];
adj[y] = num;
go[num] = x;
}
inline void dfs1(int u, int pa)
{
dep[u] = dep[pa] + 1;
dfn1[u] = ++dfn1[0];
dfn2[u] = ++dfn2[0];
st[dfn1[0]][0] = u;
fa[u] = pa;
for (int i = adj[u]; i; i = nxt[i])
{
int v = go[i];
if (v == pa) continue;
dfs1(v, u);
st[++dfn1[0]][0] = u;
}
}
inline int lca(int x, int y)
{
if (!x || !y) return 0;
if (dfn1[x] > dfn1[y]) swap(x, y);
int l = dfn1[x], r = dfn1[y], k = logn[r - l + 1], u = st[l][k],
v = st[r - (1 << k) + 1][k];
return dep[u] < dep[v] ? u : v;
}
inline void collect(int x)
{
int l = c[x].l, r = c[x].r;
c[l].s ? c[x].s = c[l].s : c[x].s = c[r].s;
c[r].t ? c[x].t = c[r].t : c[x].t = c[l].t;
c[x].f = c[l].f + c[r].f - dep[lca(c[l].t, c[r].s)];
}
inline int merge(int x, int y, int l, int r)
{
if (!x || !y) return x ^ y;
if (l == r)
{
c[x].cnt += c[y].cnt;
c[x].f |= c[y].f;
c[x].s |= c[y].s;
c[x].t |= c[y].t;
return x;
}
int mid = l + r >> 1;
c[x].l = merge(c[x].l, c[y].l, l, mid);
c[x].r = merge(c[x].r, c[y].r, mid + 1, r);
collect(x);
return x;
}
inline void insert(int &x, int l, int r, int pos, int v)
{
if (!x) x = ++pool;
if (l == r)
{
c[x].cnt += v;
if (!c[x].cnt) c[x].f = c[x].s = c[x].t = 0;
else c[x].f = dep[pos], c[x].s = c[x].t = pos;
return;
}
int mid = l + r >> 1;
if (dfn2[pos] <= mid) insert(c[x].l, l, mid, pos, v);
else insert(c[x].r, mid + 1, r, pos, v);
collect(x);
}
inline void init()
{
int i, j;
logn[0] = -1;
for (i = 1; i <= dfn1[0]; i++) logn[i] = logn[i >> 1] + 1;
for (j = 1; (1 << j) <= dfn1[0]; j++)
for (i = 1; i + (1 << j) - 1 <= dfn1[0]; i++)
{
int u = st[i][j - 1], v = st[i + (1 << j - 1)][j - 1];
st[i][j] = (dep[u] < dep[v] ? u : v);
}
}
inline void dfs2(int u, int pa)
{
for (int i = adj[u]; i; i = nxt[i])
{
int v = go[i];
if (v == pa) continue;
dfs2(v, u);
rt[u] = merge(rt[u], rt[v], 1, n);
}
for (auto v : g[u]) insert(rt[u], 1, n, v, 1);
for (auto v : h[u]) insert(rt[u], 1, n, v, -1);
ans += c[rt[u]].f - dep[lca(c[rt[u]].s, c[rt[u]].t)];
}
int main()
{
int i, x, y;
read(n); read(m);
for (i = 1; i < n; i++) read(x), read(y), add(x, y);
dfs1(1, 0);
init();
while (m--)
{
read(x); read(y);
int z = lca(x, y);
g[x].pb(x); g[x].pb(y); g[y].pb(x); g[y].pb(y);
h[z].pb(x); h[z].pb(y); h[fa[z]].pb(x); h[fa[z]].pb(y);
}
dfs2(1, 0);
cout << ans / 2 << endl;
return 0;
}