Implementation of simple Topology Sorting

Source: Internet
Author: User

[Cpp]
* Implementation of topological sorting */
# Include <iostream>
# Include <iostream>
Using namespace std;
# Deprecision MAX 100
 
Int main ()
{
Int map [MAX] [MAX];
Int d [MAX];
Int c [MAX];
Int n, m;
While (cin> n> m)
{
If (m = 0 & n = 0) break;
Int a, B;
Memset (map, 0, sizeof (map ));
Memset (d, 0, sizeof (d ));
For (int I = 0; I <m; I ++)
{
Cin> a> B;
If (map [a] [B])
Continue;
Map [a] [B] = 1;
D [B] ++; // calculate the inbound level
}

// Find the value of 0
Int index = 0;
For (int I = 0; I <n; I ++)
{
For (int j = 0; j <n; j ++ ){
If (d [j] = 0)
{
D [j] --;
C [index ++] = j;
For (int k = 0; k <n; k ++)
If (map [j] [k])
D [k] --;
Break;
}
}
}
}
System ("pause ");
Return 0;
}

Related Article

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.