1.可达性统计
题目链接
#include <iostream>
#include <bitset>
#include <algorithm>
#include <queue>
#include<string.h>
using namespace std;
const int M = 30000 + 10;
int edge[M], ne[M], last[M];
int d[M], seq[M]; //入度,拓扑排序的序列
int cnt = 0;
int n, m;
bitset<M> f[M];
void add(int a, int b)
{ //表示a为父 b为子
edge[cnt] = b;
ne[cnt] = last[a];
last[a] = cnt++;
}
void topsort()
{
queue<int> q;
for(int i=1;i<=n;i++)
if(d[i]==0) q.push(i);
int k=0;
while (q.size())
{
int t=q.front();
q.pop();
seq[k++]=t;
for(int i=last[t];i>=0;i=ne[i])
{
int j=edge[i];
if(--d[j]==0)
q.push(j);
}
}
}
int main()
{
cin >> n >> m;
int x, y;
memset(last,-1,sizeof(last));
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
add(x, y);
d[y]++;
}
topsort();
for(int i=n-1;i>=0;i--)
{
int j=seq[i];
f[j][j]=1;
for(int k=last[j];k>=0;k=ne[k])
f[j]|=f[edge[k]];
}
for(int i=1;i<=n;i++ )cout<<f[i].count()<<endl;
}