先上題目編程
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7800 Accepted Submission(s): 3014
ide
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define MAX 550 5 using namespace std; 6 7 int s[MAX][MAX]; 8 9 10 struct cmp 11 { 12 bool operator() (int x,int y) 13 { 14 return x>y; 15 } 16 }; 17 18 priority_queue<int , vector<int>,cmp > p; 19 20 21 void deal(int n) 22 { 23 int i,j; 24 queue<int> q; 25 for(i=1;i<=n;i++) if(!s[i][0]) p.push(i); 26 while(!p.empty()) 27 { 28 i=p.top(); 29 q.push(i); 30 p.pop(); 31 for(j=1;j<=n;j++) 32 { 33 if(s[i][j]) 34 { 35 s[j][0]--; 36 if(!s[j][0]) p.push(j); 37 } 38 } 39 } 40 i=0; 41 while(!q.empty()) 42 { 43 if(i++) printf(" "); 44 printf("%d",q.front()); 45 q.pop(); 46 } 47 printf("\n"); 48 } 49 50 int main() 51 { 52 int n,m,i,x,y; 53 while(scanf("%d %d",&n,&m)!=EOF) 54 { 55 memset(s,0,sizeof(s)); 56 for(i=0;i<m;i++) 57 { 58 scanf("%d %d",&x,&y); 59 if(s[x][y]) continue; 60 s[x][y]=1; 61 s[y][0]++; 62 } 63 deal(n); 64 } 65 return 0; 66 }