UVA-10305
题意:给出一些任务,有些任务是需要其他任务完成后才能进行的。保证不会存在需要相互需要作为前提的情况,求完成任务的顺序。
解题思路:a是b的前提任务,则b入度加一,a多一条指向b的边,然后拓扑一下就好了。
/*************************************************************************
> File Name: UVA-10305.cpp
> Author: Narsh
>
> Created Time: 2016年07月22日 星期五 11时14分33秒
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int map[110][110],d[110],n,m,a,b;
queue<int> q;
int main(){
while (scanf("%d%d",&n,&m) && n+m) {
memset(d,0,sizeof(d));
memset(map,0,sizeof(map));
for (int i = 1; i <= m; i++){
scanf("%d%d",&a,&b);
d[b]++;
map[a][b]=1;
}
for (int i = 1; i <= n; i++)
if (d[i] == 0)
q.push(i);
int l = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = 1; i <= n; i++)
if (map[x][i]) {
d[i]--;
if (d[i] == 0)
q.push(i);
}
l++;
printf("%d",x);
if (l < n) printf(" ");
else printf("\n");
}
}
}