拓扑排序

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

#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
int w[101][101];
int in[101],s[101];
bool a[101];
stack<int>st;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int t;
		while(cin>>t&&t!=0){
			s[i]++;
			w[i][s[i]]=t;
			in[t]++;
		}
	}
	for(int i=1;i<=n;i++){
		if(!in[i]){
			st.push(i);
		}
	}
	do{
		int num=st.top();
		st.pop();
		cout<<num<<" ";
		for(int j=1;j<=s[num];j++){
			in[w[num][j]]--;
			if(!in[w[num][j]]){
				st.push(w[num][j]);
			}
		}
	}while(!st.empty());
	
	cout<<endl;
	return 0;
}
posted @ 2025-11-26 13:54  10mbps_十兆網路  阅读(0)  评论(0)    收藏  举报