hdu1285 Topology ordering Java implementation

Source: Internet
Author: User
Tags in degrees

/* Problem Solving ideas: This is a typical topological sort, here we need to get from the input when the degree of no point, if the degree is zero, we can output from small to large.    Each time a point is output, it is necessary to eliminate all of its wide-spread edges (that is, to subtract one of the points connected to the point) and repeat the above operation.  */import Java.util.scanner;public class Main {//define global variables, use static int n,m after the aspect; Number of match teams and number of input game results static int[] degree,sorted;   The degrees of each point and whether it was searched for static int[][] arc; The connection between point and point of Arc is static Scanner sc=new Scanner (system.in);p ublic static void Main (string[] args) {while (Sc.hasnext ()) {n= Sc.nextint (); Enter the number of Match teams m=sc.nextint ();   Enter the number of times the tournament has been init (); Initialize topology diagram Toposort (); Topology sort}}//topology sort private static void Toposort () {int s=0;//is used to record the number of points that have been ordered while (s<n) {int i=0;//1) to find the junction for (; i<n) with a degree of 0 i++) {//If the entry is found to be 0 and has not been searched, get the I value if (degree[i]==0&&sorted[i]==0) {break;}} if (i==n) {//To determine if there is a loop, if there is a direct return System.out.println ("There is a circuit in the way, the problem is no solution!" "); return;} 2) out of the stack, the elimination of the Edge (the degree of all its subordinate nodes minus 1) sorted[i]=1;//searched for the mark is 1s++;//number of ordered points plus a System.out.print (i+1);//Output point if (s<n) {// Output a space according to the title, there is no space after the last value System.out.print ("");} Eliminating the edge water with the starting point of I as the end of the---j in degrees minus 1for (int j=0;j<n;j++) {if (arc[i][j]==1) {degree[j]--;}} System. OUT.PRINTLN ();} Initialize topology map private static void Init () {//Initialize sorted=new int[n];d egree=new int[n];arc=new int [n][n];for (int i=0;i<n;i++) {sorted[i]=0;//0 means not searched, 1 indicates that degree[i]=0 has been searched,//in degrees, initialized to 0//to initialize the connection between points and points, initialized to 0for (int j=0;j<n;j++) {arc[i][j]=0;}} Receive match result, for (int i=0;i<m;i++) {int a=sc.nextint () -1;int b=sc.nextint () -1;if (arc[a][b]==0) {//Prevent repetition of input of match result Arc[a][b ]=1; The correspondence between the point and the point degree[b]++; Corresponding point in degrees plus one}}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1285 Topology ordering Java implementation

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.