bitset使用
const int maxn = 30005;
bitset<maxn> f;
f.reset() //全部变成0
f.count() //统计1的个数
// 可使用|&等其他用法
拓扑排序
拓扑排序的数组倒着使用bitset与相关点进行或运算使其统计可以到达的点
#include<bits/stdc++.h>
using namespace std;
const int maxn = 30005;
bitset<maxn> f[maxn];
vector<int> var[maxn];
vector<int> a;
int du[maxn];
queue<int> q;
int tot, cnt, n, m;
void topsort(){
for(int i = 1; i <= n; i++){
if(du[i] == 0)
q.push(i);
}
while(!q.empty()){
int x = q.front();
q.pop();
a.push_back(x);
for(int j = 0; j < var[x].size(); j++){
if(--du[var[x][j]] == 0)
q.push(var[x][j]);
}
}
}
int main(){
//freopen("可达性统计.txt","r",stdin);
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++){
int x, y;
scanf("%d %d", &x, &y);
du[y]++;
var[x].push_back(y);
}
topsort();
for(int i = a.size() - 1; i >= 0; i--){
int x = a[i];
f[x].reset();
f[x][x] = 1;
for(int j = 0; j < var[x].size(); j++){
f[x] |= f[var[x][j]];
}
}
for(int i = 1; i <= n; i++){
printf("%d\n", f[i].count());
//cout << "ok" << endl;
}
return 0;
}