Poj 1094 Sorting It All Out (topological Sorting)

Source: Internet
Author: User

It only uses topological sorting for calculation! Each time an expression is added, its topological sorting is calculated:

1. Topology Sorting does not exist, which indicates that these expressions are in conflict.

2. If a unique topological sorting exists, the result can be output.

3. If there is no unique sorting, that is, there are points with the same inbound degree, this indicates that the sorting relationship cannot be determined or there is a result conflict (so when the sorting cannot be determined, determine whether a ring exists to determine whether a topological sorting exists)

 

 

[Cpp]
# Include <iostream>
// # Include <fstream>
Using namespace std;
# Define MAX 30
/* 340 K 16 MS */
// Var
Int n;
Int a [MAX] [MAX];
Bool flag1, flag2; // flag1 indicates that a conflict is found, and flag2 indicates that the expected result is displayed.
Char s [MAX];
// Fstream fin;
 
// Function
Void toposort ();
// Main Function
Int main ()
{
// Fin. open ("ipv4.txt", ios: in );
Int m;
While (cin> n> m)
{
If (n = 0 & m = 0) break;
Memset (a, 0, sizeof ());
Flag1 = flag2 = false;
Int count = 0;

Char b1, b2;
For (int I = 0; I <m; I ++)
{
Cin> b1> b2> b2;
If (flag1 | flag2) continue;
If (a [b1-'a'] [b2-'a'] = 0)
{
A [b1-'a'] [b2-'a'] = 1;
// Calculate the topological sorting
Toposort ();
}
++ Count;
}
// The first
If (flag1)
Cout <"Inconsistency found after" <count <"relations." <endl;
Else if (flag2)
Cout <"Sorted sequence determined after" <count <"relations:" <s <"." <endl;
Else
Cout <"Sorted sequence cannot be determined." <endl;

}
System ("pause ");
Return 0;
}
 
Void toposort ()
{
Int * ind = new int [n];
Memset (ind, 0, sizeof (int) * n );

// Calculate the inbound and outbound traffic
For (int I = 0; I <n; I ++)
For (int j = 0; j <n; j ++)
If (a [I] [j] = 1)
Ind [j] ++;

For (int I = 0; I <n; I ++)
{
// Point with an inbound value of 0
Int t =-1;
For (int j = 0; j <n; j ++)
{
If (ind [j] = 0 & t =-1)
T = j;
Else if (ind [j] = 0 & t! =-1)
{
// When multiple inbound values are 0, remember to judge whether the ring exists.
For (int kk = 0; kk <n; kk ++)
For (int ii = 0; ii <n; ii ++)
For (int jj = 0; jj <n; jj ++)
If (a [ii] [jj] | a [ii] [kk] & a [kk] [jj])
A [ii] [jj] = 1;

For (int ii = 0; ii <n; ii ++)
If (a [ii] [ii])
Flag1 = true;

Return;} // indicates that multiple vertices with an inbound value of 0 cannot be sorted.
}
If (t =-1)
{Flag1 = true; return;} // indicates that a ring exists.

// Subtract one degree from the corresponding edge
For (int j = 0; j <n; j ++)
If (a [t] [j] = 1)
Ind [j] --;

Ind [t] --;
S [I] = 'A' + t;
}
Flag2 = true;
S [n] = '\ 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.