Monday, May 30, 2011

10305 - Ordering Tasks


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define M 105
using namespace std;

struct Time
{
    int task,t;
}T[M];
int List[M][M],adjNo[M],color[M],t;
void DFS_Visit(int);
bool comp(Time,Time);

int main()
{
    int m,n,i,x,y;
    freopen("in.txt","r",stdin);

    while(scanf("%d%d",&n,&m)&&(n|m))
    {
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            List[x][adjNo[x]++] = y;
        }

        if(n==1)
        {
            printf("1\n");
            continue;
        }

        t = 0;
        for(i=1;i<=n;i++)
        {
            if(color[i]==0)
            {
                DFS_Visit(i);
            }
        }
        sort(T+1,T+n+1,comp);

        printf("%d",T[1].task);
        for(i = 2;i <= n;i++)
            printf(" %d",T[i].task);
        printf("\n");

        memset(color,0,sizeof(color));
        memset(adjNo,0,sizeof(adjNo));
    }
    return 0;
}

void DFS_Visit(int s)
{
    int i,x;
    color[s] = 1;
    t++;

    for(i=0;i<adjNo[s];i++)
    {
        x = List[s][i];
        if(color[x]==0)
            DFS_Visit(x);
    }
    T[s].t = ++t;T[s].task = s;
}

bool comp(Time t1,Time t2)
{
    if(t1.t < t2.t )
        return false;
    return true;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.