Hdu 1285 topological sorting

Source: Internet
Author: User

The simplest Topology Sorting method is as follows: (1) Select a vertex from the directed graph without a forward (that is, the inbound degree is 0) and output it. (2) Delete the vertex from the net and delete all directed edges from the vertex. (3) Repeat the preceding two steps until there are no longer any vertices without the precursor in the remaining network or all vertices are sorted. [cpp] # include <stdio. h> # include <string. h> # define N 505 int map [N] [N], index [N], used [N], n, m; // map indicates the adjacent matrix, index record inbound, used record sorting void toposort () {int I, j, topo = 0; while (topo <n) {for (I = 1; I <= n; I ++) {if (index [I] = 0 & used [I] = 0) {// I from small to large, if the number is small, used [I] = 1; break; // Delete the point where the input degree is 0} if (I = n + 1) Return; // ends if (topo) printf (""); printf ("% d", I); topo ++ if there is no entry point; for (j = 1; j <= n; j ++) {// delete all edges starting with I if (map [I] [j] = 1) {map [I] [j] = 0; index [j] -- ;}}} int main () {int I, a, B; while (scanf ("% d", & n, & m )! = EOF) {memset (map, 0, sizeof (map); memset (index, 0, sizeof (index); memset (used, 0, sizeof (used )); for (I = 1; I <= m; I ++) {scanf ("% d", & a, & B ); if (map [a] [B] = 0) {// when considering the duplicate edge, map [a] [B] = 1; index [B] ++ ;}} toposort (); printf ("\ n") ;}return 0 ;}

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.