help!!!!!!!!!!!
查看原帖
help!!!!!!!!!!!
1057116
gjr19509楼主2023/8/4 22:47

六点怎么过?

#include <bits/stdc++.h>

using namespace std;
const int N = 100;
int num_nodes;
int graph[N][N];
int in[N];

void topolsort() {
    queue<int> q;
    vector<int> res;
    for (int i = 1; i <= num_nodes; ++i)
        if (!in[i])q.push(i);
    while (q.size()) {
        int p = q.front();
        q.pop();
        res.push_back(p);
        for (int i = 1; i <= num_nodes; ++i)
            if (graph[p][i])if (--in[i] == 0)q.push(i);
    }
    for (auto i: res)
        cout << i << ' ';
}

int main() {
    cin >> num_nodes;
    for (int i = 1; i <= num_nodes; ++i) {
        int n;
        while (cin >> n && n != 0) {
            graph[i][n] = 1;
            in[n] += 1;
        }
    }
    topolsort();
    return 0;
}
2023/8/4 22:47
加载中...