#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;
const int N =3e4+7;
int m, n;
bitset<N> f[N]; //压缩成01串来表示可达与不可达
vector<int> G[N];
int in[N], seq[N];
void topsort(){
queue<int> q;
for(int i = 1; i <= n; i++){
if(in[i] == 0) q.push(i);
}
int k = 0;
while(q.size()){
int t = q.front();
q.pop();
seq[k++] = t; //拓扑排序的序列放入数组中
for(auto &v: G[t]){
in[v]--;
if(in[v] == 0) q.push(v);
}
}
}
int main(){
int u, v;
cin >> n>>m;
while(m--){
cin>>u>>v;
G[u].push_back(v);
in[v]++;
}
topsort();
for(int i = n-1; i >= 0; i--){ //从后往前统计每个可达的点
int j = seq[i];
f[j][j] = 1;
for(auto &u: G[j]){
f[j] |= f[u];
}
}
for(int i = 1; i <= n; i++) cout<<f[i].count()<<endl;
return 0;
}
164. 可达性统计
最新推荐文章于 2022-05-20 22:17:23 发布