Acwing-164- accessibility statistics (topological sorting, bit computing statistics)

link:

https://www.acwing.com/problem/content/166/

Meaning of the questions:

Given a point M of the N sides of the directed acyclic graph, each count the number of the starting point can be reached from each point.

Ideas:

Obtaining first topological sort order, and then by using bitset bit operation, and set recording, repeated calculation can solve the problem.

Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e4+10;
vector<int> G[MAXN];
vector<int> Tup;
bitset<30010> F[MAXN];
int Dis[MAXN];
int n, m;

void Tupo()
{
    queue<int> que;
    for (int i = 1;i <= n;i++)
    {
        if (Dis[i] == 0)
            que.push(i);
    }
    while (!que.empty())
    {
        int node = que.front();
        que.pop();
        Tup.push_back(node);
        for (int i = 0;i < G[node].size();i++)
        {
            int to = G[node][i];
            if (--Dis[to] == 0)
                que.push(to);
        }
    }
}

void Solve()
{
    for (int i = n-1;i >= 0;i--)
    {
        int x = Tup[i];
        F[x].reset();
        F[x][x] = 1;
        for (int k = 0;k < G[x].size();k++)
        {
            F[x] |= F[G[x][k]];
        }
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    int u, v;
    for (int i = 1;i <= m;i++)
    {
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        Dis[v]++;
    }
    Tupo();
    Solve();
    for (int i = 1;i <= n;i++)
        printf("%d\n", (int)F[i].count());

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11531019.html