Yixght is a manager of the company called SzqNetwork(SN). Now she’s very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN’s business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN’s network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.
As the DN’s best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.
Input
The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.
Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.
Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.
Output
Output a single integer — the number of ways to divide the network into at least two parts.
Sample Input
4 1
1 2
2 3
1 4
3 4
Sample Output
3
描述:问题是要求在树上去掉一条边,附加边也去掉一条边,有多少种。
思路:假设在建完树以后,我们在加一条边,那么必然形成环,假设只有这一条附加边,那么形成环的路径(在树上)都是可行的,假设又有一条附加边,如果在刚才的路径上有边又参与了一次形成了环,那么同时形成两次环的边,就是不能考虑的,因为即使去掉此边,还存在另一个环。所以这是一个路径覆盖的问题,之前我们在线性覆盖有过类似的覆盖问题,就是在
[a]
[
a
]
++,
[b+1]
[
b
+
1
]
—,类似的我们也可以在树上做差分。
假设附加边为
(x,y)
(
x
,
y
)
,那么
F[x]++,F[y]++,F(LCA(x,y))−=2
F
[
x
]
+
+
,
F
[
y
]
+
+
,
F
(
L
C
A
(
x
,
y
)
)
−
=
2
,然后做一次dfs即可.
然后累加答案。
代码
#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 100005
#define maxn 20
using namespace std;
int head[maxx],_next[maxx<<1],to[maxx<<1];
int cnt;
int n,m,N;
void addEdge(int x,int y)
{
to[++cnt]=y,_next[cnt]=head[x],head[x]=cnt;
to[++cnt]=x,_next[cnt]=head[y],head[y]=cnt;
}
int grand[maxx][maxn];
int depth[maxx];
int F[maxx];
void dfs(int root)
{
for(int i=1;i<=N;i++)
grand[root][i]=grand[grand[root][i-1]][i-1];
for(int i=head[root];i;i=_next[i])
{
int v=to[i];
if(v==grand[root][0])
continue;
grand[v][0]=root;
depth[v]=depth[root]+1;
dfs(v);
}
}
int lca(int a,int b)
{
if(depth[a]>depth[b])swap(a,b);
for(int i=N;i>=0;i--)
if(depth[a]<depth[b]&&depth[a]<=depth[grand[b][i]])
b=grand[b][i];
if(a==b)
return a;
for(int i=N;i>=0;i--)
{
if(grand[a][i]!=grand[b][i])
{
a=grand[a][i];
b=grand[b][i];
}
}
return grand[a][0];
}
void dfs2(int root)
{
for(int i=head[root];i;i=_next[i])
{
int v=to[i];
if(v==grand[root][0])
continue;
dfs2(v);
F[root]+=F[v];//累加覆盖数,类似于线性覆盖
}
}
void init()
{
memset(head,0,sizeof(head));
memset(F,0,sizeof(F));
N=floor(log2(n));
depth[1]=0;
cnt=0;
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
init();
int x,y;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
addEdge(x,y);
}
dfs(1);
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
F[x]++;
F[y]++;
F[lca(x,y)]-=2;
}
dfs2(1);
long long ans=0;
for(int i=2;i<=n;i++)
if(F[i]==0)
ans+=m;
else
if(F[i]==1)
ans++;
cout<<ans<<endl;
}
return 0;
}