zoukankan      html  css  js  c++  java
  • BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154

    题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: a,b。表示 process b 要在 process a 之前完成。问经过 m 种关系之后,有没有可能完成所有的 process。

      可以利用拓扑排序的思想做。遍历所有 process,处理所有入度为 0 的点,然后把与该点关联的点,即度数都减一。这样处理完之后,每个点的度数应该都是-1,否则就代表有环,不能完成所有的process。

        

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 
     6 const int maxn = 100 + 10;
     7 int map[maxn][maxn];
     8 int in[maxn];
     9 int n, m;
    10 
    11 int main()
    12 {
    13     #ifndef ONLINE_JUDGE
    14         freopen("in.txt", "r", stdin);
    15     #endif // ONLINE_JUDGE
    16     int from, to;
    17     while (scanf("%d%d", &n, &m) != EOF) {
    18         memset(map, 0, sizeof(map));
    19         memset(in, 0, sizeof(in));
    20         for (int i = 0; i < m; i++) {
    21             scanf("%d%d", &to, &from);
    22             if (!map[from][to])
    23             {
    24                 map[from][to] = 1;
    25                 in[to]++;       // 入度
    26             }
    27         }
    28         for (int i = 1; i <= n; i++)
    29         {
    30             for (int j = 1; j <= n; j++)
    31             {
    32                 if (!in[j])
    33                 {
    34                     in[j] = -1;
    35                     for (int k = 1; k <= n; k++)
    36                     {
    37                         if (map[j][k])
    38                         {
    39                             map[j][k] = 0;
    40                             in[k]--;
    41                         }
    42                     }
    43                     break;
    44                 }
    45             }
    46         }
    47         bool flag = true;
    48         for (int i = 1; i <= n; i++)
    49         {
    50             if (in[i] != -1)
    51             {
    52                 flag = false;
    53                 break;
    54             }
    55         }
    56         printf("%s
    ", flag ? "YES" : "NO");
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    ubuntu下安装eclipse
    UTC时间、GMT时间、本地时间、Unix时间戳
    [转]mysql使用关键字作为列名的处理方式
    mysql日期格式化
    ssh远程登陆看不到用户名和主机名
    ssh以root用户远程登录失败
    PowerBI发布到网页
    视图是否有主键的问题
    select count(*)和select count(1)
    PPT产品的重要性
  • 原文地址:https://www.cnblogs.com/windysai/p/4215921.html
Copyright © 2011-2022 走看看