zoukankan      html  css  js  c++  java
  • hdoj-1285-确定比赛名次(拓扑排序)

    题目链接

     1 /*
     2     Name:hdoj-1285-确定比赛名次
     3     Copyright:
     4     Author:
     5     Date: 2018/4/11 15:59:18
     6     Description:
     7         标准的拓扑排序模板题,注意要优先小号输出 
     8 */
     9 #include <iostream>
    10 #include <queue>
    11 #include <vector>
    12 #include <cstring>
    13 #include <algorithm>
    14 using namespace std;
    15 const int MAXN = 1e+5;
    16 int du[MAXN], n , m, L[MAXN];
    17 vector<int>  g[MAXN];
    18 int topsort() {
    19     memset(du, 0, sizeof(du));
    20     for (int i=0; i<n; i++) {
    21         for (int j=0; j<g[i].size(); j++) {
    22             du[g[i][j]]++; 
    23         }
    24     }
    25     int tot = 0;
    26     priority_queue<int, vector<int>, greater<int>> Q;
    27     for (int i=0; i<n; i++) {
    28         if (!du[i]) {
    29             Q.push(i);
    30         }
    31     }
    32     while (!Q.empty()) {
    33         int x = Q.top();
    34         Q.pop();
    35         L[tot++] = x;
    36         for (int j=0; j<g[x].size(); j++) {
    37             int t = g[x][j];
    38             du[t]--;
    39             if (!du[t]) {
    40                 Q.push(t);
    41             }
    42         } 
    43     }
    44     if (tot == n) return 1;
    45     return 0;
    46 } 
    47 int main()
    48 {
    49 //    freopen("in.txt", "r", stdin);
    50     while (cin>>n>>m) {
    51         memset(L, 0, sizeof(L));
    52         memset(g, 0, sizeof(g));
    53         for (int i=0; i<m; i++) {
    54             int a, b;
    55             cin>>a>>b;
    56             a--;
    57             b--;
    58             if (find(g[a].begin(), g[a].end(), b) != g[a].end()) continue;
    59             g[a].push_back(b);
    60         }
    61         if (topsort() == 1) {
    62             cout<<L[0]+1 ;
    63             for (int i=1; i<n; i++) {
    64                 cout<<" "<<L[i]+1;
    65             }
    66         }
    67         cout<<endl;
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/slothrbk/p/8796584.html
Copyright © 2011-2022 走看看