zoukankan      html  css  js  c++  java
  • M

    Description

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.

    Input

    The input will consist of several instances of the problem. Each instance begins with a line containing two integers, 1 <= n <= 100 and m. n is the number of tasks (numbered from 1 to n) and m is the number of direct precedence relations between tasks. After this, there will be m lines with two integers i and j, representing the fact that task i must be executed before task j. An instance with n = m = 0 will finish the input.

    Output

    For each instance, print a line with n integers representing the tasks in a possible order of execution.

    Sample Input

    	5 4
    	1 2
    	2 3
    	1 3
    	1 5
    	0 0

    Sample Output

    	1 4 2 5 3


    解题思路:拓扑排序加上dfs。不错的一道题

    代码如下:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    int ss[101][101],num[101],daan[101],m,n,t;
    bool bfs(int i)
    {
      num[i]=-1;
      for(int t=1;t<=m;t++) if(ss[i][t]==1)
              {
                   if(num[t]==-1)return false;
                   else if(!num[t]) bfs(t);
              }
              num[i]=1;daan[t--]=i;
              return true;
    }
    bool bbb()
    {
      t=m;
      memset(num,0,sizeof(num));
      for(int y=1;y<=m;y++)
        if(!num[y]&&!bfs(y))
         return false;
         return true;
    }
    int main()
    {
         int a,b;
         while(scanf("%d%d",&m,&n)==2)
         {    memset(ss,0,sizeof(ss));
              if(m==0&&n==0)break;
              for(int i=1;i<=n;i++)
              {
                   scanf("%d%d",&a,&b);
                   ss[a][b]=1;
              }
              if(bbb())
              {
                   for(int z=1;z<m;z++)
                    printf("%d ",daan[z]);
                     printf("%d ",daan[m]);
              }
                 else printf("No ");
         }
         return 0;
    }

     
  • 相关阅读:
    sql like模糊查询
    mysql没有delete操作,那是delete from操作,
    j详细说明ava于clone办法
    基于ZooKeeper的Dubbo简单抽样登记中心
    度小于所述过程:es.exe
    Android 支付宝钱包手势password裂纹战斗
    How draw a stem -and -leaf &amp; box-plot display by R.or Python
    POJ 2420 A Star not a Tree? (模拟退火)
    记userscripts.org
    基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple)
  • 原文地址:https://www.cnblogs.com/441179572qqcom/p/5693293.html
Copyright © 2011-2022 走看看