zoukankan      html  css  js  c++  java
  • UVA10305 Ordering Tasks

    题目链接:https://cn.vjudge.net/problem/UVA-10305(忍不住uva连接满)

    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 mn 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

     

    ***************************************************************************************

    题意:给你n个点(从1开始),还有要求一些点i必须在j前面。输出任意一种排序。

    题解:裸的拓扑排序,直接套模板。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 #define pb push_back
    14 #define mp make_pair
    15 #define ms(a, b)  memset((a), (b), sizeof(a))
    16 //#define LOCAL
    17 typedef long long LL;
    18 const int inf = 0x3f3f3f3f;
    19 const int maxn = 100+10;
    20 const int mod = 1e9+7;
    21 int gap[maxn][maxn];
    22 int topo[maxn], c[maxn], t;
    23 int n, m;
    24 bool dfs(int u){
    25     c[u] = -1;
    26     for(int v = 1; v<=n; v++)   if(gap[u][v]){
    27         if(c[v]< 0) return false;
    28         else if(!c[v] && !dfs(v) )  return false;
    29     }
    30     c[u] = 1;
    31     topo[--t] = u;
    32     return true;
    33 }
    34 bool toposort(){
    35     t = n+1;
    36     ms(c, 0);
    37     for(int u = 1; u<=n ;u++)   if(!c[u])
    38         if(!dfs(u)) return false;
    39     return true;
    40 }
    41 int main()
    42 {
    43     #ifdef LOCAL
    44         freopen("input.txt" , "r", stdin);
    45     #endif // LOCAL
    46     while(~scanf("%d%d", &n, &m)){
    47         if(n==0 && m==0)    break;
    48         ms(gap, 0);
    49         for(int i = 0;i<m;i++){
    50             int a, b;
    51             scanf("%d%d", &a, &b);
    52             gap[a][b] = 1;
    53         }
    54         if(toposort()){
    55             for(int i =1 ; i<=n ;i++)
    56                 if(i==1)    printf("%d", topo[i]);
    57                 else    printf(" %d", topo[i]);
    58             printf("
    ");
    59         }
    60     }
    61     return 0;
    62 }
    View Code


  • 相关阅读:
    nginx命令
    linux 命令
    js导出excel页面数据
    Linux上使用shell脚本查看内存情况(超实用)
    Gson解析json繁杂数据
    纯js制作遮罩层对话框
    简易树形菜单(可伸缩)
    一句实现jquery导航栏
    沁园春-雪
    python day3 int,str,list类型补充
  • 原文地址:https://www.cnblogs.com/denghaiquan/p/6668417.html
Copyright © 2011-2022 走看看