zoukankan      html  css  js  c++  java
  • SGU 230. Weighings (拓扑排序)

    题意:

          给出质量为1~n的n个箱子的m对轻重关系,输出一种可能的箱子的质量排列。


    Solution:

         拓扑排序,注意要处理重边。

    #include <iostream>
    #include <queue>
    using namespace std;
    
    const int N = 209;
    
    queue<int> q;
    bool G[N][N];
    int deg[N], ans[N];
    int n, m;
    
    int main()
    {
        ios::sync_with_stdio (0);
        cin >> n >> m;
        for (int i = 1, u, v; i <= m; ++i) {
            cin >> u >> v;
            if(!G[u][v]){
                G[u][v]=1;
                deg[v]++;
            }
        }
        for (int i = 1; i <= n; i++) {
            if (deg[i]==0) q.push (i);
        }
        int now = 0;
        while (!q.empty() ) {
            int u = q.front(); q.pop();
            ans[u] = ++now;
            for (int i = 1; i <= n; i++) {
                if (G[u][i] && (--deg[i]) == 0) {
                    q.push (i);
                }
            }
        }
        if (now == n) {
            for (int i = 1; i <= n; i++)
                cout << ans[i] << ' ';
        }
        else {
            cout << "No solution
    ";
        }
        return 0;
    }
    View Code
  • 相关阅读:
    CF 336494 C. Meme Problem
    MySql备份
    MySQL索引背后的数据结构及算法原理
    show profile 开启权限
    示例数据库
    索引使用策略及优化
    shiro权限登录案例
    Swagger技术
    Shiro框架
    shiro授权管理
  • 原文地址:https://www.cnblogs.com/keam37/p/4510317.html
Copyright © 2011-2022 走看看