题目地址;http://acm.hdu.edu.cn/showproblem.php?pid=1285
马刺队刚刚拿到了西部冠军,开心ing,马刺队真是太伟大。希望总决赛加油!
开始了拓扑排序专题,发现一点都不会了。。于是先从最简单的开始做。。这题就不多说了。。直接上代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
int d[600], head[600], cnt, n, a[600], num;
struct node
{
int u, v, w;
int next;
} edge[10000];
void add(int u, int v, int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tops()
{
int i, j, u;
num=0;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(d[j]==0)
{
u=j;
d[j]--;
a[num++]=j;
break;
}
}
for(j=head[u]; j!=-1; j=edge[j].next)
{
if(edge[j].w==1)
{
edge[j].w--;
d[edge[j].v]--;
}
}
}
for(i=0; i<num; i++)
{
if(i!=num-1)
printf("%d ",a[i]);
else
printf("%d\n",a[i]);
}
}
int main()
{
int i, m, a, b;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
memset(d,0,sizeof(d));
cnt=0;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b,1);
d[b]++;
}
tops();
}
return 0;
}