HDU1285 (Topology Sorting (Template ))

Source: Internet
Author: User

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

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.