• UVA10305 Ordering Tasks


     这是一道典型的拓扑排序题,  学长讲完拓扑序之后敲了一下, 结果wa了两发,也不知道错在了哪, 然后就给放到一边了,  今天又重新看了一遍,  在网上搜了一下大神的解题报告,    突然间看到了一句话 :   这道题有个坑  就是 n != 0 && m == 0   我一下子就发现了自己代码的问题。   果然 去掉了m == 0 的判断  A了 。

    果然还是太年轻。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 
     7 using namespace std;
     8 
     9 const int MaxN = 100;
    10 int pre[2 * MaxN + 5], last[MaxN + 5], other[2 * MaxN + 5];
    11 int all, n, m;
    12 int seq[MaxN + 5];
    13 int ind[105];
    14 
    15 void Build(int u, int v)
    16 {
    17     pre[++all] = last[u];
    18     last[u] = all;
    19     other[all] = v;
    20     ind[v]++;
    21 }
    22 
    23 void topo()
    24 {
    25     int head = 1, tial = 0;
    26     for(int i = 1; i <= n; i++)
    27         if(ind[i] == 0)
    28             seq[++tial] = i;
    29     while(head <= tial)
    30     {
    31         int now = seq[head];
    32         int ed = last[now], dr;
    33         while(ed != -1)
    34         {
    35             dr = other[ed];
    36             ind[dr]--;
    37             if(ind[dr] == 0) seq[++tial] = dr;
    38             ed = pre[ed];
    39         }
    40         head++;
    41     }
    42     for(int i = 1; i <= n; i++)
    43         printf("%d ", seq[i]);
    44         printf("
    ");
    45 }
    46 int main()
    47 {
    48     while(~scanf("%d%d", &n, &m) && n)
    49     {
    50         all = -1;
    51         memset(last, -1, sizeof(last));
    52         memset(ind, 0, sizeof(ind));
    53         memset(pre, 0, sizeof(pre));
    54         memset(other, 0, sizeof(other));
    55         memset(seq, 0, sizeof(seq));
    56         for(int i = 1; i <= m; i++)
    57         {
    58             int x, y;
    59             scanf("%d%d", &x, &y);
    60             Build(x, y);
    61         }
    62         topo();
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    如何使用反射技术获取泛型类的真实类型?
    applicationContext.xml文件如何调用外部properties等配置文件
    applicationContext.xml中的使用${}是代表什么意思?
    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
    MySql中LongText字段对应Hibernate映射文件的设置(转)
    ckeditor的详细配置(转)
    XML-学习
    WSDL-学习总结
    ONVIF-WSDL
    sourceinsight相关配置
  • 原文地址:https://www.cnblogs.com/ZZZZone/p/6106094.html
Copyright © 2020-2023  润新知