Determine the position of the match
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15037 Accepted Submission (s): 6022
Problem description has 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 inputs have several groups, the first behavior in each group is two n (1<=n<=500), M, where n represents the number of troops, and m represents 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 gives 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
Authorsmallbeer (CML)
SOURCE Hangzhou Electric ACM Training Team Training Tournament (VII) today, idle to see the social call topological sort ~! ~ It's quite simple ~
#include <iostream> #include <queue> #include <algorithm> #include <string.h> #include < stdio.h>using namespace Std;const int m=505;int graph[m][m];int digree[m];int main () {int n,m; while (cin>>n>>m) {memset (graph,0,sizeof (graph)); memset (digree,0,sizeof (Digree)); for (int i=1;i<=m;i++) {int A, B; cin>>a>>b; if (graph[a][b]==0) {graph[a][b]=1; digree[b]++; }} priority_queue<int, vector<int>,greater<int> >q; for (int k=1;k<=n;k++) {if (!digree[k]) Q.push (k); } int ok=1; while (!q.empty ()) {int cur=q.top (); if (OK) {cout<<cur;ok=0; } else cout<< "" <<cur; Q.pop (); for (int j=1;j<=n;j++) {if (Graph[cur][j]) {digree[j]--; if (digree[j]==0) Q.push (j); }}} cout<<endl; } return 0;}
There's another one that's basically the same as this one.
UVA 10305
#include <iostream> #include <string.h> #include <queue>using namespace Std;int graph[500][500];int Digree[500];int Main () {int n,m; while (cin>>n>>m,n+m) {memset (graph,0,sizeof (graph)); memset (digree,0,sizeof (Digree)); for (int i=1;i<=m;i++) {int A, B; cin>>a>>b; if (!graph[a][b]) {graph[a][b]=1; digree[b]++; }} queue<int>q; for (int j=1;j<=n;j++) {if (digree[j]==0) Q.push (j); } while (!q.empty ()) {int Cur=q.front (); cout<<cur<< ""; Q.pop (); for (int k=1;k<=n;k++) {if (Graph[cur][k]) {digree[k]--; if (digree[k]==0) Q.push (k); }}} cout<<endl; }}
Topology sequencing HDU 1285 determine the tournament position