Accessibility statistics

Topic Link

The meaning of problems: given directed acyclic graph, each count number to reach the starting point of each point from a point M of the N of edges. N, M <= 30000.

Ideas: first topological sorting, so certainly in front of the front of the topological order definitely in the back of the topology point of order. Then compressed state, is converted into a binary bitset, meal or bit from the back, the number 1 represents the number of the number of points can be achieved there.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cmath>
#include<queue>
#include<iostream>
using namespace std;
const int maxn = 30000+7;
int head[maxn],ver[maxn];
int Next[maxn];
int tot,cnt;
int deg[maxn],a[maxn];
int n,m;
bitset<maxn> c[maxn];
void add(int x, int y){
    ver[++tot] = y, Next[tot] = head[x];
    head[x] = tot;
    deg[y]++;
}
 
void toposort(){
    queue<int> q;
    for(int i = 1; i <= n; i++)
        if(deg[i] == 0) q.push(i);
    while(q.size()){
        int x = q.front();
        q.pop();
        a[++cnt] = x;
        for(int i = head[x]; i; i = Next[i]) {
            int y = ver[i];
            deg[y]--;
            if(deg[y] == 0) q.push(y);
        }
    }
}
 
void solve(){
    int x, y;
    for(int i = cnt; i >= 1; i--) {
        x = a[i];
        c[x][x] = 1;
        for(int j = head[x]; j; j = Next[j]) {
            int y = ver[j];
          
            c[x] |= c[y];
        }
    }
}
 
int main(){
    int x, y;
    scanf("%d %d", &n, &m);
    while(m--){
        scanf("%d %d", &x, &y);
        add(x, y);
    }
    toposort();
    solve();
    for(int i = 1; i <= n; i++)
        printf("%d\n",c[i].count());
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/2462478392Lee/p/11285039.html