Hdu---1285--topology sequencing

Source: Internet
Author: User

The basic idea of topological ordering is:

1. Select a non-precursor vertex output from the graph

2. Delete this vertex and the arc starting with it

3. Repeat <1><2> operate until no precursor vertices are present

4. If the number of vertices in the output at this time is less than the number of vertices in the forward graph, then there is a loop in the direction graph, otherwise the order of the output vertices is a topological sort.

This problem is the simplest and most basic sort of topology

Description

There are N teams (1<=n<=500), numbered three-in-one, .... , N to play, after the game, the Referee Committee will be all the teams from the arrival of the ranking, but now the referee committee can not directly get each team's performance, only know the results of each game, that is, P1 win P2, with P1,p2 said, ranked P1 before P2. Now ask you to compile the program to determine the Rankings. 

Input

The input has several groups, the first behavior in each group is two number n (1<=n<=500), m, where n represents the number of troops, and m indicates the input data for the M ROW. In the next M-row data, there are also two integers per line p1,p2 means that the P1 team won the P2 Team. 

Output

Give a ranking that meets the Requirements. There is a space between the queue numbers at the time of the output, and no space after the Last.
Other Notes: qualifying rankings may not be unique, at which point the output is required to be numbered in front of the team; the input data is guaranteed to be correct, i.e. the input data ensures that there must be a qualifying Ranking.  

Sample Input

4 31 22) 34 3

Sample Output

1 2 4 3
#include <cstdio>#include<cstring>#include<iostream>#include<cmath>#include<vector>#include<queue>#include<algorithm>using namespaceStd;typedefLong LongLL;Const intmaxn=1005;Const intinf=0x3f3f3f3f;intmaps[maxn][maxn];intindegree[maxn];intp[maxn];intn, m;voidtoposort () {intk=0;  while(k<n)///if the element inside the p array is less than n, the loop continues.    {         for(intI=1; i<=n; i++)        {            if(!indegree[i]) {indegree[i]--;///reduce the value of the Indegree in the non-precursor to 1, otherwise no more points can be found without precursorsp[k++]=i;///Save the found number to the P array                 for(intj=1; j<=n; J + +)                    if(maps[i][j]) indegree[j]--;///Delete this vertex and the arc starting with it                 break; }        }    }}intmain () { while(~SCANF ("%d%d", &n, &M) {memset (maps,0,sizeof(maps)); Memset (p,0,sizeof(p)); Memset (indegree,0,sizeof(indegree));  while(m--)        {            inta, b; scanf ("%d%d", &a, &b); if(!maps[a][b])///this place needs to be careful .{maps[a][b]=1;///to mark the graph with a mapindegree[b]++;///number of records after drive}} Toposort ();  for(intI=0; i<n; i++) printf ("%d%c", p[i], i==n-1?'\ n':' '); }    return 0;}

Hdu---1285--topology sequencing

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.