拓扑排序
#include<iostream> #include<cstdio>#include<cmath>#include<cstring>using namespace std;int m,n,list[101],p=0,head[101],indgr[101],edgenum;//indgr:入度 struct node{int next,to;} edge[101];int stack[101],top=0;void push(int d){stack[++top]=d;}int pop(){return stack[top--];}void add(int x,int y){ edgenum++; edge[edgenum].to=y; edge[edgenum].next=head[x]; head[x]=edgenum; indgr[y]++;}void rm(int en)//edgenum{ edgenum--; indgr[edge[en].to]--; if(!indgr[edge[en].to]) push(edge[en].to); if(edge[en].next) rm(edge[en].next);}void read(){ cin >> n >> m; int u, v; for (int i = 1; i <= m; i++) { cin >> u >> v; add(u, v); }}void work(){ //初始化stack for(int i=1;i<=n;i++) if(!indgr[i]) push(i); while(top>0) { list[++p]=pop(); rm(head[list[p]]); } if(!edgenum) cout<<"Circle"; else for(int i=1;i<=p;i++) cout<<list[i];}int main(){ freopen("tppx.in", "r", stdin); read(); work(); return 0;}