Poj3417——Network

翻译

Network

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5935 Accepted: 1698

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

题解

我们称每条附加边都把树上 x x , y y 之间的路径上的每条边“覆盖了一次”。我们只需统计出每条“主要边”被覆盖了几次。若把第一步覆盖0次的主要边切断,则第二次可任意切一条附加边。若第一步把被覆盖1次的主要边切断,则第二步方法唯一。若第一步把被覆盖2次及以上的主要边切断,则无法实现。

因此,经过转化之后,我们将此题变成了这样一个模型:给一个无向图和一颗生成树,求每条树边被非树边覆盖了几次。

对此,我们运用树上差分算法,对于每条非树边 (x,y) ( x , y ) ,令x的权值增加,令y的权值增加,令 lca(x,y) l c a ( x , y ) 的权值减少2,对这颗生成树进行一次dfs,求出 f[x] f [ x ] 表示以x为根的子树中各个节点的权值之和。 f[x] f [ x ] 就是与它的父节点之间的树边被覆盖的次数

代码

#include
#include
#include
#include
#include
#include
using namespace std;
const int N=400005;
int n,m,vet[N],deep[N],head[N],next[N],edgenum,fa[N][25],f[N];
void addedge(int u,int v)
{
    vet[++edgenum]=v;
    next[edgenum]=head[u];
    head[u]=edgenum;
}
void dfs(int u)
{
    for (int e=head[u]; e!=-1; e=next[e]){
        if (deep[vet[e]]) continue;
        fa[vet[e]][0]=u;
        deep[vet[e]]=deep[u]+1;
        dfs(vet[e]);
    }
}
int lca(int x,int y)
{
    if (deep[x]for (int i=20; i>=0; i--)
        if (deep[fa[x][i]]>=deep[y]) x=fa[x][i];
    for (int i=20; i>=0; i--)
        if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    if (x!=y) x=fa[x][0];
    return x;
}
void dfs_second(int u)
{
    for (int e=head[u]; e!=-1; e=next[e]){
        int v=vet[e];
        if (deep[v]<=deep[u]) continue;
        dfs_second(v);
        f[u]+=f[v];
    }
}
int main()
{
    memset(head,-1,sizeof(head)); memset(next,-1,sizeof(next));
    scanf("%d%d",&n,&m);
    for (int i=1; iint x,y; scanf("%d%d",&x,&y);
        addedge(x,y); addedge(y,x);
    }
    deep[1]=1; dfs(1);
    for (int i=1; i<=20; i++)
        for (int j=1; j<=n; j++) fa[j][i]=fa[fa[j][i-1]][i-1];
    for (int i=1; i<=m; i++){
        int x,y; scanf("%d%d",&x,&y);
        f[x]++; f[y]++; f[lca(x,y)]-=2;
    }
    dfs_second(1); int ans=0;
    for (int i=2; i<=n; i++)
        if (f[i]==0) ans+=m; else if (f[i]==1) ans++;
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(一本通提高篇)