0分全re求调

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

wangyuyao @ 2024-08-01 15:25:49

#include <bits/stdc++.h>
using namespace std;
vector<int> e[10005];
queue<int> q;
int in[10005], n;
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a;
        while (cin >> a && a != 0)
        {
            e[i].push_back(a);
            in[a]++;
        }
    }
    for(int i = 1; i <= n; i++)
        if(in[i] == 0)
            q.push(i);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        cout << x << " ";
        for(int i = 0; i <= e[x].size() - 1; i++)
        {
            in[e[x][i]]--;
            if(in[e[x][i]] == 0)
                q.push(e[x][i]);
        }
    }
    return 0;
}

by DLSunyukai @ 2024-08-02 09:28:18

用这个吧,yongTA AC

#include<bits/stdc++.h>
using namespace std;
int n,vis[114514];
vector<unsigned long long int>g[114514];
void toposort(){
    queue<int>q;
    for(int i=1;i<=n;i++) if(vis[i]==0) q.push(i);
    while(q.size()){
        int x=q.front();
        q.pop();
        cout<<x<<" ";
        for(int i=0;i<g[x].size();i++){
            vis[g[x][i]]--;
            if(vis[g[x][i]]==0) q.push(g[x][i]);
        }
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        while(1){
            int t;
            cin>>t;
            if(t==0) break;
            g[i].push_back(t);
            vis[t]++;
        }
    }
    toposort();
    return 0;
}

by wangyuyao @ 2024-08-04 16:37:28

感谢大佬???


by ZR_BL @ 2024-08-13 16:31:00

#include <bits/stdc++.h>
using namespace std;
vector<int> e[10005];
queue<int> q;
int in[10005], n;
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a;
        while (cin >> a && a != 0)
        {
            e[i].push_back(a);
            in[a]++;
        }
    }
    for(int i = 1; i <= n; i++)
        if(in[i] == 0)
            q.push(i);
    while(!q.empty())
    {
        int x = q.front();      
        q.pop();
        cout << x << " ";
        for(int i = 0; i <e[x].size() ; i++)
        {
            in[e[x][i]]--;
            if(in[e[x][i]] == 0)
                q.push(e[x][i]);
        }

    }
    return 0;
}

by ZR_BL @ 2024-08-13 16:31:44

问题


by wangyuyao @ 2024-08-15 13:34:23

感谢感谢???


|