【九校联考】苹果树

题目描述

小 B 是一个辛勤的农民,他家里种了一棵很大的苹果树。

这棵苹果树可以看作一张 n 个点 n-1 条边的无向连通图,小 B 觉得这颗苹果树很脆弱,因为只要剪断任意一条边,苹果树就不连通了,于是他给苹果树新加了 m 条边。现在这颗苹果树就不像是一棵树了,成了一张 n 个点 n+m-1 条边的无向连通图,小 Q是小 B 的好朋友,他觉得这棵树依然很脆弱,他告诉小 B,自己只要破坏两条边,一条是原树中的边,一条是小 B 新加的边,就能使苹果树不连通。

小 B 觉得小 Q 说得很不可思议,于是他找来了擅长编程的你,希望你告诉他,按照小B 的破坏规则,有多少种不同的方案使得苹果树不连通。注意:两种方案不同当且仅当一条边在第一种方案中被删除了但在第二种方案中没有被删除。


输入

第一行两个正整数 n,m(n,m ≤ 300,000,表示苹果树的点数和后面新加的边的个数。

接下来 n-1 行,每行两个正整数 u,v,表示(u,v)之间有一条树边。
接下来 m 行,每行两个正整数 u,v,表示(u,v)之间有一条小 B 新加的边。


输出

输出一行一个整数,表示有多少种不同的方案。


样例输入

4 1
1 2
2 3
1 4
3 4


样例输出

3



题解

这道题其实是某个考试题的简化版。因为原树是连通的,那么一定会删去一条原树上的边。

考虑删去这条边之后,由于新加入了m条边,如果这m条边没有覆盖到删去的边,那么图依旧不连通(即删去的是桥),ans+=m,即这新加的m条边都可以取。

如果这m条边里有一条边覆盖删去的边的话,就删除这条边,ans++。

如果有2条或以上的边覆盖删去的边的话,此时不能删去这条边。

所以只需树剖算出树上的边被新的m条边覆盖的次数,再枚举检查一遍原树上的边是否可以删去就可以得到答案了。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

const int maxn=300000*2+50;
const int maxm=300000*4+50;

int fir[maxn],nex[maxm],to[maxm],from[maxm],ecnt,cnt,tot,ans;
int son[maxn],top[maxn],dep[maxn],sz[maxn],wt[maxn],id[maxn],fa[maxn];
int n,m,x,y,dfn[maxn],low[maxn],t,bri[maxn];
bool p[maxn];

void add_edge(int u,int v){
    nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;from[ecnt]=u;
}

struct SegmentTree{int l,r,v,add;}st[maxn*4];

void dfs1(int x,int f,int deep){
    fa[x]=f;
    dep[x]=deep;
    sz[x]=1;
    int maxson=-1;
    for(int e=fir[x];e;e=nex[e]){
        int v=to[e];
        if(v==f) continue;
        dfs1(v,x,deep+1);
        sz[x]+=sz[v];
        if(sz[v]>maxson) maxson=sz[v],son[x]=v;
    }
}

void dfs2(int x,int topf){
    top[x]=topf;
    id[x]=++cnt;
    if(!son[x]) return ;
    dfs2(son[x],topf);
    for(int e=fir[x];e;e=nex[e]){
        int v=to[e];
        if(v==fa[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}

void pushup(int root){
    st[root].v=st[root<<1].v+st[root<<1|1].v;
}

void build(int root,int l,int r){
    st[root].l=l;st[root].r=r;
    int m=l+r>>1;
    if(l==r) return ;
    build(root<<1,l,m);build(root<<1|1,m+1,r);
}

void pushdown(int root){
    st[root<<1].v+=st[root].add*(st[root<<1].r-st[root<<1].l+1);
    st[root<<1|1].v+=st[root].add*(st[root<<1|1].r-st[root<<1|1].l+1);
    st[root<<1].add+=st[root].add;
    st[root<<1|1].add+=st[root].add;
    st[root].add=0;
}

void add(int root,int l,int r,int val){
    if(st[root].l>r||st[root].r<l) return ;
    if(st[root].l>=l&&st[root].r<=r){
        st[root].v+=val*(st[root].r-st[root].l+1);
        st[root].add+=val;
    }
    else{
        pushdown(root);
        add(root<<1,l,r,val);add(root<<1|1,l,r,val);
        pushup(root);
    }
}

int query(int root,int l,int r){
    if(st[root].l>r||st[root].r<l) return 0;
    if(st[root].l>=l&&st[root].r<=r) return st[root].v;
    pushdown(root);
    return query(root<<1,l,r)+query(root<<1|1,l,r);
}

void Change(int x,int y,int val){
    int f1=top[x],f2=top[y];
    while(f1!=f2){
        if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        add(1,id[f1],id[x],val);
        x=fa[f1];f1=top[x];
    }
    if(dep[x]>dep[y]) swap(x,y);
    add(1,id[x],id[y],val);
}

int Query(int x,int y){
    int f1=top[x],f2=top[y],ans=0;
    while(f1!=f2){
        if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        ans+=query(1,id[f1],id[x]);
        x=fa[f1];f1=top[x];
    }
    if(dep[x]>dep[y]) swap(x,y);
    ans+=query(1,id[x],id[y]);
    return ans;
}

int lca(int x,int y){
    int f1=top[x],f2=top[y];
    while(f1!=f2){
        if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        x=fa[f1];f1=top[x];
    }
    return dep[x]<dep[y]?x:y;
}

template<typename T>void read(T& aa){
    char cc; ll ff;aa=0;cc=getchar();ff=1;
    while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
    if(cc=='-') ff=-1,cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    aa*=ff;
}

int main(){
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    memset(p,false,sizeof(p));
    read(n),read(m);
    for(int i=1;i<n;i++){
        read(x),read(y);
        add_edge(x,y);
        add_edge(y,x);
    }
    int ee=ecnt;
    dfs1(1,0,1);dfs2(1,1);build(1,1,n);
    for(int i=1;i<=m;i++){
        read(x),read(y);
        Change(x,y,1);
        int w=lca(x,y);
        Change(w,w,-1);
        add_edge(x,y);add_edge(y,x);
    }
    for(int e=1;e<=ee;e+=2){
        int u=from[e],v=to[e];
        if(dep[u]>dep[v]) swap(u,v);
        int w=Query(v,v);
        if(w==0) ans+=m;
        if(w==1) ans++;
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/rlddd/p/9800042.html