#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >;
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+5;
int n,m;
int in[N];//ru du shu zu入度数组
vector<int> e[N];
void topsort()
{
vector<int>v;
queue<int> q;
for(int i=1;i<=n;i++) if(in[i]==0) q.push(i);
while(q.size())
{
auto now=q.front();
q.pop();
v.pb(now);
for(auto spot:e[now])
{
if(--in[spot]==0) q.push(spot);
}
}
for(auto t:v) cout<<t<<" ";
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
while(cin>>x&&x!=0)//一直输入直到x等于0为止
{
e[i].pb(x);//qian bei xiang hou bei jian bian
in[x]++;
}
}
topsort();
return 0;
}
B3644 【模板】拓扑排序 / 家谱树
于 2024-08-04 14:58:04 首次发布