# POJ3417 Network（LCA+树上差分）

Network

 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6047 Accepted: 1724

Description

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`

``````#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll maxv=1e5+5;

ll n,m,edgenum,ans;

struct node
{
ll to,next;
}edge[maxv<<1];

void init()
{
fill(d,d+maxv,0);
fill(ff,ff+maxv,0);
fill(val,val+maxv,0);
edgenum=0;
}

{
edge[edgenum].to=v;
}

void bfs()
{
queueq;
q.push(1);
d[1]=1;
while(!q.empty())
{
ll now=q.front();
q.pop();
{
ll y=edge[i].to;
if(d[y])
continue;
d[y]=d[now]+1;
fa[y][0]=now;
for(ll j=1;j<20;j++)
{
fa[y][j]=fa[fa[y][j-1]][j-1];
}
q.push(y);
}
}
}

ll lca(ll x,ll y)
{
if(d[x]>d[y])
swap(x,y);
for(ll i=19;i>=0;i--)
{
if(d[fa[y][i]]>=d[x])
y=fa[y][i];
}
if(x==y)
return x;
for(ll i=19;i>=0;i--)
{
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}

ll dfs(ll p,ll father)
{
ll sum=val[p];
{
ll now=edge[i].to;
if(now==father)
continue;
ll cnt=dfs(now,p);
sum+=cnt;
}
ff[p]=sum;
return sum;
}

int main()
{
while(scanf("%lld%lld",&n,&m)!=EOF)
{
init();
ll u,v;
for(ll i=1;i<=n-1;i++)
{
scanf("%lld%lld",&u,&v);
}
bfs();
for(int i=1;i<=m;i++)
{
ll a,b;
scanf("%lld%lld",&a,&b);
val[a]++;
val[b]++;
val[lca(a,b)]-=2;
}
dfs(1,-1);
ans=0;
for(ll i=2;i<=n;i++)
{
if(ff[i]==1)
{
ans++;
}
else if(ff[i]==0)
{
ans+=m;
}
}
printf("%lld\n",ans);
}
return 0;
}``````