可达性统计 拓扑排序

因为一个点能到达的点等于他后续的点能到达的点集的并集,因此需要先算完后续的点才能算该点。
如何先算后续的点呢?先找到已经算完的点,当一个点的入度等于0的时候,它的值就不会再有变化了,这启发我们用拓扑排序。
反向建边,这样子每次找到一个入度为0的点,就可以找到他的前驱节点,并将自身的点集并到前驱结点上。
由于一个点可能被多个点到达,这样子不能简单的统计到达的点的数量,这里用的是bitset来进行合并。

#include <bits/stdc++.h>
using namespace std;
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define REP(i, a, n) for (int i = a; i <= n; ++i)
#define PER(i, n, a) for (int i = n; i >= a; --i)
#define LL int64_t
#define OUT(var) cout << #var << "=" << var << '\n'
template<typename T>void In(T &x){x=0;char ch=getchar();LL f=1;while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}x*=f;}
template<typename T>int64_t __lcm(T a, T b){return a/__gcd(a,b)*b;}
template<typename T>int64_t __ceil(T a, T b){return (a+b-1)/b;}
template<typename T>ostream& operator<<(ostream& os,const vector<T>& v){for(int i = 0;i < v.size();i++)os<<v[i]<<" ";return os;}
template<typename T>ostream& operator<<(ostream& os,const set<T>& v){for(typename set<T>::iterator it = v.begin();it != v.end();it++)os<<*it<<" ";return os;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline __int128 read(){__int128 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;}
inline void print(__int128 x){if(x<0){putchar('-');x=-x;}if(x>9)print(x/10);putchar(x%10+'0');}
inline int64_t __rand(LL a,LL b) {return a + rng() % (b-a+1);}
inline int64_t QuickPow(LL base,LL n,LL Mod=0){LL ret(1);while(n){if(n&1){ret*=base;if(Mod)ret%=Mod;}base*=base;if(Mod)base%=Mod;n>>=1;}return Mod?ret%Mod:ret;}
inline int64_t __inv(LL x,LL p){return QuickPow(x,p-2,p);}
inline void GetTime(){time_t rawtime;struct tm *ptminfo;time(&rawtime);ptminfo = localtime(&rawtime);printf("Current time: %02d-%02d-%02d %02d:%02d:%02d\n",ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);}
struct Point{int x,y;Point(int x = 0,int y = 0):x(x),y(y){}};
const double pi = acos(-1.0);
const LL kInf(LONG_LONG_MAX);
const LL kMod(998244353);
const LL kMax(3e4+10);
LL group(1);
int head[kMax],cnt = 0;
bitset<kMax>bs[kMax];
int n,m;
int in[kMax];
set<pair<int,int> >s;
struct node
{
    int to,next;
}edge[kMax<<1];
void init()
{
    for(int i = 1;i <= n;i++)
    {
        head[i] = -1;
        bs[i].set(i,1);
    }
    s.clear();
}
void add(int u,int v)
{
    edge[++cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
}

inline void Solve()
{
    /*write your code down here*/
    cin>>n>>m;
    init();
    for(int i = 1;i <= m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        if(!s.count({v,u})){
            s.insert({v,u});
            add(v,u);
            in[u]++;
        }
    }
    queue<int>q;
    for(int i = 1; i <= n;i++)
    {
        if(in[i] == 0)q.push(i);
    }
    while(!q.empty())
    {
        int pos = q.front();
        q.pop();
        for(int i = head[pos];i != -1;i = edge[i].next)
        {
            int to = edge[i].to;
            bs[to]|=bs[pos];
            in[to]--;
            if(in[to] == 0)q.push(to);
        }
    }
    for(int i = 1;i <= n;i++)
        cout<<bs[i].count()<<'\n';
}
int main()
{
    //Read(group);
    //gettime();
    while (group--)
    {
        Solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值