poj3417

Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5665 Accepted: 1602

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

Source

此题可采用树上差分,差分时w[x],w[y]分别加一,而w[lca(x,y)]-=2;

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;

struct my{
   int next;
   int v;
};

const int maxn=1000000+10;
int  adj[maxn],fa,d[maxn],f[maxn][21],t,w[maxn];
queue<int>Q;
my bian[maxn*2];

void myinsert(int u,int v){
     bian[++fa].v=v;
     bian[fa].next=adj[u];
     adj[u]=fa;
}

void bfs(){
     d[1]=1;
     Q.push(1);
     while(!Q.empty()){
        int x=Q.front();
        Q.pop();
        for (int i=adj[x];i;i=bian[i].next){
            int v=bian[i].v;
            if(d[v]) continue;
            d[v]=d[x]+1;
            f[v][0]=x;
            for (int j=1;j<=t;j++)
                f[v][j]=f[f[v][j-1]][j-1];
            Q.push(v);
        }
     }
}

int lca(int x,int y){
    if(d[x]>d[y]) swap(x,y);
    for (int i=t;i>=0;i--) if(d[f[y][i]]>=d[x]) y=f[y][i];
    if(x==y) return x;
    for (int i=t;i>=0;i--) if(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 i=adj[x];i;i=bian[i].next){
        int v=bian[i].v;
        if(v==fa) continue;
        dfs(v,x);
        w[x]+=w[v];
     }
}

int main(){
    int n,m;
    int u,v;
    scanf("%d%d",&n,&m);
    for (int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        myinsert(u,v);
        myinsert(v,u);
    }
    t=int(log(n)/log(2))+1;
    bfs();
    for (int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        w[u]++;
        w[v]++;
        w[lca(u,v)]-=2;
    }
    int  ans=0;
    dfs(1,1);
    for (int i=2;i<=n;i++){
         if(!w[i]) ans+=m;
        else if(w[i]==1) ans++;
    }
    printf("%d\n",ans);
return 0;
}

 

转载于:https://www.cnblogs.com/lmjer/p/8719466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值