#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e5+5;
int n,m;
int in[N];
vector<int> e[N];
void topsort()
{
queue<int> q;
vector<int> v;
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()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
while(cin>>x&&x!=0)
{
e[i].pb(x);
in[x]++;
}
}
topsort();
return 0;
}
[知识点]-[拓扑排序]
最新推荐文章于 2024-09-10 23:34:31 发布