zoukankan      html  css  js  c++  java
  • hihoCoder 1175:拓扑排序二

    题目链接:

    http://hihocoder.com/problemset/problem/1175

    题目难度:一星级(简单题)

    今天闲来无事,决定刷一道水题。结果发现这道水题居然把我卡了将近一个钟头。

    最后终于调通了。总结起来,原因只有一个:不够仔细

    思路不用细说了,就是拓扑排序的简单应用。然而,一些不起眼的细节才是让你掉坑里的真正原因。

    猜猜哪儿可能出bug?

    // A simple problem, but you can't be too careful with it.
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <queue>
    #include <unordered_set>
    #include <vector>
    using namespace std;
    
    const int MOD = 142857;
    const int N = 100000;
    vector<unordered_set<int> > e;
    queue<int> q;
    int n, m, k;
    int ind[N];
    int res[N];
    
    int main()
    {
        int i;
        int x, y;
        
        scanf("%d%d%d", &n, &m, &k);
        memset(res, 0, sizeof(res));
        for (i = 0; i < k; ++i) {
            scanf("%d", &x);
            --x;
            res[x] = (res[x] + 1) % MOD;
        }
        
        e.resize(n);
        memset(ind, 0, sizeof(ind));
        for (i = 0; i < m; ++i) {
            scanf("%d%d", &x, &y);
            --x;
            --y;
            e[x].insert(y);
            ++ind[y];
        }
    
        for (i = 0; i < n; ++i) {
            if (ind[i] == 0) {
                q.push(i);
            }
        }
        while (!q.empty()) {
            i = q.front();
            q.pop();
            if (ind[i] > 0) {
                continue;
            }
            
            auto eit = e[i].begin();
            while(eit != e[i].end()) {
                --ind[*eit];
                res[*eit] = (res[*eit] + res[i]) % MOD;
                q.push(*eit);
                e[i].erase(*eit);
                eit = e[i].begin();
            }
        }
        
        int ans = 0;
        for (i = 0; i < n; ++i) {
            ans = (ans + res[i]) % MOD;
        }
        printf("%d
    ", ans);
        
        return 0;
    }
  • 相关阅读:
    C++基础学习6:内联函数
    lvm
    yum源
    mysql性能优化
    PXE
    dns配置
    进程命令
    ssh免密登陆和加密解密
    RAID阵列
    快速部署postfix邮件服务器
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/4852852.html
Copyright © 2011-2022 走看看