【POJ3417】Network(树形dp)

8 篇文章 0 订阅
4 篇文章 0 订阅

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

题目大意:给出一棵无根树,然后再给出m条边,把这m条边连上,然后你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂。

题解:这种一眼看上去不会的题可以先画图试试……
画完图,我们可以发现:如果某条边被大于1个环覆盖,那么无论如何这条边都不会使树断裂。
于是只会有两种情况:
1.这条边被0个环覆盖。它断了那树就断了。因此,它可以和任意新边组合断裂,产生m种方法。
2.这条边被1个环覆盖。只有它和形成这个环的新边一起断裂,树才会断裂,因此产生1种方法。
至此,本题的方法就比较明晰了。用dp[i]表示i上方的边被覆盖了几次。对于一条新边(u,v),dp[u]++,dp[v]++,dp[lca(u,v)]—-,然后treedp,统计答案就好了。

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100005
struct node
{
    int to,nex;
}edge[N<<1];
int h[N],p[N][30],vis[N],head[N],tot,dp[N];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(h,0,sizeof(h));
    memset(p,-1,sizeof(p));
    memset(dp,0,sizeof(dp));
}
void add(int u,int v)
{
    edge[tot].to=v;edge[tot].nex=head[u];head[u]=tot++;
}
void dfs(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=edge[i].nex)
    {
        int v=edge[i].to;
        if(!vis[v])
        {
            p[v][0]=u;h[v]=h[u]+1;
            dfs(v);
        }
    }
}
void rmq(int n)
{
    for(int j=1;(1<<j)<=n;j++)
    for(int i=1;i<=n;i++)
    if(~p[i][j-1]) p[i][j]=p[p[i][j-1]][j-1];
}
int lca(int a,int b)
{
    int i;
    if(h[a]<h[b]) swap(a,b);
    int t=h[a]-h[b];
    for(i=0;(1<<i)<=h[a];i++); 
    for(int j=i-1;j>=0;j--)
    if(t&(1<<j)) a=p[a][j];
    if(a==b)return a;
    for(int j=i-1;j>=0;j--)
    if(~p[a][j] && p[a][j]!=p[b][j]) a=p[a][j],b=p[b][j];
    return p[a][0];
}
void DP(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=edge[i].nex)
    {
        int v=edge[i].to;
        if(!vis[v])
        {
            DP(v);
            dp[u]+=dp[v];
        }
    }
}
int main()
{
    int n,m,u,v;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v),add(v,u);
        }
        dfs(1);rmq(n);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            dp[u]++,dp[v]++,dp[lca(u,v)]-=2;
        }
        memset(vis,0,sizeof(vis));
        DP(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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值