zoukankan      html  css  js  c++  java
  • 拓扑排序()

    题目链接:http://hihocoder.com/problemset/problem/1174?sid=1322989

    讲解视频链接:https://www.bilibili.com/video/av22494171

    输入

    第1行:1个整数T,表示数据的组数T(1 <= T <= 5)
    接下来T组数据按照以下格式:
    第1行:2个整数,N,M。N表示课程总数量,课程编号为1..N。M表示顺序关系的数量。1 <= N <= 100,000. 1 <= M <= 500,000
    第2..M+1行:每行2个整数,A,B。表示课程A是课程B的前置课程。

    输出

    第1..T行:每行1个字符串,若该组信息无误,输出"Correct",若该组信息有误,输出"Wrong"。

    样例输入

    2
    2 2
    1 2
    2 1
    3 2
    1 2
    1 3

    样例输出

    Wrong
    Correct
     1 #include<iostream>
     2 #include<vector>
     3 #include<cstring>
     4 #include<queue>
     5 using namespace std;
     6 #define maxn 500000
     7 vector <int> vc[maxn];
     8 int n, m;
     9 int dis[maxn];
    10 bool toupu()
    11 {
    12     queue<int> q;
    13     while (!q.empty()) q.pop();
    14     for (int i = 1; i <= n; i++) if (!dis[i]) q.push(i);
    15     int ans = 0;
    16     while (!q.empty())
    17     {
    18         int temp = q.front(); q.pop();
    19         ans++;
    20         for (int i = 0; i < vc[temp].size(); i++)
    21         {
    22             if (--dis[vc[temp][i] ]== 0) q.push(vc[temp][i]);
    23         }
    24     }
    25     if (ans == n) return true;
    26     return false;
    27 }
    28 int main()
    29 {
    30     int t;
    31     cin >> t;
    32     while (t--)
    33     {
    34         cin >> n >> m;
    35         for (int i = 0; i < n; i++) vc[i].clear();
    36         memset(dis, 0, sizeof(dis));
    37         for (int i = 0; i < m; i++)
    38         {
    39             int a, b;
    40             cin >> a >> b;
    41             vc[a].push_back(b);
    42             dis[b]++;
    43         }
    44         if (!toupu()) cout << "Wrong" << endl;
    45         else cout << "Correct" << endl;
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    c++11之智能指针
    SurfaceFlinger与Surface概述
    android GUI 流程记录
    文章收藏
    android performance
    POJ3349
    java中的volatile和synchronized
    [原创]分期还款的名义利率与真实利率
    Java IO 流总结
    telegram
  • 原文地址:https://www.cnblogs.com/kangdong/p/9125588.html
Copyright © 2011-2022 走看看