Accessibility statistics (topological sorting)

Source: Internet
Author: User
Tags bitset

Describe

Given a direction-free graph of N-point m-bars, the number of points that can be reached from each point is counted separately. n,m≤30000.

Input format

The first line is two integers n,m, and the next M-line is two integers x, y, and x = Y, which represents a forward edge from X.

Output format

A total of n rows representing the number of points that each point can reach.

Sample input
10 103 82 32 55 95 92 33 94 82 104 9
Sample output
1633211111

Analysis: Topology sequencing, in order to count the convenience of avoiding repeated records subsequent, using Bitset to store n nodes for the accessibility of N nodes, the binary directly through or operation to scan the topological sequence from backward forward.

#define MAX 30001int head[max],nxt[max],ver[max],deg[max];int a[max];int n,m;int cnt = 0,tot=0;bitset<30001> s[    max];void Add (int x,int y) {Ver[++tot] = y;    Nxt[tot] = head[x];    HEAD[X] = tot; deg[y]++;}    void Topsort () {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=nxt[i]) {int y = ver[i];        if (--deg[y]==0) Q.push (y);        }}}void Sol () {for (int i=cnt;i;i--) {int x = a[i];        S[X][X] = 1;            for (int j=head[x];j;j=nxt[j]) {int y=ver[j];        S[x]|=s[y];    }}}int Main () {cin>>n>>m;        for (int i=0;i<m;i++) {int x, y;        scanf ("%d%d", &x,&y);    Add (x, y);    } topsort ();    Sol ();    for (int i=1;i<=n;i++) {printf ("%d\n", S[i].count ()); } return 0;} 

Accessibility statistics (topological sorting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.