题意:找到最强者,其实不用优先队列也可以.....简单拓扑排序,先判断度为零的点,因为要从小到大,所以直接循环即可.....
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 #define M 550 13 14 int du[M],tp[M][M]; 15 int n,m,i,j; 16 17 void topu() 18 { 19 priority_queue<int,vector<int>,greater<int> > Q; //保证小值int先出队列 20 for(i=1;i<=n;i++){ 21 if(du[i]==0) 22 Q.push(i); 23 } 24 bool flag=0; 25 while(!Q.empty()){ 26 int ans=Q.top(); 27 Q.pop(); 28 if(!flag){ 29 printf("%d",ans); 30 flag=1; 31 } 32 else{ 33 printf(" %d",ans); 34 } 35 for(i=1;i<=n;i++){ 36 if(tp[ans][i]){ 37 du[i]--; 38 if(du[i]==0) 39 Q.push(i); 40 } 41 } 42 } 43 printf("\n"); 44 } 45 46 int main() 47 { 48 int x,y; 49 while(~scanf("%d%d",&n,&m)){ 50 memset(du,0,sizeof(du)); 51 memset(tp,0,sizeof(tp)); 52 for(i=0;i<m;i++){ 53 scanf("%d%d",&x,&y); 54 if(!tp[x][y]){ 55 tp[x][y]=1; 56 du[y]++; 57 } 58 } 59 topu(); 60 } 61 }
原文:http://www.cnblogs.com/wangmengmeng/p/5023898.html