#include #include #include typedef struct _vertex{ int label; struct _vertex *next; }vertex; void insert(vertex **L, int u, int v){ vertex *t,*p = L[v],*new; t = L[v]->next; while(t != NULL){ if(u > t->label){ p = t; t = t->next; } else if(u == t->label) return; else break; } new = (vertex*)malloc(sizeof(vertex)); new->label = u; new->next = t; if(L[v]->next == NULL) L[v]->next = new; else p->next = new; return; } int dfs(char *s ,vertex **L, int u, int *vis, int *back){ int i; vertex *p; vis[u] = back[u] = 1; p = L[u]->next; while(p != NULL){ if((!vis[p->label]) && dfs(s,L,p->label,vis,back)) return 1; else if(back[p->label]) return 1; p = p->next; } back[u] = 0; l += sprintf(s+l,"%d ",u); return 0; } /* int dfs(vertex **L, int u, int *vis){ */ /* int i; */ /* vertex *p; */ /* vis[u] = 1; */ /* p = L[u]->next; */ /* while(p != NULL){ */ /* if(!vis[p->label]) */ /* if(dfs(L,p->label,vis)) */ /* return 1; */ /* p = p->next; */ /* } */ /* printf("%d ", u); */ /* return 0; */ /* } */ int main(){ int n; printf("n = "); scanf("%d", &n); int u,v,i; vertex **L; vertex *t; L = (vertex**)malloc(n*sizeof(vertex*)); for(i=0; ilabel = i; L[i]->next = NULL; } printf("\nDependencies: "); while(1){ scanf("%d", &u); if (u < 0) break; scanf("%d", &v); if(v < 0) break; insert(L,u,v); } for(i=0; inext; t != NULL; t = t->next) printf("%d ",t->label); printf("\n"); } int *vis = (int*)malloc(n*sizeof(int)); int *back = (int*)malloc(n*sizeof(int)); char s[1000] = ""; for(i=0; i