bzoj 4484: [Jsoi2015]最小表示 拓扑排序+bitset

24 篇文章 0 订阅

题意

对于一个N个点(每个点从1到N编号),M条边的有向图,JYY发现,如果从图中删去一些边,那么原图的连通性会发生改变;而也有一些边,删去之后图的连通性并不会发生改变。JYY想知道,如果想要使得原图任意两点的连通性保持不变,我们最多能删掉多少条边呢?为了简化一下大家的工作量,这次JYY保证他给定的有向图一定是一个有向无环图
n<=30000,m<=100000

分析

如果我们单分析一条边在什么情况下会被删掉,显然不好分析。我们换一个角度,对于每一个点x,在它连出去的边中,有哪些是会被删掉的。显然如果x连向的节点中有一个可以走到另一个,那么x指向另一个的边是可以被删掉的。那么我们只要按拓扑序倒着做,同时用bitset维护连通性就好了。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<queue>
using namespace std;

const int N=30005;

int n,m,d[N],a[N],last[N],cnt;
struct edge{int to,next;}e[100005];
bitset<N> f[N];
queue<int> que;

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void addedge(int u,int v)
{
    e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
}

void topsort()
{
    int a1=0;
    for (int i=1;i<=n;i++) if (!d[i]) que.push(i);
    while (!que.empty())
    {
        int u=que.front();que.pop();a[++a1]=u;
        for (int i=last[u];i;i=e[i].next)
        {
            d[e[i].to]--;
            if (!d[e[i].to]) que.push(e[i].to);
        }
    }
}

int main()
{
    n=read();m=read();
    for (int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        addedge(x,y);d[y]++;
    }
    topsort();
    int ans=0;
    for (int i=n;i>=1;i--)
    {
        int x=a[i];
        for (int j=last[x];j;j=e[j].next)
        {
            int to=e[j].to;
            f[x]|=f[to];
        }
        for (int j=last[x];j;j=e[j].next)
        {
            int to=e[j].to;
            if (f[x][to]) ans++;
            f[x].set(to);
        }
    }
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值