Topological sorting:
Idea: 1. Select a vertex without a precursor in the directed graph and Output
2. Delete the node and all edges ending with it.
Repeat the preceding steps to know that all vertices have been output, or the current graph does not have any vertices without a forward. The following condition indicates that a ring exists in the directed graph.
HDU1285: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1285
[Java]
Package D0731;
Import java. util .*;
// Graph sorting (graph storage using the adjacent matrix)
Public class HDU1285_2 {
Static int n, m;
Static int [] indegree; // The indegree of the vertex.
Static int [] result; // Save the final sorted result
Static int [] [] G; // storage of the adjacent matrix
Static Queue <Integer> que;
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
While (SC. hasNext ()){
N = SC. nextInt ();
M = SC. nextInt ();
G = new int [n + 1] [n + 1];
Indegree = new int [n + 1];
While (m --> 0 ){
Int u = SC. nextInt ();
Int v = SC. nextInt ();
If (G [u] [v] = 0) {// This condition must be met. Otherwise, WA can avoid duplicate edges. For example, a B may appear twice.
Indegree [v] ++;
G [u] [v] = 1;
}
}
Topsort ();
// Output
System. out. print (result [1]);
For (int I = 2; I <= n; I ++ ){
System. out. print ("" + result [I]);
}
System. out. println ();
}
}
Private static void topsort (){
Result = new int [n + 1];
Que = new PriorityQueue <Integer> ();
Int index = 0;
For (int I = 1; I <= n; I ++ ){
If (indegree [I] = 0)
Que. add (I );
}
While (! Que. isEmpty ()){
Int u = que. poll ();
Result [++ index] = u;
For (int I = 1; I <= n; I ++ ){
If (G [u] [I] = 1 ){
Indegree [I] --;
If (indegree [I] = 0)
Que. add (I );
}
}
}
}
}
[Java]
Package D0731;
Import java. util .*;
// Topological sorting (using an adjacent table)
Public class HDU1285_3 {
Static int n, m;
Static int [] indegree; // The indegree of the vertex.
Static int [] result; // Save the final sorted result
Static List <ArrayList <Integer> G; // The List of adjacent tables.
Static Queue <Integer> que;
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
While (SC. hasNext ()){
N = SC. nextInt ();
M = SC. nextInt ();
G = new ArrayList <Integer> ();
For (int I = 1; I <= n + 1; I ++)
G. add (new ArrayList <Integer> ());
Indegree = new int [n + 1];
While (m --> 0 ){
Int u = SC. nextInt ();
Int v = SC. nextInt ();
If (! G. get (u). contains (v) {// This condition must be met. Otherwise, WA can avoid duplicate edges. For example, a B may appear twice.
Indegree [v] ++;
G. get (u). add (v );
}
}
Topsort ();
// Output
System. out. print (result [1]);
For (int I = 2; I <= n; I ++ ){
System. out. print ("" + result [I]);
}
System. out. println ();
}
}
Private static void topsort (){
Result = new int [n + 1];
Que = new PriorityQueue <Integer> ();
Int index = 0;
For (int I = 1; I <= n; I ++ ){
If (indegree [I] = 0)
Que. add (I );
} Www.2cto.com
While (! Que. isEmpty ()){
Int v = que. poll ();
Result [++ index] = v;
For (int I: G. get (v )){
Indegree [I] --;
If (indegree [I] = 0)
Que. add (I );
}
}
}
}
Author: lhfight