HDU-1285(拓扑排序改变)

来源:互联网 发布:万方地方志数据库网址 编辑:程序博客网 时间:2024/04/28 04:47

其实只要弄明白拓扑排序的原理,自己还是很容易实现出自己的拓扑排序的,

但需要注意的几点,分别有:要注意重边,虽然这道题目,你注意不注意都可以过,

然后就是一定要注意注意注意加注意的一点是:找到一个就立马退出循环!

要不然 容易出现这种情况5 6    5 1,你找到了5 然后你不退出的话就会直接继续找6,但其实应该是输出1的..诶...

所以,以后还是要小心点的,当然多做定题目,多积累点经验还是有必要的.

贴出代码:

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>int N,M;//N stands for the number of points,and M represents the number of edges;struct Point{int out;int in;int adj;}head[505];struct Edge{int v;int next;}e[250005];int visit[505];int hash[505][505];int idx;void addedge(int a,int b){idx++;head[a].out++;head[b].in++;e[idx].v=b;e[idx].next=head[a].adj;head[a].adj=idx;}int main(){while(scanf("%d%d",&N,&M)!=EOF){int a,b;idx=0;memset(head,0,sizeof(head));memset(visit,0,sizeof(visit));memset(hash,0,sizeof(hash));for(int i=1;i<=M;i++){scanf("%d%d",&a,&b);if(!hash[a][b])//判断重边,{hash[a][b]=1;addedge(a,b);}}int k=0;for(int j=1;j<=N;j++){for(i=1;i<=N;i++){if(!visit[i]&&head[i].in==0){visit[i]=1;printf(k==0?"%d":" %d",i);for(int p=head[i].adj;p!=0;p=e[p].next){int v=e[p].v;if (head[v].in!=0) {//&&!visit[v]head[v].in--;}}k++;break;//这里...}}}printf("\n");}return 0;}


 

原创粉丝点击