1 ////#include<stdio.h> 2 ////#include<string.h> 3 ////#include<queue> 4 ////#include<algorithm> 5 ////using namespace std; 6 ////const int INF=0x3f3f3f3f; 7 ////const int MAXN=110; 8 ////const int MAXM=20010; 9 ////int top,vis[MAXN],dis[MAXN],head[MAXM]; 10 ////int N,M; 11 ////queue<int>dl; 12 ////struct Edge{ 13 //// int from,to,value,next; 14 //// }; 15 //// Edge edg[MAXM]; 16 ////void initial(){ 17 //// top=0; 18 //// memset(vis,0,sizeof(vis)); 19 //// memset(dis,INF,sizeof(dis)); 20 //// memset(head,-1,sizeof(head)); 21 //// while(!dl.empty())dl.pop(); 22 ////} 23 ////void add(int u,int v,int value){ 24 //// Edge E={u,v,value,head[u]}; 25 //// edg[top]=E; 26 //// head[u]=top++; 27 ////} 28 ////void SPFA(int sx){ 29 //// dl.push(sx); 30 //// dis[sx]=0; 31 //// vis[sx]=1; 32 //// while(!dl.empty()){ 33 //// int u=dl.front(); 34 //// dl.pop(); 35 //// vis[u]=0; 36 //// for(int i=head[u];i!=-1;i=edg[i].next){ 37 //// int v=edg[i].to; 38 //// if(dis[u]+edg[i].value<dis[v]){ 39 //// dis[v]=dis[u]+edg[i].value; 40 //// if(!vis[v]){ 41 //// vis[v]=1; 42 //// dl.push(v); 43 //// } 44 //// } 45 //// } 46 //// } 47 //// printf("%d\n",dis[N]); 48 ////} 49 ////int main(){ 50 //// int a,b,c; 51 //// while(~scanf("%d%d",&N,&M),N|M){ 52 //// initial(); 53 //// while(M--){ 54 //// scanf("%d%d%d",&a,&b,&c); 55 //// add(a,b,c); 56 //// add(b,a,c); 57 //// } 58 //// SPFA(1); 59 //// } 60 //// return 0; 61 ////} 62 ///**********************************************/ 63 //#include<stdio.h> 64 //#include<string.h> 65 //const int MAXN=110; 66 //const int INF=0x3f3f3f3f; 67 //int map[MAXN][MAXN],vis[MAXN],dis[MAXN]; 68 //int N,M; 69 //void initial(){ 70 // memset(map,INF,sizeof(map)); 71 // memset(vis,0,sizeof(vis)); 72 // memset(dis,INF,sizeof(dis)); 73 //} 74 //void dijskra(int sx){ 75 // dis[sx]=0; 76 // while(true){ 77 // int k=-1; 78 // for(int i=1;i<=N;i++) 79 // if(!vis[i]&&(k==-1||dis[i]<dis[k]))k=i; 80 // if(k==-1)break; 81 // vis[k]=1; 82 // for(int i=1;i<=N;i++) 83 // if(!vis[i]&&dis[k]+map[k][i]<dis[i]) 84 // dis[i]=dis[k]+map[k][i]; 85 // } 86 // printf("%d\n",dis[N]); 87 //} 88 //void add(){ 89 // int a,b,c; 90 // while(M--){ 91 // scanf("%d%d%d",&a,&b,&c); 92 // map[a][b]=map[b][a]=c; 93 // } 94 //} 95 //int main(){ 96 // while(~scanf("%d%d",&N,&M),N|M){ 97 // initial(); 98 // add(); 99 // dijskra(1); 100 // } 101 // return 0; 102 //} 103 ///**********************************************/ 104 //#include<stdio.h> 105 //#include<string.h> 106 //const int MAXN=10010; 107 //const int INF=0x3f3f3f3f; 108 //int N,M,ans,flot; 109 //int vis[MAXN],dis[MAXN],map[MAXN][MAXN]; 110 //void initial(){ 111 // memset(vis,0,sizeof(vis)); 112 // memset(dis,0x3f,sizeof(dis)); 113 // memset(map,0x3f,sizeof(map)); 114 // ans=0; 115 // flot=1; 116 //} 117 //void prime(){ 118 // vis[1]=1; 119 // for(int i=1;i<=N;i++)dis[i]=map[1][i]; 120 // while(true){ 121 // int temp=INF,k; 122 // for(int i=1;i<=N;i++) 123 // if(!vis[i]&&dis[i]<temp)temp=dis[k=i]; 124 // if(temp==INF)break; 125 // vis[k]=1; 126 // flot++; 127 // ans+=temp; 128 // for(int i=1;i<=N;i++) 129 // if(!vis[i]&&map[k][i]<dis[i])dis[i]=map[k][i]; 130 // } 131 // printf("%d %d\n",ans,flot); 132 //} 133 //void add(){ 134 // while(M--){int a,b,c; 135 // scanf("%d%d%d",&a,&b,&c); 136 // map[a][b]=map[b][a]=c; 137 // } 138 //} 139 //int main(){ 140 // while(~scanf("%d%d",&N,&M),N|M){ 141 // initial(); 142 // add(); 143 // prime(); 144 // } 145 // return 0; 146 //} 147 /*********************************************/ 148 #include<stdio.h> 149 #include<string.h> 150 #include<queue> 151 using namespace std; 152 const int MAXN=1010; 153 const int MAXM=100010; 154 int head[MAXM]; 155 struct Edge{ 156 int frome,to,next; 157 }; 158 Edge edg[MAXM]; 159 int N,M; 160 int que[MAXN],ans[MAXN],top,edgnum; 161 priority_queue<int,vector<int>,greater<int> >dl; 162 163 void initial(){ 164 memset(head,-1,sizeof(head)); 165 while(!dl.empty())dl.pop(); 166 memset(que,0,sizeof(que)); 167 top=0;edgnum=0; 168 } 169 void topu(){ 170 for(int i=1;i<=N;i++) 171 if(!que[i])dl.push(i); 172 while(!dl.empty()){ 173 int k=dl.top(); 174 dl.pop(); 175 ans[top++]=k; 176 for(int i=head[k];i!=-1;i=edg[i].next){ 177 que[edg[i].to]--; 178 if(que[edg[i].to]==0)dl.push(edg[i].to); 179 } 180 } 181 for(int i=0;i<top;i++)printf("%d ",ans[i]);puts(""); 182 } 183 void add(int a,int b){ 184 Edge E={a,b,head[a]}; 185 edg[edgnum]=E; 186 head[a]=edgnum++; 187 } 188 void get(){ 189 int a,b; 190 while(M--){ 191 scanf("%d%d",&a,&b); 192 que[b]++; 193 add(a,b); 194 } 195 } 196 int main(){ 197 while(~scanf("%d%d",&N,&M),N|M){ 198 initial(); 199 get(); 200 topu(); 201 } 202 return 0; 203 }
时间: 2024-11-02 23:29:45