poj2367Genealogical tre

来源:互联网 发布:jdk 7u79 windows x64 编辑:程序博客网 时间:2024/05/20 13:17

原题连接:http://poj.org/problem?id=2367

思路:拓扑排序

#include<stdio.h>#include<stdlib.h>typedef struct vert{int num;           //结点编号struct vert* next;}G_v;typedef struct node{int InDgree,OutDgree;            //出度和入度G_v* next;}G_n;G_n P[101];                         //结点int main(void){G_v *tmp;G_v *q;int n,i,a,j,count;scanf("%d",&n);for(i = 0;i <= n;i ++){P[i].next = NULL;P[i].InDgree = P[i].OutDgree = 0;}i = 1;while(i <= n){tmp = P[i].next;while(scanf("%d",&a)&&a != 0){P[i].OutDgree ++;P[a].InDgree ++;q = (G_v*)malloc(sizeof(G_v));q->num = a;q->next = NULL;if(tmp == NULL){P[i].next = q;tmp = P[i].next;}else{tmp->next = q;tmp = tmp->next;}}i ++;}count = 1;for(i = 1;i <= n;i ++){if(P[i].InDgree == 0){if(count++ == n){   printf("%d\n",i);   break;}elseprintf("%d ",i);tmp = P[i].next;while(tmp){P[tmp->num].InDgree --;tmp = tmp->next;}    P[i].InDgree = 200;i = 0;}}return 0;}


0 0
原创粉丝点击