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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <bits/stdc++.h> #define int long long using namespace std;
const int N = 2e5 + 66;
inline int read() { int s(0), w(1); char ch = getchar(); while (ch < '0' || ch > '9') {if (ch == '-') w = -1; ch = getchar();} while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; }
inline void put(int x) { if (! x) putchar('0'); if (x < 0) putchar('-'), x = -x; int num(0); char c[66]; while (x) c[++ num] = x % 10 + 48, x /= 10; while (num) putchar(c[num --]); return (void)(putchar('\n')); }
int to[N << 1], nex[N << 1], head[N], cnt;
inline void add_edge(int x, int y) { to[++ cnt] = y; nex[cnt] = head[x]; return (void)(head[x] = cnt); }
int n, m, res; int g[N], dep[N], fa[N][66], vis[N];
inline void dfs(int x) { int i, y; for (i = 1; i <= 30; ++ i) { if (fa[x][i - 1]) fa[x][i] = fa[fa[x][i - 1]][i - 1]; else break; } for (i = head[x]; i; i = nex[i]) { y = to[i]; if (y == fa[x][0]) continue; fa[y][0] = x; dep[y] = dep[x] + 1; dfs(y); } }
inline int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); int delta = dep[x] - dep[y], i; for (i = 30; i >= 0; -- i) { if (delta & (1 << i)) x = fa[x][i]; } if (x == y) return x; for (i = 30; i >= 0; -- i) { if (fa[x][i] != fa[y][i]) { x = fa[x][i], y = fa[y][i]; } } return fa[x][0]; }
inline void dp(int x) { int i, y; vis[x] = 1; for (i = head[x]; i; i = nex[i]) { y = to[i]; if (! vis[y]) { dp(y); g[x] += g[y]; } } }
signed main() { int i, x, y; n = read(), m = read(); for (i = 1; i < n; ++ i) { x = read(), y = read(); add_edge(x, y), add_edge(y, x); } dfs(1); for (i = 1; i <= m; ++ i) { x = read(), y = read(); ++ g[x], ++ g[y], g[lca(x, y)] -= 2; } dp(1); for (i = 2; i <= n; ++ i) { if (! g[i]) res += m; else if (g[i] == 1) ++ res; } put(res); return 0; }
|