hdu1285 topological sequence

Source: Internet
Author: User

Test instructions: 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. (The Chinese problem surface will not speak)

The main problem needs to be sorted by the minimum dictionary order, so the priority queue is used instead of the queue to complete the topology sort.

1#include <stdio.h>2#include <string.h>3#include <queue>4#include <algorithm>5 using namespacestd;6 Const intmaxm=505;7 8 intid[maxm],n,m;9 intMA[MAXM][MAXM];Ten  One voidtopo () { Apriority_queue<int,vector<int>,greater<int> >Q; -      for(intI=1; i<=n;i++){ -         if(!Id[i]) Q.push (i); the     } -     intCnt=0; -      while(!Q.empty ()) { -         intu=q.top (); +printf"%d", u); -         if(++cnt==n) printf ("\ n"); +         Elseprintf" "); A Q.pop (); at          for(intI=1; i<=n;++i) { -             if(I==u)Continue; -             if(Ma[u][i]) { -id[i]--; -                 if(!Id[i]) Q.push (i); -             } in         } -     } to } +  - intMain () { the      while(SCANF ("%d%d", &n,&m)! =EOF) { *Memset (MA,0,sizeof(MA)); $          while(m--){Panax Notoginseng             intb; -scanf"%d%d",&a,&b); the             if(!Ma[a][b]) { +ma[a][b]=1; Aid[b]++; the             } +         } - topo (); $     } $     return 0; -}
View Code

hdu1285 topological sequence

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.