拓扑排序 (模板)

优先队列 无vector
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;

int n, m;
vector <int> mp[N];
int ind[N];
bool topo() 
{
    int sum = 0;
    queue<int> Q;
    while(!Q.empty()) Q.pop();
    for(int i = 1; i <= n; i++)
        if(ind[i] == 0)
            Q.push(i);
    while(!Q.empty()) {
        int num = Q.front();
        Q.pop();
        sum++;
        for(int i = 0; i < mp[num].size(); i++){
            ind[mp[num][i]]--;
            if(ind[mp[num][i]] == 0) 
                Q.push(mp[num][i]);
        }
    }
//    cout << sum << endl;
    if(sum == n) return true;
    return false;
}
int main()
{
    int T;
    cin >> T;
    while(T--) {
        memset(ind,0,sizeof(ind)); 
        cin >> n >> m;
        for(int i = 1; i <= n; i++) mp[i].clear();
        for(int i = 0; i < m; i++) {
            int a, b;
            cin >> a >> b;
            mp[a].push_back(b);
            ind[b]++;
        }
        if(topo()) cout << "Correct" << endl;
        else cout << "Wrong" << endl;
    }
    return 0;
}
vector

猜你喜欢

转载自www.cnblogs.com/JiaaaaKe/p/11283601.html
今日推荐