A title intended to be understood that the n-1 sides of the primary tree and adds the m additional edges.
We can only remove a major edge, an additional edge, this edge is called the main edge, this edge is called the additional edge.
After the two sides called for the deletion, the tree is no longer in communication.
We need to count, how many kinds of programs can make none, output program number.
algorithm to resolve
additional edge in the end what is the use?
for each connection x, y node (x, y), in fact, we can consider this edge, connect the (x, y) which all points on the path.
When no major side when, in fact additional edge is our main side.
Therefore, additional edge (x, y), the tree is to x, every y on the path between the main side, covered a .
Because when any of the (x, y) path of a major edge disappear, he can become a major edge, to maintain connectivity.
So now we have a model into question.
Given edge of the n-1 a tree, a tree request for each side, the non-tree edge is covered many times
The main edge side of the tree is
a non-edge side of the tree is attached
so that the tree is a differential statistical frequency coverage problems.
On each additional path side, so that the (x, y) of nodes, each node weights the value +1 .
At this point we question becomes how statistics program.
We'll have a good talk about the classification of the main side, the body of the additional edge.
1. The main side is covered with a 0, that is, with only 0 additional edge.
We found that the main finished delete this side, arbitrarily delete an additional edge, we can make the tree is not connected. That is m kinds of programs.
Just delete (2,4) this red edge, then a random additional edge, we are to meet the conditions.
2. The main side cover 1, that is, only one additional top edge
We found that the main finished delete this side, we can delete this additional side major sides. That is 11 kinds of programs.
That is, we delete diagram above (3,7) red edge, then we can only remove the top piece of purple edges.
3. The main side cover is greater than 1, that a number of additional side above
We found that, how to delete, always connected. So 0 kinds of programs.
#include<bits/stdc++.h> using namespace std; const int N=500010; struct node { int n,v; } e[N*2]; int h[N],f[N][20],t,d[N],x,y,n,m,ans,date[N]; void add(int u,int v) { t++; e[t].v=v; e[t].n=h[u]; h[u]=t; } void dfs(int x,int fa) { d[x]=d[fa]+1; f[x][0]=fa; for (int i=1; (1<<i)<=d[x]; i++) { f[x][i]=f[f[x][i-1]][i-1]; } for (int i=h[x]; i; i=e[i].n) { if (e[i].v!=fa) { dfs(e[i].v,x); } } } int lca(int x,int y) { if (d[x] < d[y]) { swap(x, y); } int h = d[x] - d[y], k = 0; while (h) { if (h & 1) { x = f[x][k]; } h >>= 1; k++; } if (x == y) { return x; } for (int k = 19; k >= 0; k--) { if (f[x][k] != f[y][k]) { x = f[x][k]; y = f[y][k]; } } return f[x][0]; } void query(int u,int fa) { for (int i = h[u]; i; i = e[i].n) { int v = e[i].v; if (v == fa) continue; query(v, u); date[u] += date[v]; if (date[v] == 0) ans += m; else if (date[v] == 1) ans++; } } void update(int x,int y) { date[x]++; date[y]++; date[lca(x, y)] -= 2; } int main() { scanf("%d%d", &n, &m); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); add(u, v); add(v, u); } dfs(1, 0); for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); update(u, v); } query(1, 0); printf("%d\n",ans); }