C - Ordering Tasks UVA - 10305 【拓扑排序】


#include<iostream>
#include<cstring>

#define ms(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=1e3+10;
int n,m;
int a[maxn][maxn];
int in[maxn];
void toposort(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(in[j]==0){
                in[j]--;
                if(i!=n){
                    cout<<j<<' ';
                }
                else{
                    cout<<j<<endl;
                }
                for(int k=1;k<=n;k++){
                    if(a[j][k]) in[k]--;
                }
                break;
            }
        }
    }
}
int main(){
    std::ios::sync_with_stdio(false);
    int x,y;
    while(cin>>n>>m && n||m) {
        ms(a);
        ms(in);
        for(int i=1;i<=m;i++){
            cin>>x>>y;
      //    if(!a[x][y]){
                a[x][y]=1;
                in[y]++;
     //     }
        }
        toposort();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41333844/article/details/81429078