Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4929   Accepted: 1413

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 buildsM 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 theM 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 nodea and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between nodea 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

【题意】给了一颗树和一些新的边的关系,要你毁掉一条树边和一条新的边,问你有多少种方法使得树不连通。

【解题方法】

我们知道加入新边必定会成环,可以发现
对于被环覆盖2次及以上的树边,贡献是0
对于被环覆盖1次的树边,贡献是1
对于被环覆盖0次的树边,贡献是M
然后用树形dp,dp[u]:=(u,f)这条边被环覆盖的次数 对于(u,v),先计数dp[u]++,dp[v]++,自底向上dp之后就可以把环上的点都更新了,但是对于环上的点来说显然lca要减去,dp[lca]−=2

【AC 代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
const int maxm = 200010;
int dp[maxn],n,m;//dp[i]表示i到fa[i]的被覆盖次数
int headE[maxn],tot1;
struct Edge{
    int v,next;
}E[maxn*2];
void addEdge(int u,int v)
{
    E[tot1].v=v,E[tot1].next=headE[u],headE[u]=tot1++;
}
void init1()
{
    memset(headE,-1,sizeof(headE));
    tot1=0;
}

int headQ[maxn],tot2;
struct Query{
    int v,next,id;
}Q[maxn<<2];
void addQuery(int u,int v,int i)
{
    Q[tot2].v=v,Q[tot2].id=i,Q[tot2].next=headQ[u],headQ[u]=tot2++;
}
void init2()
{
    memset(headQ,-1,sizeof(headQ));
    tot2=0;
}

bool vis[maxn],check[maxn];
int ancestor[maxn];
int Find(int x)
{
    if(x==ancestor[x]) return x;
    else return ancestor[x]=Find(ancestor[x]);
}
void tarjian(int u)
{
    ancestor[u]=u;
    vis[u]=true;
    for(int i=headE[u]; ~i; i=E[i].next)
    {
        int v=E[i].v;
        if(vis[v]) continue;
        tarjian(v);
        ancestor[v]=u;
    }
    for(int i=headQ[u]; ~i; i=Q[i].next)
    {
        int v=Q[i].v,id=Q[i].id;
        if(!vis[v]) continue;
        if(check[id]) continue;
        check[id]=true;
        int lca=Find(v);
        dp[lca]-=2;
    }
}
void dfs(int u,int fa)
{
    for(int i=headE[u]; ~i; i=E[i].next)
    {
        int v=E[i].v;
        if(v==fa) continue;
        dfs(v,u);
        dp[u]+=dp[v];
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v;
        init1();
        init2();
        memset(dp,0,sizeof(dp));
        for(int i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            addEdge(u,v);
            addEdge(v,u);
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            addQuery(u,v,i);
            addQuery(v,u,i);
            ++dp[u],++dp[v];
        }
        memset(vis,false,sizeof(vis));
        memset(check,false,sizeof(check));
        tarjian(1);
        dfs(1,-1);
        int ans=0;
        for(int i=2; i<=n; i++)
        {
            if(dp[i]==0) ans+=m;
            else if(dp[i]==1) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}