30分(输入?)有问题,大佬求调

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

IT_theory @ 2024-07-31 08:46:48

#include<bits/stdc++.h>
using namespace std;
int ecnt,go[100005],adj[200010],nxt[200010],deg[100005],n,m;
void add(int u,int v){
    go[++ecnt]=v;
    nxt[ecnt]=adj[u];
    adj[u] = ecnt;
    deg[v]++;
}
void tuopu(){
    queue<int>que;
    for(int i = 1;i<=n;i++)
        if(deg[i]==0){
            printf("%d ",i);
            que.push(i);
            break;
        }
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int i = adj[u];i;i=nxt[i]){
            deg[go[i]]--;
            if(deg[go[i]]==0){
                printf("%d ",go[i]);
                que.push(go[i]);
            }
        }
    }
    return;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;i++){
        int tar;
        while(scanf("%d",&tar)!=EOF&&tar!=0){
            add(i,tar);

        }
    }
    tuopu();
    return 0;
}

by yjc15297932294 @ 2024-11-15 21:50:15


using namespace std;
int ecnt,go[100005],adj[200010],nxt[200010],deg[100005],n,m;
void add(int u,int v){
    go[++ecnt]=v;
    nxt[ecnt]=adj[u];
    adj[u] = ecnt;
    deg[v]++;
}
void tuopu(){
    queue<int>que;
    for(int i = 1;i<=n;i++)
        if(deg[i]==0){
            printf("%d ",i);
            que.push(i);
        }
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int i = adj[u];i;i=nxt[i]){
            deg[go[i]]--;
            if(deg[go[i]]==0){
                printf("%d ",go[i]);
                que.push(go[i]);
            }
        }
    }
    return;
}
int main(){
    scanf("%d",&n);
    for(int i = 1;i<=n;i++){
        int tar;
        while(scanf("%d",&tar)!=EOF&&tar!=0){
            add(i,tar);
        }
    }
    tuopu();
    return 0;
}```

by yjc15297932294 @ 2024-11-15 21:51:53

@IT_theory 入度为0的要全部加入进去,删掉break 其次输入错误,前面只需要输入一个n,不需要m


|