【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


【题目大意】给出一棵有n个节点的树。现在树上新增m条新边,问有多少种方案使得断开一条树边和一条新边之后,图被分为互不连通的两部分。

【题解】树上倍增lca+树上差分
因为方案是选择断开一条新边一条旧边,我们选择先观察旧边。什么情况下断开一条树边后无论断开哪一条新边都无法使得图被分成两块?
如果有两条以上的新边能替代这条旧边,这条边对答案是没有贡献的。那么怎么看有几条边能代替这条旧边呢?如果一条新边 (p,q),它能替代的边将会是 lca(p,q) 分别向下走到p和q经过的所有边。
这里用到树上差分,对于每一条新边(u,v),找到两点的最近公共祖先lca,将 s[u]++, s[v]++, s[lca]-=2, 做完后,对于点x,统计其为根的子树的 s 值总和sum,而sum即为能代替旧边 (x, fa[x]) 的新边数量。//这个可以参照 {【NOIP2015】运输计划} 的想法。
得到这个性质后,计算方案:
<1>若此边能被一条新边代替,对方案数的贡献为1;
<2>若此边能被两条新边代替,则无论取那条新边都能符合要求,对方案数的贡献为m;
统计得出答案即可。


#include 
#include 
#include 
#define N 100005
struct edge{ int to,nxt;}e[N<<1];
int n,m,cnt,q[N],f[N][20],d[N],s[N],fi[N];
bool bo[N];long long ans;
    void add(int u,int v)
    {
        e[++cnt].to=v;e[cnt].nxt=fi[u];fi[u]=cnt;
    }
    void lca() 
    {
        int h=1,t=1;
        memset(bo,false,sizeof(bo)); 
        for (bo[q[1]=1]=true;h<=t;++h)
            for (int i=fi[q[h]];i;i=e[i].nxt)
                if (!bo[e[i].to])
                {
                    bo[q[++t]=e[i].to]=true;
                    f[e[i].to][0]=q[h];
                    d[e[i].to]=d[q[h]]+1;
                    for (int j=0,k=q[h];f[k][j];k=f[k][j++])
                        f[e[i].to][j+1]=f[k][j];
                }
    }
    int findlca(int u,int v)
    {
        if (d[u]for (int i;d[u]>d[v];u=f[u][i-1])
            for (i=1;d[f[u][i]]>d[v];++i);
        for (int i;u!=v;u=f[u][i-1],v=f[v][i-1])
            for (i=1;f[u][i]!=f[v][i];++i); 
        return u;
    }
    void dfs(int x,int fa)
    {
        for (int i=fi[x];i;i=e[i].nxt)
            if (e[i].to!=fa)
            {
                dfs(e[i].to,x);
                s[x]+=s[e[i].to];
            }
        if (x!=1) ans=ans+(s[x]==1)+(!s[x])*m; 
    }
int main()
{
    scanf("%d%d\n",&n,&m);
    for (int i=1;iint u,v;scanf("%d%d\n",&u,&v);
        add(u,v);add(v,u);
    }
    lca();
    for (int i=1;i<=m;++i)
    {
        int u,v;scanf("%d%d\n",&u,&v);
        int lca=findlca(u,v);
        ++s[u];++s[v];s[lca]-=2;
    }
    ans=0;dfs(1,0);
    printf("%lld\n",ans);
    return 0;
}

【题外话】话说每次写倍增RE都是我不得不说的痛吗???!!!

这里写图片描述

//忘打 return u; 的伤感。。。

你可能感兴趣的:(倍增)