2015年4月29日星期三

[UVa] 10305 - Ordering Tasks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int main(){
  int n,m;
  
  while(scanf("%d %d",&n,&m)){ //n=tasks, m = pair of "<"
    if(!n && !m)break;

    int pre[n]; //入度
    int has[n]; //刪除與否
    int out[n][n]; //出邊
    for(int i=0;i<n;i++)has[i]=1;  //初始化拜訪
    memset(pre,0,sizeof(pre));
    memset(out,0,sizeof(out));
    //輸入約束組
    for(int i=0;i<m;i++){
      int a,b;
      scanf("%d %d",&a,&b);
      a--;
      b--;
      pre[b]++;
      out[a--][b]=1;
    }
    while(1){

      for(int i=0;i<n;i++){
        if(pre[i]==0 && has[i]==1){
          printf("%d",i+1);
          has[i]=0; //delete
          for(int j=0; j<n ;j++){
            if(out[i][j]==1 && i!=j){
              out[i][j]=0;
              pre[j]--;
            }
          }
          break;
        }
      }
      int stop=1;
      for(int i=0;i<n;i++){
        if(has[i]==1){
          stop = 0;
          printf(" ");
          break;
        }
      }
      if(stop==1)break;
    }
      printf("\n");
    
  }




  return 0;
}

沒有留言: