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)