POJ3417:Network

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

这题还是蛮妙的啊

首先注意到这是一棵树,树上加边,现在要删除一条树的边和一条补边使它不联通

单单只有树的话删什么的可以,但是还有补边

但其实补边支持的还是两个点路径之间所有的边,那么我们就染色来体现这个操作

 树上差分,当前d[x]++,d[y]++,d[LCA(x,y)]-=2;

最后DFS一遍,就可以得到每个点到它父亲的值

如果为0,随便删补边,如果为1,只能删与它关联的补边,如果>=2,那就不可行

//MT_LI
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n,m;
struct node{
    int x,y,next;
}a[810000];int len,last[210000];
int d[210000],bin[21];
int f[210000][21],dep[210000];
void ins(int x,int y)
{
    len++;
    a[len].x=x;a[len].y=y;
    a[len].next=last[x];last[x]=len;
}
void dfs(int x,int fa)
{
    dep[x]=dep[fa]+1;
    f[x][0]=fa;
    for(int i=1;bin[i]<=dep[x];i++)
        f[x][i]=f[f[x][i-1]][i-1];
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fa)
            dfs(y,x);
    }
}
int LCA(int x,int y)
{
    if(dep[x]<dep[y])swap(x,y);
    for(int i=20;i>=0;i--)
        if(dep[y]+bin[i]<=dep[x])
            x=f[x][i];
    if(x==y)return x;
    for(int i=20;i>=0;i--)
        if(dep[x]>=bin[i]&&f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    return f[x][0];
}
void DFS(int x,int fa)
{
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fa)
        {
            DFS(y,x);
            d[x]+=d[y];
        }
    }
}
int main()
{
    bin[0]=1;
    for(int i=1;i<=20;i++)bin[i]=bin[i-1]<<1;
    scanf("%d%d",&n,&m);
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        ins(x,y);ins(y,x);
    }
    dfs(1,0);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        d[x]++;d[y]++;
        d[LCA(x,y)]-=2;
    }
    DFS(1,0);
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        if(d[i]==0)ans+=m;
        if(d[i]==1)ans++;
    }
    printf("%d\n",ans);
    return 0;
}
/*
9 2
1 2
1 3
1 4
2 5
2 6
4 7
4 8
7 9
6 7
9 8
*/

 

转载于:https://www.cnblogs.com/MT-LI/p/9684917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值