POJ 2367 Genealogical Tree "topological sort"

Source: Internet
Author: User

Test instructions: It probably means--there is a family gathered together, now by the people inside the family to speak, generational high person first speech. Now give N, and then give the number of n row number I input means the number of descendants of line I, then these numbers are in the back of I.

Like Example 5

0

4 5 1 0

1 0

5 3 0

3 0

1 No number behind

2 followed by 4 5 1

3 followed by a 1

4 followed by 5 3

5 followed by a 3

A little experience of topological sort

(1) First save the diagram and then store the corresponding vertex degree

(2) Select a point without a precursor (i.e., a point with an entry level of 0) in the diagram and join the queue

(3) Delete the edge of the point at which it is starting (minus 1 of the points pointed to by the dot)

(4) Repeat the above operation until there are no points with an entry level of 0

#include <iostream>  #include <cstdio>  #include <cstring>  #include <algorithm>  using namespace Std;int iq,n;int queue[1000],map[1000][1000],indegree[1000];void toposort () {int i,j,k;iq=0;for ( i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (map[i][j]) indegree[j]++;}} for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (indegree[j]==0) {queue[iq++]=j;break;}} indegree[j]=-1;//this point into the after, the degree of this is recorded as-1, in order to avoid the next time it is added to the queue for (k=1;k<=n;k++) {if (map[j][k]) indegree[k]--;}} int main () {int i,v;scanf ("%d", &n), for (i=1;i<=n;i++) {while (scanf ("%d", &v)!=eof&&v) map[i][v]=1;} Toposort (); for (i=0;i<iq-1;i++) printf ("%d", Queue[i]);p rintf ("%d", Queue[i]);}

  

The first question of topological sequencing [email protected][email protected]

Go--go

POJ 2367 Genealogical Tree "topological sort"

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.