help!!!!!!!!!!!

B3644 【模板】拓扑排序 / 家谱树

gjr19509 @ 2023-08-04 22:47:12

六点怎么过?

#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;
}

by wzb13958817049 @ 2023-08-04 23:08:09

@gjr19509 N的大小,因为101及以上不然会遍历不到,害的我帮你提交了好几次


by wzb13958817049 @ 2023-08-04 23:08:33

@wzb13958817049 应为,错别字别在意


|